From c204bcac06d267f183f0cf261897ae77db700f1b Mon Sep 17 00:00:00 2001 From: sahil24cu Date: Tue, 15 Sep 2026 00:35:05 +0530 Subject: [PATCH 1/2] Make shell sort support comparable types --- sorts/shell_sort.py | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/sorts/shell_sort.py b/sorts/shell_sort.py index b65609c974b7..91bc317029bb 100644 --- a/sorts/shell_sort.py +++ b/sorts/shell_sort.py @@ -1,9 +1,16 @@ """ https://en.wikipedia.org/wiki/Shellsort#Pseudocode """ +from typing import Any, Protocol, TypeVar -def shell_sort(collection: list[int]) -> list[int]: +class Comparable(Protocol): + def __lt__(self, other: Any, /) -> bool: ... + + +T = TypeVar("T", bound=Comparable) + +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 @@ -15,9 +22,16 @@ def shell_sort(collection: list[int]) -> list[int]: [] >>> shell_sort([-2, -5, -45]) [-45, -5, -2] + >>> shell_sort(["d", "a", "b", "e", "c"]) + ['a', 'b', 'c', 'd', 'e'] + >>> shell_sort([3, 1.5, 2, 0.5]) + [0.5, 1.5, 2, 3] + >>> shell_sort([1, "two", 3]) + Traceback (most recent call last): + ... + TypeError: '>' not supported between instances of 'int' and 'str' """ - # Marcin Ciura's gap sequence - +# 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)): From 2cf5014ef8a7c8c9cf9b52abcaa6b41b52466962 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 14 Sep 2026 19:08:14 +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 | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/sorts/shell_sort.py b/sorts/shell_sort.py index 91bc317029bb..6bf52c56b3bb 100644 --- a/sorts/shell_sort.py +++ b/sorts/shell_sort.py @@ -1,6 +1,7 @@ """ https://en.wikipedia.org/wiki/Shellsort#Pseudocode """ + from typing import Any, Protocol, TypeVar @@ -10,6 +11,7 @@ def __lt__(self, other: Any, /) -> bool: ... T = TypeVar("T", bound=Comparable) + 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 @@ -31,7 +33,7 @@ def shell_sort[T: Comparable](collection: list[T]) -> list[T]: ... TypeError: '>' not supported between instances of 'int' and 'str' """ -# Marcin Ciura's gap sequence + # 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)):