sorts: make comb_sort generic for comparable items (#15288)

* sorts: make comb sort generic for comparable items

* tests: cover comb sort incomparable inputs

* Update sorts/comb_sort.py

* Apply suggestion from @cclauss

* Apply suggestion from @cclauss

---------

Co-authored-by: Christian Clauss <cclauss@me.com>
This commit is contained in:
Orji Patricia
2026-09-12 07:53:55 +02:00
committed by GitHub
co-authored by Christian Clauss
parent 3e34e8ef75
commit d502013438
2 changed files with 17 additions and 2 deletions
+16 -2
View File
@@ -18,8 +18,14 @@ For manual testing run:
python comb_sort.py
"""
from typing import Any, Protocol
def comb_sort(data: list) -> list:
class Comparable(Protocol):
def __lt__(self, other: Any, /) -> bool: ...
def comb_sort[T: Comparable](data: list[T]) -> list[T]:
"""Pure implementation of comb sort algorithm in Python
:param data: mutable collection with comparable items
:return: the same collection in ascending order
@@ -32,6 +38,14 @@ def comb_sort(data: list) -> list:
[-15, -7, 0, 2, 3, 8, 45, 99]
>>> comb_sort([2, 0, 3, 4, 5, 6, 1])
[0, 1, 2, 3, 4, 5, 6]
>>> comb_sort(["c", "a", "b"])
['a', 'b', 'c']
>>> comb_sort([2.5, -1, 0.0])
[-1, 0.0, 2.5]
>>> comb_sort([1, "a"])
Traceback (most recent call last):
...
TypeError: '<' not supported between instances of 'str' and 'int'
"""
shrink_factor = 1.3
gap = len(data)
@@ -47,7 +61,7 @@ def comb_sort(data: list) -> list:
index = 0
while index + gap < len(data):
if data[index] > data[index + gap]:
if data[index + gap] < data[index]:
# Swap values
data[index], data[index + gap] = data[index + gap], data[index]
completed = False
+1
View File
@@ -116,6 +116,7 @@ def test_sort_matches_builtin(sort, case):
bubble_sort_recursive,
circle_sort,
cocktail_shaker_sort,
comb_sort,
gnome_sort,
insertion_sort,
merge_sort,