22A recursive implementation of the insertion sort algorithm
33"""
44
5- def rec_insertion_sort (collection , n ):
5+ from typing import List
6+
7+ def rec_insertion_sort (collection : List , n : int ):
68 """
79 Given a collection of numbers and its length, sorts the collections
810 in ascending order
911
10- :param collection: A mutable collection of heterogenous, comparable elements
12+ :param collection: A mutable collection of comparable elements
1113 :param n: The length of collections
1214
1315 >>> col = [1, 2, 1]
@@ -25,18 +27,33 @@ def rec_insertion_sort(collection, n):
2527 >>> print(col)
2628 [1]
2729 """
28-
29-
3030 #Checks if the entire collection has been sorted
3131 if len (collection ) <= 1 or n <= 1 :
3232 return
3333
3434
35- data_swap (collection , n - 1 )
35+ insert_next (collection , n - 1 )
3636 rec_insertion_sort (collection , n - 1 )
3737
38- def data_swap (collection , index ):
38+ def insert_next (collection : List , index : int ):
39+ """
40+ Inserts the '(index-1)th' element into place
41+
42+ >>> col = [3, 2, 4, 2]
43+ >>> insert_next(col, 1)
44+ >>> print(col)
45+ [2, 3, 4, 2]
46+
47+ >>> col = [3, 2, 3]
48+ >>> insert_next(col, 2)
49+ >>> print(col)
50+ [3, 2, 3]
3951
52+ >>> col = []
53+ >>> insert_next(col, 1)
54+ >>> print(col)
55+ []
56+ """
4057 #Checks order between adjacent elements
4158 if index >= len (collection ) or collection [index - 1 ] <= collection [index ]:
4259 return
@@ -46,7 +63,7 @@ def data_swap(collection, index):
4663 collection [index ], collection [index - 1 ]
4764 )
4865
49- data_swap (collection , index + 1 )
66+ insert_next (collection , index + 1 )
5067
5168if __name__ == "__main__" :
5269 numbers = input ("Enter integers seperated by spaces: " )
0 commit comments