Skip to content

Commit 9d04ef1

Browse files
sorts: make odd_even_sort support comparable items
- make odd_even_sort generic over comparable item types - rewrite the two > comparisons as < so the algorithm only relies on __lt__, which is what the Comparable protocol guarantees - add non-integer doctest coverage (str, float) and a TypeError doctest for mixed non-comparable items - add odd_even_sort to test_sort_rejects_non_comparable_items - fix the docstring :param: name, which did not match the actual argument Ref #15234
1 parent f0391e0 commit 9d04ef1

2 files changed

Lines changed: 19 additions & 4 deletions

File tree

sorts/odd_even_sort.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,22 @@
44
https://en.wikipedia.org/wiki/Odd%E2%80%93even_sort
55
"""
66

7+
from typing import Protocol
78

8-
def odd_even_sort(input_list: list) -> list:
9+
10+
class Comparable(Protocol):
11+
def __lt__(self, other: object, /) -> bool: ...
12+
13+
14+
def odd_even_sort[T: Comparable](input_list: list[T]) -> list[T]:
915
"""
1016
Sort input with odd even sort.
1117
1218
This algorithm uses the same idea of bubblesort,
1319
but by first dividing in two phase (odd and even).
1420
Originally developed for use on parallel processors
1521
with local interconnections.
16-
:param collection: mutable ordered sequence of elements
22+
:param input_list: mutable ordered sequence of comparable elements
1723
:return: same collection in ascending order
1824
Examples:
1925
>>> odd_even_sort([5 , 4 ,3 ,2 ,1])
@@ -24,18 +30,26 @@ def odd_even_sort(input_list: list) -> list:
2430
[-10, -1, 2, 10]
2531
>>> odd_even_sort([1 ,2 ,3 ,4])
2632
[1, 2, 3, 4]
33+
>>> odd_even_sort(["c", "a", "b"])
34+
['a', 'b', 'c']
35+
>>> odd_even_sort([2.5, -1.0, 0.0])
36+
[-1.0, 0.0, 2.5]
37+
>>> odd_even_sort([1, "a"]) # doctest: +ELLIPSIS
38+
Traceback (most recent call last):
39+
...
40+
TypeError: ...
2741
"""
2842
is_sorted = False
2943
while is_sorted is False: # Until all the indices are traversed keep looping
3044
is_sorted = True
3145
for i in range(0, len(input_list) - 1, 2): # iterating over all even indices
32-
if input_list[i] > input_list[i + 1]:
46+
if input_list[i + 1] < input_list[i]:
3347
input_list[i], input_list[i + 1] = input_list[i + 1], input_list[i]
3448
# swapping if elements not in order
3549
is_sorted = False
3650

3751
for i in range(1, len(input_list) - 1, 2): # iterating over all odd indices
38-
if input_list[i] > input_list[i + 1]:
52+
if input_list[i + 1] < input_list[i]:
3953
input_list[i], input_list[i + 1] = input_list[i + 1], input_list[i]
4054
# swapping if elements not in order
4155
is_sorted = False

tests/test_sorts.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ def test_sort_matches_builtin(sort, case) -> None:
121121
gnome_sort,
122122
insertion_sort,
123123
merge_sort,
124+
odd_even_sort,
124125
selection_sort,
125126
],
126127
ids=lambda f: f.__name__,

0 commit comments

Comments
 (0)