From 48b9a8cb5700f31986df4af363770ad9d538ae67 Mon Sep 17 00:00:00 2001 From: Labib-Bin-Salam Date: Tue, 28 Apr 2026 23:47:26 +0100 Subject: [PATCH 1/4] Refine Shell Sort implementation and documentation --- sorts/shell_sort.py | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/sorts/shell_sort.py b/sorts/shell_sort.py index b65609c974b7..1bf1c63d9008 100644 --- a/sorts/shell_sort.py +++ b/sorts/shell_sort.py @@ -1,13 +1,15 @@ -""" +"""Shell sort implementation. + +Reference: https://en.wikipedia.org/wiki/Shellsort#Pseudocode """ def shell_sort(collection: list[int]) -> list[int]: - """Pure implementation of shell sort algorithm in Python - :param collection: Some mutable ordered collection with heterogeneous - comparable items inside - :return: the same collection ordered by ascending + """Sort a list of integers in ascending order using shell sort. + + :param collection: A list of integers + :return: The same list sorted in ascending order >>> shell_sort([0, 5, 3, 2, 2]) [0, 2, 2, 3, 5] @@ -16,18 +18,19 @@ def shell_sort(collection: list[int]) -> list[int]: >>> shell_sort([-2, -5, -45]) [-45, -5, -2] """ - # Marcin Ciura's gap sequence - gaps = [701, 301, 132, 57, 23, 10, 4, 1] + for gap in gaps: for i in range(gap, len(collection)): insert_value = collection[i] j = i + while j >= gap and collection[j - gap] > insert_value: collection[j] = collection[j - gap] j -= gap - if j != i: - collection[j] = insert_value + + collection[j] = insert_value + return collection @@ -37,4 +40,4 @@ def shell_sort(collection: list[int]) -> list[int]: testmod() user_input = input("Enter numbers separated by a comma:\n").strip() unsorted = [int(item) for item in user_input.split(",")] - print(shell_sort(unsorted)) + print(shell_sort(unsorted)) \ No newline at end of file From 400546fef0d60419c7e7f5ffd2147875db7121b8 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 28 Apr 2026 22:51:32 +0000 Subject: [PATCH 2/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- sorts/shell_sort.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sorts/shell_sort.py b/sorts/shell_sort.py index 1bf1c63d9008..e3805c5ac6f6 100644 --- a/sorts/shell_sort.py +++ b/sorts/shell_sort.py @@ -40,4 +40,4 @@ def shell_sort(collection: list[int]) -> list[int]: testmod() user_input = input("Enter numbers separated by a comma:\n").strip() unsorted = [int(item) for item in user_input.split(",")] - print(shell_sort(unsorted)) \ No newline at end of file + print(shell_sort(unsorted)) From ff133d60bedc94f26cc031da9652d9651d41b8f4 Mon Sep 17 00:00:00 2001 From: Labib-Bin-Salam Date: Tue, 28 Apr 2026 23:56:34 +0100 Subject: [PATCH 3/4] Refine Shell Sort implementation and documentation --- sorts/shell_sort.py | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/sorts/shell_sort.py b/sorts/shell_sort.py index 1bf1c63d9008..3159b11659c1 100644 --- a/sorts/shell_sort.py +++ b/sorts/shell_sort.py @@ -31,13 +31,4 @@ def shell_sort(collection: list[int]) -> list[int]: collection[j] = insert_value - return collection - - -if __name__ == "__main__": - from doctest import testmod - - testmod() - user_input = input("Enter numbers separated by a comma:\n").strip() - unsorted = [int(item) for item in user_input.split(",")] - print(shell_sort(unsorted)) \ No newline at end of file + return collection \ No newline at end of file From 6bd17df2d661751ee13cf014322f7e3e058f3cd3 Mon Sep 17 00:00:00 2001 From: Labib-Bin-Salam Date: Wed, 29 Apr 2026 00:07:03 +0100 Subject: [PATCH 4/4] Refine Shell Sort implementation and documentation --- sorts/shell_sort.py | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/sorts/shell_sort.py b/sorts/shell_sort.py index 3159b11659c1..68bdc8fc173b 100644 --- a/sorts/shell_sort.py +++ b/sorts/shell_sort.py @@ -1,4 +1,5 @@ -"""Shell sort implementation. +""" +Shell sort implementation. Reference: https://en.wikipedia.org/wiki/Shellsort#Pseudocode @@ -6,9 +7,10 @@ def shell_sort(collection: list[int]) -> list[int]: - """Sort a list of integers in ascending order using shell sort. + """ + Sort a list of integers in ascending order using Shell sort. - :param collection: A list of integers + :param collection: A list of integers to sort :return: The same list sorted in ascending order >>> shell_sort([0, 5, 3, 2, 2]) @@ -17,18 +19,24 @@ def shell_sort(collection: list[int]) -> list[int]: [] >>> shell_sort([-2, -5, -45]) [-45, -5, -2] + >>> shell_sort([3, "a"]) # doctest: +IGNORE_EXCEPTION_DETAIL + Traceback (most recent call last): + TypeError """ gaps = [701, 301, 132, 57, 23, 10, 4, 1] for gap in gaps: - for i in range(gap, len(collection)): - insert_value = collection[i] - j = i - - while j >= gap and collection[j - gap] > insert_value: - collection[j] = collection[j - gap] - j -= gap - - collection[j] = insert_value + for index in range(gap, len(collection)): + insert_value = collection[index] + current_index = index + + while ( + current_index >= gap + and collection[current_index - gap] > insert_value + ): + collection[current_index] = collection[current_index - gap] + current_index -= gap + + collection[current_index] = insert_value return collection \ No newline at end of file