Skip to content

Commit 9e1cf59

Browse files
hzagamingcclauss
andauthored
sorts: support comparable items in circle_sort (#15244)
* sorts: support comparable items in circle_sort * style: format circle_sort * Apply batched suggestions from code review Co-authored-by: Christian Clauss <cclauss@me.com> --------- Co-authored-by: Christian Clauss <cclauss@me.com>
1 parent 9e84598 commit 9e1cf59

2 files changed

Lines changed: 25 additions & 5 deletions

File tree

‎sorts/circle_sort.py‎

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,17 @@
88
python3 circle_sort.py
99
"""
1010

11+
from collections.abc import MutableSequence
12+
from typing import Any, Protocol
1113

12-
def circle_sort(collection: list) -> list:
14+
15+
class Comparable(Protocol):
16+
def __lt__(self, other: Any, /) -> bool: ...
17+
18+
19+
def circle_sort[T: Comparable](
20+
collection: MutableSequence[T],
21+
) -> MutableSequence[T]:
1322
"""A pure Python implementation of circle sort algorithm
1423
1524
:param collection: a mutable collection of comparable items in any order
@@ -22,6 +31,14 @@ def circle_sort(collection: list) -> list:
2231
[]
2332
>>> circle_sort([-2, 5, 0, -45])
2433
[-45, -2, 0, 5]
34+
>>> circle_sort(["d", "a", "c", "b"])
35+
['a', 'b', 'c', 'd']
36+
>>> circle_sort([2.5, -1.0, 0.0])
37+
[-1.0, 0.0, 2.5]
38+
>>> circle_sort([1, "a"])
39+
Traceback (most recent call last):
40+
...
41+
TypeError: '<' not supported between instances of 'str' and 'int'
2542
>>> collections = ([], [0, 5, 3, 2, 2], [-2, 5, 0, -45])
2643
>>> all(sorted(collection) == circle_sort(collection) for collection in collections)
2744
True
@@ -30,10 +47,10 @@ def circle_sort(collection: list) -> list:
3047
if len(collection) < 2:
3148
return collection
3249

33-
def circle_sort_util(collection: list, low: int, high: int) -> bool:
50+
def circle_sort_util(collection: MutableSequence[T], low: int, high: int) -> bool:
3451
"""
3552
>>> arr = [5,4,3,2,1]
36-
>>> circle_sort_util(lst, 0, 2)
53+
>>> circle_sort_util(arr, 0, 2)
3754
True
3855
>>> arr
3956
[3, 4, 5, 2, 1]
@@ -48,7 +65,7 @@ def circle_sort_util(collection: list, low: int, high: int) -> bool:
4865
right = high
4966

5067
while left < right:
51-
if collection[left] > collection[right]:
68+
if collection[right] < collection[left]:
5269
collection[left], collection[right] = (
5370
collection[right],
5471
collection[left],
@@ -58,7 +75,7 @@ def circle_sort_util(collection: list, low: int, high: int) -> bool:
5875
left += 1
5976
right -= 1
6077

61-
if left == right and collection[left] > collection[right + 1]:
78+
if left == right and collection[right + 1] < collection[left]:
6279
collection[left], collection[right + 1] = (
6380
collection[right + 1],
6481
collection[left],

‎tests/test_sorts.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,10 @@ def test_sort_matches_builtin(sort, case):
9595
binary_insertion_sort,
9696
bubble_sort_iterative,
9797
bubble_sort_recursive,
98+
circle_sort,
9899
insertion_sort,
100+
merge_sort,
101+
selection_sort,
99102
],
100103
ids=lambda f: f.__name__,
101104
)

0 commit comments

Comments
 (0)