From 62586631e2ccd84dc1a53fca4f2b231a171b2bea Mon Sep 17 00:00:00 2001 From: hzagaming <17358677225@163.com> Date: Wed, 9 Sep 2026 22:57:18 +0800 Subject: [PATCH 1/4] sorts: support comparable items in circle_sort --- sorts/circle_sort.py | 32 +++++++++++++++++++++++++++----- tests/test_sorts.py | 5 +++++ 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/sorts/circle_sort.py b/sorts/circle_sort.py index 271fa1e8d58a..6d24eb391fb3 100644 --- a/sorts/circle_sort.py +++ b/sorts/circle_sort.py @@ -8,8 +8,20 @@ python3 circle_sort.py """ +from collections.abc import MutableSequence +from typing import Any, Protocol, TypeVar -def circle_sort(collection: list) -> list: + +class Comparable(Protocol): + def __lt__(self, other: Any, /) -> bool: ... + + +T = TypeVar("T", bound=Comparable) + + +def circle_sort[T: Comparable]( + collection: MutableSequence[T], +) -> MutableSequence[T]: """A pure Python implementation of circle sort algorithm :param collection: a mutable collection of comparable items in any order @@ -22,6 +34,14 @@ def circle_sort(collection: list) -> list: [] >>> circle_sort([-2, 5, 0, -45]) [-45, -2, 0, 5] + >>> circle_sort(["d", "a", "c", "b"]) + ['a', 'b', 'c', 'd'] + >>> circle_sort([2.5, -1.0, 0.0]) + [-1.0, 0.0, 2.5] + >>> circle_sort([1, "a"]) + Traceback (most recent call last): + ... + TypeError: '<' not supported between instances of 'str' and 'int' >>> collections = ([], [0, 5, 3, 2, 2], [-2, 5, 0, -45]) >>> all(sorted(collection) == circle_sort(collection) for collection in collections) True @@ -30,10 +50,12 @@ def circle_sort(collection: list) -> list: if len(collection) < 2: return collection - def circle_sort_util(collection: list, low: int, high: int) -> bool: + def circle_sort_util( + collection: MutableSequence[T], low: int, high: int + ) -> bool: """ >>> arr = [5,4,3,2,1] - >>> circle_sort_util(lst, 0, 2) + >>> circle_sort_util(arr, 0, 2) True >>> arr [3, 4, 5, 2, 1] @@ -48,7 +70,7 @@ def circle_sort_util(collection: list, low: int, high: int) -> bool: right = high while left < right: - if collection[left] > collection[right]: + if collection[right] < collection[left]: collection[left], collection[right] = ( collection[right], collection[left], @@ -58,7 +80,7 @@ def circle_sort_util(collection: list, low: int, high: int) -> bool: left += 1 right -= 1 - if left == right and collection[left] > collection[right + 1]: + if left == right and collection[right + 1] < collection[left]: collection[left], collection[right + 1] = ( collection[right + 1], collection[left], diff --git a/tests/test_sorts.py b/tests/test_sorts.py index 6de12789dd0a..08d4bfe89814 100644 --- a/tests/test_sorts.py +++ b/tests/test_sorts.py @@ -92,3 +92,8 @@ def test_sort_matches_builtin(sort, case): def test_binary_insertion_sort_rejects_non_comparable_items(): with pytest.raises(TypeError): binary_insertion_sort([1, "a"]) + + +def test_circle_sort_rejects_non_comparable_items(): + with pytest.raises(TypeError): + circle_sort([1, "a"]) From ba96812af8ad021fdf7690a56444a28306e71faa Mon Sep 17 00:00:00 2001 From: hzagaming <17358677225@163.com> Date: Wed, 9 Sep 2026 23:09:07 +0800 Subject: [PATCH 2/4] style: format circle_sort --- sorts/circle_sort.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/sorts/circle_sort.py b/sorts/circle_sort.py index 6d24eb391fb3..1970069f4956 100644 --- a/sorts/circle_sort.py +++ b/sorts/circle_sort.py @@ -50,9 +50,7 @@ def circle_sort[T: Comparable]( if len(collection) < 2: return collection - def circle_sort_util( - collection: MutableSequence[T], low: int, high: int - ) -> bool: + def circle_sort_util(collection: MutableSequence[T], low: int, high: int) -> bool: """ >>> arr = [5,4,3,2,1] >>> circle_sort_util(arr, 0, 2) From 3887a1b4fe31d024d78417753aba1fdeca70d175 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Wed, 9 Sep 2026 23:50:32 +0200 Subject: [PATCH 3/4] Apply batched suggestions from code review Co-authored-by: Christian Clauss --- sorts/circle_sort.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/sorts/circle_sort.py b/sorts/circle_sort.py index 1970069f4956..b3b88cd0ddae 100644 --- a/sorts/circle_sort.py +++ b/sorts/circle_sort.py @@ -9,16 +9,13 @@ """ from collections.abc import MutableSequence -from typing import Any, Protocol, TypeVar +from typing import Any, Protocol class Comparable(Protocol): def __lt__(self, other: Any, /) -> bool: ... -T = TypeVar("T", bound=Comparable) - - def circle_sort[T: Comparable]( collection: MutableSequence[T], ) -> MutableSequence[T]: From fadeb298a8d07e98c13795d8b361add81c7d70cf Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Thu, 10 Sep 2026 00:29:31 +0200 Subject: [PATCH 4/4] Add merge_sort and selection_sort to test suite --- tests/test_sorts.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/test_sorts.py b/tests/test_sorts.py index a7308f4f5329..05c3c747b95a 100644 --- a/tests/test_sorts.py +++ b/tests/test_sorts.py @@ -97,6 +97,8 @@ def test_sort_matches_builtin(sort, case): bubble_sort_recursive, circle_sort, insertion_sort, + merge_sort, + selection_sort, ], ids=lambda f: f.__name__, )