From 0f906d27b001a17853e1249e66d8a2826e7e943f Mon Sep 17 00:00:00 2001 From: Shrikar Vasisht Date: Wed, 7 Dec 2022 19:53:32 -0500 Subject: [PATCH 1/5] added binary insertion sort --- sorts/binary_insertion_sort.py | 42 ++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 sorts/binary_insertion_sort.py diff --git a/sorts/binary_insertion_sort.py b/sorts/binary_insertion_sort.py new file mode 100644 index 000000000000..b79669916a6a --- /dev/null +++ b/sorts/binary_insertion_sort.py @@ -0,0 +1,42 @@ +""" +This is a pure Python implementation of the binary insertion sort algorithm + +For doctests run following command: +python -m doctest -v binary_insertion_sort.py +or +python3 -m doctest -v binary_insertion_sort.py + +For manual testing run: +python binary_insertion_sort.py +""" + + +def binary_insertion_sort(collection): + """Pure implementation of the binary insertion sort algorithm in Python + :param collection: some mutable ordered collection with heterogeneous + comparable items inside + :return: the same collection ordered by ascending + """ + + n = len(collection) + for i in range(1, n): + val = collection[i] + low = 0, high = i - 1 + + while low <= high: + mid = (low + high) // 2 + if val < collection[mid]: + high = mid - 1 + else: + low = mid + 1 + for j in range(i, low, -1): + collection[j] = collection[j - 1] + collection[low] = val + return collection + + + +if __name__ == "__main__": + user_input = input("Enter numbers separated by a comma:\n").strip() + unsorted = [int(item) for item in user_input.split(",")] + print(binary_insertion_sort(unsorted)) From 03dca58dae0ae512071b42ccad2c27b9c2e8681c Mon Sep 17 00:00:00 2001 From: Shrikar Vasisht Date: Wed, 7 Dec 2022 19:54:45 -0500 Subject: [PATCH 2/5] added binary insertion sort working --- sorts/binary_insertion_sort.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/sorts/binary_insertion_sort.py b/sorts/binary_insertion_sort.py index b79669916a6a..3403fdc91f8e 100644 --- a/sorts/binary_insertion_sort.py +++ b/sorts/binary_insertion_sort.py @@ -21,7 +21,8 @@ def binary_insertion_sort(collection): n = len(collection) for i in range(1, n): val = collection[i] - low = 0, high = i - 1 + low = 0 + high = i - 1 while low <= high: mid = (low + high) // 2 From 3388753891359f78fca73d0ea7f807443db9393b Mon Sep 17 00:00:00 2001 From: Shrikar Vasisht Date: Wed, 7 Dec 2022 20:27:05 -0500 Subject: [PATCH 3/5] added binary insertion sort working --- sorts/binary_insertion_sort.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/sorts/binary_insertion_sort.py b/sorts/binary_insertion_sort.py index 3403fdc91f8e..0b67328dad69 100644 --- a/sorts/binary_insertion_sort.py +++ b/sorts/binary_insertion_sort.py @@ -16,8 +16,28 @@ def binary_insertion_sort(collection): :param collection: some mutable ordered collection with heterogeneous comparable items inside :return: the same collection ordered by ascending + + Examples: + >>> binary_insertion_sort([0, 4, 1234, 4, 1]) + [0, 1, 4, 4, 1234] + >>> binary_insertion_sort([]) == sorted([]) + True + >>> binary_insertion_sort([-1, -2, -3]) == sorted([-1, -2, -3]) + True + >>> binary_insertion_sort(['d', 'a', 'b', 'e', 'c']) == sorted(['d', 'a', 'b', 'e', 'c']) + True + >>> import random + >>> collection = random.sample(range(-50, 50), 100) + >>> binary_insertion_sort(collection) == sorted(collection) + True + >>> import string + >>> collection = random.choices(string.ascii_letters + string.digits, k=100) + >>> binary_insertion_sort(collection) == sorted(collection) + True """ + + n = len(collection) for i in range(1, n): val = collection[i] From f5824bdb3c0cb3759ed8b82cd6301f98aacb8df4 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 8 Dec 2022 01:33:25 +0000 Subject: [PATCH 4/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- sorts/binary_insertion_sort.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/sorts/binary_insertion_sort.py b/sorts/binary_insertion_sort.py index 0b67328dad69..02b9e040d42c 100644 --- a/sorts/binary_insertion_sort.py +++ b/sorts/binary_insertion_sort.py @@ -36,8 +36,6 @@ def binary_insertion_sort(collection): True """ - - n = len(collection) for i in range(1, n): val = collection[i] @@ -56,7 +54,6 @@ def binary_insertion_sort(collection): return collection - if __name__ == "__main__": user_input = input("Enter numbers separated by a comma:\n").strip() unsorted = [int(item) for item in user_input.split(",")] From fac508960b493b955b816998782102983ae0760a Mon Sep 17 00:00:00 2001 From: Shrikar Vasisht Date: Wed, 7 Dec 2022 20:39:38 -0500 Subject: [PATCH 5/5] fixed line length issues --- sorts/binary_insertion_sort.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/sorts/binary_insertion_sort.py b/sorts/binary_insertion_sort.py index 0b67328dad69..a62839c65b65 100644 --- a/sorts/binary_insertion_sort.py +++ b/sorts/binary_insertion_sort.py @@ -11,7 +11,7 @@ """ -def binary_insertion_sort(collection): +def binary_insertion_sort(collection: list) -> list: """Pure implementation of the binary insertion sort algorithm in Python :param collection: some mutable ordered collection with heterogeneous comparable items inside @@ -24,7 +24,8 @@ def binary_insertion_sort(collection): True >>> binary_insertion_sort([-1, -2, -3]) == sorted([-1, -2, -3]) True - >>> binary_insertion_sort(['d', 'a', 'b', 'e', 'c']) == sorted(['d', 'a', 'b', 'e', 'c']) + >>> lst = ['d', 'a', 'b', 'e', 'c'] + >>> binary_insertion_sort(lst) == sorted(lst) True >>> import random >>> collection = random.sample(range(-50, 50), 100)