From d49a112a1c31824d121507e04649c734a5ca642d Mon Sep 17 00:00:00 2001 From: Orji Patricia <81320550+otrisha@users.noreply.github.com> Date: Sat, 12 Sep 2026 02:06:21 +0100 Subject: [PATCH 1/2] sorts: make comb sort generic for comparable items --- sorts/comb_sort.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/sorts/comb_sort.py b/sorts/comb_sort.py index 72caeb9c7350..cfa6539aefac 100644 --- a/sorts/comb_sort.py +++ b/sorts/comb_sort.py @@ -18,8 +18,14 @@ 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.0]) + [-1.0, 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 From 8b9fbab0400d9789095a31b6ed65ecf035202709 Mon Sep 17 00:00:00 2001 From: Orji Patricia <81320550+otrisha@users.noreply.github.com> Date: Sat, 12 Sep 2026 02:06:34 +0100 Subject: [PATCH 2/2] tests: cover comb sort incomparable inputs --- tests/test_sorts.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_sorts.py b/tests/test_sorts.py index 799ab5979dff..adabc2c7d43a 100644 --- a/tests/test_sorts.py +++ b/tests/test_sorts.py @@ -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,