Skip to content
Open
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
35 changes: 33 additions & 2 deletions sorts/selection_sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,15 @@ def selection_sort(collection: list[int]) -> list[int]:
"""
Sorts a list in ascending order using the selection sort algorithm.

:param collection: A list of integers to be sorted.
:return: The sorted list.
Selection sort divides the input list into a sorted and unsorted region.
It repeatedly finds the minimum element from the unsorted region and
places it at the end of the sorted region.

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.

Examples:
>>> selection_sort([0, 5, 3, 2, 2])
Expand All @@ -14,6 +21,30 @@ def selection_sort(collection: list[int]) -> list[int]:

>>> selection_sort([-2, -5, -45])
[-45, -5, -2]

>>> selection_sort([1])
[1]

>>> selection_sort([5, 4, 3, 2, 1])
[1, 2, 3, 4, 5]

>>> selection_sort([1, 2, 3, 4, 5])
[1, 2, 3, 4, 5]

>>> selection_sort([3, 3, 3, 3])
[3, 3, 3, 3]

>>> selection_sort([0])
[0]

>>> selection_sort([2, -3, 0, 5, -1])
[-3, -1, 0, 2, 5]

>>> selection_sort([0, 5, 3, 2, 2]) == sorted([0, 5, 3, 2, 2])
True

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

length = len(collection)
Expand Down
Loading