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)
| 6 | |
| 7 | |
| 8 | def 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 | |
| 39 | def insert_next(collection: list, index: int): |
no test coverage detected