sorts.recursive_insertion_sort

A recursive implementation of the insertion sort algorithm

Attributes

numbers

Functions

insert_next(collection, index)

Inserts the '(index-1)th' element into place

rec_insertion_sort(collection, n)

Given a collection of numbers and its length, sorts the collections

Module Contents

sorts.recursive_insertion_sort.insert_next(collection: list, index: int)

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
[]
sorts.recursive_insertion_sort.rec_insertion_sort(collection: list, n: int)

Given a collection of numbers and its length, sorts the collections in ascending order

Parameters:
  • 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]
sorts.recursive_insertion_sort.numbers