Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
16 changes: 14 additions & 2 deletions 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 Protocol


class Comparable(Protocol):
def __lt__(self, other: object, /) -> 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,7 +19,12 @@ def exchange_sort(numbers: list[int]) -> list[int]:
[-2, 0, 3, 5, 10]
>>> exchange_sort([])
[]
"""

>>> exchange_sort(["d", "a", "c", "b"])
['a', 'b', 'c', 'd']
>>> exchange_sort([2.5, -1.0, 0.0])
[-1.0, 0.0, 2.5]
"""
numbers_length = len(numbers)
for i in range(numbers_length):
for j in range(i + 1, 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 @@ -96,6 +96,7 @@ def test_sort_matches_builtin(sort, case):
bubble_sort_iterative,
bubble_sort_recursive,
insertion_sort,
exchange_sort,
],
ids=lambda f: f.__name__,
)
Expand Down
Loading