A recursive implementation of the insertion sort algorithm
|
Inserts the '(index-1)th' element into place |
|
Given a collection of numbers and its length, sorts the collections |
Inserts the ‘(index-1)th’ element into place
>>> col = [3, 2, 4, 2]
>>> insert_next(col, 1)
>>> col
[2, 3, 4, 2]
>>> col = [3, 2, 3]
>>> insert_next(col, 2)
>>> col
[3, 2, 3]
>>> col = []
>>> insert_next(col, 1)
>>> col
[]
Given a collection of numbers and its length, sorts the collections in ascending order
collection – A mutable collection of comparable elements
n – The length of collections
>>> col = [1, 2, 1]
>>> rec_insertion_sort(col, len(col))
>>> col
[1, 1, 2]
>>> col = [2, 1, 0, -1, -2]
>>> rec_insertion_sort(col, len(col))
>>> col
[-2, -1, 0, 1, 2]
>>> col = [1]
>>> rec_insertion_sort(col, len(col))
>>> col
[1]