From cc84768a33a1e4e97149ca0d3e9f33626a2538ce Mon Sep 17 00:00:00 2001 From: Gauri Date: Sun, 16 Nov 2025 18:05:48 +0530 Subject: [PATCH 1/2] Add Shell Sort algorithm implementation --- sorts/shell_sort.py | 26 ++++++++------------------ 1 file changed, 8 insertions(+), 18 deletions(-) diff --git a/sorts/shell_sort.py b/sorts/shell_sort.py index b65609c974b7..dfc2de65761f 100644 --- a/sorts/shell_sort.py +++ b/sorts/shell_sort.py @@ -2,12 +2,12 @@ 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 using the Shell Sort algorithm. + + Reference: + https://en.wikipedia.org/wiki/Shellsort >>> shell_sort([0, 5, 3, 2, 2]) [0, 2, 2, 3, 5] @@ -16,9 +16,8 @@ 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] @@ -26,15 +25,6 @@ def shell_sort(collection: list[int]) -> list[int]: while j >= gap and collection[j - gap] > insert_value: collection[j] = collection[j - gap] j -= gap - if j != i: - collection[j] = insert_value - return collection - - -if __name__ == "__main__": - from doctest import testmod + collection[j] = insert_value - 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)) + return collection \ No newline at end of file From 22770e5fbd39b6fa170f790a92a48f027c1931d0 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 16 Nov 2025 12:39:24 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- sorts/shell_sort.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/sorts/shell_sort.py b/sorts/shell_sort.py index dfc2de65761f..46c0f21ff4fa 100644 --- a/sorts/shell_sort.py +++ b/sorts/shell_sort.py @@ -2,6 +2,7 @@ https://en.wikipedia.org/wiki/Shellsort#Pseudocode """ + def shell_sort(collection: list[int]) -> list[int]: """ Sort a list of integers using the Shell Sort algorithm. @@ -27,4 +28,4 @@ def shell_sort(collection: list[int]) -> list[int]: j -= gap collection[j] = insert_value - return collection \ No newline at end of file + return collection