Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion sorts/exchange_sort.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
def exchange_sort(numbers: list[int]) -> list[int]:
from typing import Any, Protocol


class Comparable(Protocol):
def __lt__(self, other: Any, /) -> bool: ...


def exchange_sort[T: Comparable](numbers: list[T]) -> list[T]:
"""
Uses exchange sort to sort a list of numbers.
Source: https://en.wikipedia.org/wiki/Sorting_algorithm#Exchange_sort
Expand All @@ -12,6 +19,14 @@ def exchange_sort(numbers: list[int]) -> list[int]:
[-2, 0, 3, 5, 10]
>>> exchange_sort([])
[]
>>> exchange_sort(["c", "a", "b"])
['a', 'b', 'c']
>>> exchange_sort([2.5, -1.0, 0.0])
[-1.0, 0.0, 2.5]
>>> exchange_sort([1, "a"]) # doctest: +IGNORE_EXCEPTION_DETAIL
Comment thread
cclauss marked this conversation as resolved.
Outdated
Traceback (most recent call last):
...
TypeError: '<' not supported between instances of 'str' and 'int'
"""
numbers_length = len(numbers)
for i in range(numbers_length):
Expand Down
1 change: 1 addition & 0 deletions tests/test_sorts.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ def test_sort_matches_builtin(sort, case) -> None:
circle_sort,
cocktail_shaker_sort,
comb_sort,
exchange_sort,
gnome_sort,
insertion_sort,
merge_sort,
Expand Down
Loading