From cc84768a33a1e4e97149ca0d3e9f33626a2538ce Mon Sep 17 00:00:00 2001 From: Gauri Date: Sun, 16 Nov 2025 18:05:48 +0530 Subject: [PATCH 1/3] 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/3] [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 From 53c2134117b6e4c2a99b38575a39e9e3620369db Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Thu, 24 Sep 2026 22:22:55 +0200 Subject: [PATCH 3/3] Refactor shell_sort to use Comparable protocol Refactor shell_sort function to use a Comparable protocol for type hinting. Update sorting logic to improve clarity. --- sorts/shell_sort.py | 43 ++++++++++++++++++++++++++++++++++--------- 1 file changed, 34 insertions(+), 9 deletions(-) diff --git a/sorts/shell_sort.py b/sorts/shell_sort.py index 46c0f21ff4fa..be6d2ff3de8a 100644 --- a/sorts/shell_sort.py +++ b/sorts/shell_sort.py @@ -1,31 +1,56 @@ """ +https://en.wikipedia.org/wiki/Shellsort https://en.wikipedia.org/wiki/Shellsort#Pseudocode """ +from typing import Any, Protocol + + +class Comparable(Protocol): + def __lt__(self, other: Any, /) -> bool: ... -def shell_sort(collection: list[int]) -> list[int]: - """ - Sort a list of integers using the Shell Sort algorithm. - Reference: - https://en.wikipedia.org/wiki/Shellsort +def shell_sort[T: Comparable](collection: list[T]) -> list[T]: + """Pure implementation of shell sort algorithm in Python. + :param collection: Some mutable ordered collection with heterogeneous + comparable items inside + :return: the same collection ordered in ascending order + + Examples: >>> shell_sort([0, 5, 3, 2, 2]) [0, 2, 2, 3, 5] >>> shell_sort([]) [] >>> shell_sort([-2, -5, -45]) [-45, -5, -2] + >>> shell_sort(["c", "a", "b"]) + ['a', 'b', 'c'] + >>> shell_sort([2.5, -1.0, 0.0]) + [-1.0, 0.0, 2.5] + >>> shell_sort([0, 5, 3, 2, 2]) == sorted([0, 5, 3, 2, 2]) + True + >>> shell_sort(["c", "a", "b"]) == sorted(["c", "a", "b"]) + True """ + # 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: + while j >= gap and insert_value < collection[j - gap]: collection[j] = collection[j - gap] j -= gap - collection[j] = insert_value - + if j != i: + 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))