Skip to content
Merged
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
21 changes: 17 additions & 4 deletions sorts/selection_sort.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
def selection_sort(collection: list[int]) -> list[int]:
from collections.abc import MutableSequence
from typing import Any, Protocol


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


def selection_sort[T: Comparable](collection: MutableSequence[T]) -> MutableSequence[T]:
"""
Sorts a list in ascending order using the selection sort algorithm.

Expand All @@ -9,8 +17,8 @@ def selection_sort(collection: list[int]) -> list[int]:
Time Complexity: O(n²) in all cases
Space Complexity: O(1)

:param collection: A list of comparable items to be sorted.
:return: The same list sorted in ascending order.
:param collection: A mutable sequence of comparable items to be sorted.
:return: The same sequence sorted in ascending order.

Examples:
>>> selection_sort([0, 5, 3, 2, 2])
Expand Down Expand Up @@ -45,8 +53,13 @@ def selection_sort(collection: list[int]) -> list[int]:

>>> selection_sort([-2, -5, -45]) == sorted([-2, -5, -45])
True
"""

>>> selection_sort(["d", "a", "c", "b"])
['a', 'b', 'c', 'd']

>>> selection_sort([3.2, 1.1, 2.4, 0.5])
[0.5, 1.1, 2.4, 3.2]
"""
length = len(collection)
for i in range(length - 1):
min_index = i
Expand Down
Loading