From 9f7862c34e77867ee3cfa5c85a94181a4b2907c2 Mon Sep 17 00:00:00 2001 From: Bisma nadeem <130698042+Bisma-Nadeemm@users.noreply.github.com> Date: Tue, 24 Oct 2023 11:46:18 -0700 Subject: [PATCH 1/3] Code enhancements in binary_insertion_sort.py --- sorts/binary_insertion_sort.py | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/sorts/binary_insertion_sort.py b/sorts/binary_insertion_sort.py index 8d41025583b1..a9b3818be7ab 100644 --- a/sorts/binary_insertion_sort.py +++ b/sorts/binary_insertion_sort.py @@ -12,10 +12,11 @@ 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 - :return: the same collection ordered by ascending + """ + Sorts a list using the binary insertion sort algorithm. + + :param collection: A mutable ordered collection with comparable items. + :return: The same collection ordered in ascending order. Examples: >>> binary_insertion_sort([0, 4, 1234, 4, 1]) @@ -39,23 +40,29 @@ def binary_insertion_sort(collection: list) -> list: n = len(collection) for i in range(1, n): - val = collection[i] + value_to_insert = collection[i] low = 0 high = i - 1 while low <= high: mid = (low + high) // 2 - if val < collection[mid]: + if value_to_insert < 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 + collection[low] = value_to_insert + + 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)) +if __name__ == "__main": + try: + user_input = input("Enter numbers separated by a comma:\n").strip() + unsorted = [int(item) for item in user_input.split(",")] + sorted_list = binary_insertion_sort(unsorted) + print(sorted_list) + except ValueError: + print("Invalid input. Please enter valid integers separated by commas.") From ffd8e66ea8794a666f1afa4fb6294dabf05c7fd0 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 24 Oct 2023 18:49:10 +0000 Subject: [PATCH 2/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- sorts/binary_insertion_sort.py | 1 + 1 file changed, 1 insertion(+) diff --git a/sorts/binary_insertion_sort.py b/sorts/binary_insertion_sort.py index a9b3818be7ab..cb5f34be14f5 100644 --- a/sorts/binary_insertion_sort.py +++ b/sorts/binary_insertion_sort.py @@ -58,6 +58,7 @@ def binary_insertion_sort(collection: list) -> list: return collection + if __name__ == "__main": try: user_input = input("Enter numbers separated by a comma:\n").strip() From dd712eedb919730911b7bb4da4524aa4e24c1ba8 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Tue, 24 Oct 2023 23:46:53 +0200 Subject: [PATCH 3/3] Apply suggestions from code review --- sorts/binary_insertion_sort.py | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/sorts/binary_insertion_sort.py b/sorts/binary_insertion_sort.py index cb5f34be14f5..50653a99e7ce 100644 --- a/sorts/binary_insertion_sort.py +++ b/sorts/binary_insertion_sort.py @@ -50,20 +50,17 @@ def binary_insertion_sort(collection: list) -> list: high = mid - 1 else: low = mid + 1 - for j in range(i, low, -1): collection[j] = collection[j - 1] - collection[low] = value_to_insert - return collection if __name__ == "__main": + user_input = input("Enter numbers separated by a comma:\n").strip() try: - user_input = input("Enter numbers separated by a comma:\n").strip() unsorted = [int(item) for item in user_input.split(",")] - sorted_list = binary_insertion_sort(unsorted) - print(sorted_list) except ValueError: print("Invalid input. Please enter valid integers separated by commas.") + raise + print(f"{binary_insertion_sort(unsorted) = }")