MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / rec_insertion_sort

Function rec_insertion_sort

sorts/recursive_insertion_sort.py:8–36  ·  view source on GitHub ↗

Given a collection of numbers and its length, sorts the collections in ascending order :param collection: A mutable collection of comparable elements :param n: The length of collections >>> col = [1, 2, 1] >>> rec_insertion_sort(col, len(col)) >>> col [1, 1, 2]

(collection: list, n: int)

Source from the content-addressed store, hash-verified

6
7
8def rec_insertion_sort(collection: list, n: int):
9 """
10 Given a collection of numbers and its length, sorts the collections
11 in ascending order
12
13 :param collection: A mutable collection of comparable elements
14 :param n: The length of collections
15
16 >>> col = [1, 2, 1]
17 >>> rec_insertion_sort(col, len(col))
18 >>> col
19 [1, 1, 2]
20
21 >>> col = [2, 1, 0, -1, -2]
22 >>> rec_insertion_sort(col, len(col))
23 >>> col
24 [-2, -1, 0, 1, 2]
25
26 >>> col = [1]
27 >>> rec_insertion_sort(col, len(col))
28 >>> col
29 [1]
30 """
31 # Checks if the entire collection has been sorted
32 if len(collection) <= 1 or n <= 1:
33 return
34
35 insert_next(collection, n - 1)
36 rec_insertion_sort(collection, n - 1)
37
38
39def insert_next(collection: list, index: int):

Callers 1

Calls 1

insert_nextFunction · 0.85

Tested by

no test coverage detected