mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-07-05 09:21:13 +08:00
Create pull_request_template.md (#1684)
* Create pull_request_template.md * fixup! Format Python code with psf/black push * Update pull_request_template.md * updating DIRECTORY.md * Update pull_request_template.md * Update pull_request_template.md * Update pull_request_template.md * Update pull_request_template.md * Update pull_request_template.md * Typos and formatting Co-authored-by: John Law <johnlaw.po@gmail.com>
This commit is contained in:

committed by
John Law

parent
75523f9c1a
commit
b492e64417
@ -4,6 +4,7 @@ A recursive implementation of the insertion sort algorithm
|
||||
|
||||
from typing import List
|
||||
|
||||
|
||||
def rec_insertion_sort(collection: List, n: int):
|
||||
"""
|
||||
Given a collection of numbers and its length, sorts the collections
|
||||
@ -27,13 +28,13 @@ def rec_insertion_sort(collection: List, n: int):
|
||||
>>> print(col)
|
||||
[1]
|
||||
"""
|
||||
#Checks if the entire collection has been sorted
|
||||
# Checks if the entire collection has been sorted
|
||||
if len(collection) <= 1 or n <= 1:
|
||||
return
|
||||
|
||||
insert_next(collection, n - 1)
|
||||
rec_insertion_sort(collection, n - 1)
|
||||
|
||||
insert_next(collection, n-1)
|
||||
rec_insertion_sort(collection, n-1)
|
||||
|
||||
def insert_next(collection: List, index: int):
|
||||
"""
|
||||
@ -54,17 +55,19 @@ def insert_next(collection: List, index: int):
|
||||
>>> print(col)
|
||||
[]
|
||||
"""
|
||||
#Checks order between adjacent elements
|
||||
# Checks order between adjacent elements
|
||||
if index >= len(collection) or collection[index - 1] <= collection[index]:
|
||||
return
|
||||
|
||||
#Swaps adjacent elements since they are not in ascending order
|
||||
# Swaps adjacent elements since they are not in ascending order
|
||||
collection[index - 1], collection[index] = (
|
||||
collection[index], collection[index - 1]
|
||||
collection[index],
|
||||
collection[index - 1],
|
||||
)
|
||||
|
||||
insert_next(collection, index + 1)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
numbers = input("Enter integers seperated by spaces: ")
|
||||
numbers = [int(num) for num in numbers.split()]
|
||||
|
Reference in New Issue
Block a user