From baf7a8da1b9adfe424138a3bbc4dbf358387d252 Mon Sep 17 00:00:00 2001 From: Sharan Krishnan Date: Mon, 13 Jan 2020 19:02:24 +1100 Subject: [PATCH 1/2] A recursive insertion sort --- sorts/recursive_insertion_sort.py | 55 +++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 sorts/recursive_insertion_sort.py diff --git a/sorts/recursive_insertion_sort.py b/sorts/recursive_insertion_sort.py new file mode 100644 index 000000000000..1cd30c2e5738 --- /dev/null +++ b/sorts/recursive_insertion_sort.py @@ -0,0 +1,55 @@ +""" +A recursive implementation of the insertion sort algorithm +""" + +def rec_insertion_sort(collection, n): + """ + Given a collection of numbers and its length, sorts the collections + in ascending order + + :param collection: A mutable collection of heterogenous, comparable elements + :param n: The length of collections + + >>> col = [1, 2, 1] + >>> rec_insertion_sort(col, len(col)) + >>> print(col) + [1, 1, 2] + + >>> col = [2, 1, 0, -1, -2] + >>> rec_insertion_sort(col, len(col)) + >>> print(col) + [-2, -1, 0, 1, 2] + + >>> col = [1] + >>> rec_insertion_sort(col, len(col)) + >>> print(col) + [1] + """ + + + #Checks if the entire collection has been sorted + if len(collection) <= 1 or n <= 1: + return + + + data_swap(collection, n-1) + rec_insertion_sort(collection, n-1) + +def data_swap(collection, index): + + #Checks order between adjacent elements + if index >= len(collection) or collection[index - 1] <= collection[index]: + return + + #Swaps adjacent elements since they are not in ascending order + collection[index - 1], collection[index] = ( + collection[index], collection[index - 1] + ) + + data_swap(collection, index + 1) + +if __name__ == "__main__": + numbers = input("Enter integers seperated by spaces: ") + numbers = [int(num) for num in numbers.split()] + rec_insertion_sort(numbers, len(numbers)) + print(numbers) From 0af0c940e16581c6218ddc8eb45f38bbcffcbcb1 Mon Sep 17 00:00:00 2001 From: Sharan Krishnan Date: Mon, 13 Jan 2020 20:27:17 +1100 Subject: [PATCH 2/2] added doctests and typehints --- sorts/recursive_insertion_sort.py | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/sorts/recursive_insertion_sort.py b/sorts/recursive_insertion_sort.py index 1cd30c2e5738..a8bd2b9114ad 100644 --- a/sorts/recursive_insertion_sort.py +++ b/sorts/recursive_insertion_sort.py @@ -2,12 +2,14 @@ A recursive implementation of the insertion sort algorithm """ -def rec_insertion_sort(collection, n): +from typing import List + +def rec_insertion_sort(collection: List, n: int): """ Given a collection of numbers and its length, sorts the collections in ascending order - :param collection: A mutable collection of heterogenous, comparable elements + :param collection: A mutable collection of comparable elements :param n: The length of collections >>> col = [1, 2, 1] @@ -25,18 +27,33 @@ def rec_insertion_sort(collection, n): >>> print(col) [1] """ - - #Checks if the entire collection has been sorted if len(collection) <= 1 or n <= 1: return - data_swap(collection, n-1) + insert_next(collection, n-1) rec_insertion_sort(collection, n-1) -def data_swap(collection, index): +def insert_next(collection: List, index: int): + """ + Inserts the '(index-1)th' element into place + + >>> col = [3, 2, 4, 2] + >>> insert_next(col, 1) + >>> print(col) + [2, 3, 4, 2] + + >>> col = [3, 2, 3] + >>> insert_next(col, 2) + >>> print(col) + [3, 2, 3] + >>> col = [] + >>> insert_next(col, 1) + >>> print(col) + [] + """ #Checks order between adjacent elements if index >= len(collection) or collection[index - 1] <= collection[index]: return @@ -46,7 +63,7 @@ def data_swap(collection, index): collection[index], collection[index - 1] ) - data_swap(collection, index + 1) + insert_next(collection, index + 1) if __name__ == "__main__": numbers = input("Enter integers seperated by spaces: ")