Skip to content

Commit 74245fe

Browse files
authored
Enhance docstring for selection_sort function
Updated the docstring to provide detailed explanation of the selection sort algorithm, including time and space complexity.
1 parent a051ab5 commit 74245fe

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

sorts/selection_sort.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,16 @@ def selection_sort(collection: list[int]) -> list[int]:
22
"""
33
Sorts a list in ascending order using the selection sort algorithm.
44
5+
Selection sort works by repeatedly finding the minimum element from the
6+
unsorted portion of the list and placing it at the beginning.
7+
58
:param collection: A list of integers to be sorted.
69
:return: The sorted list.
710
11+
Time Complexity: O(n^2) - where n is the number of elements.
12+
The algorithm always makes n*(n-1)/2 comparisons regardless of input.
13+
Space Complexity: O(1) - sorts in place using only a constant amount of extra space.
14+
815
Examples:
916
>>> selection_sort([0, 5, 3, 2, 2])
1017
[0, 2, 2, 3, 5]
@@ -15,7 +22,6 @@ def selection_sort(collection: list[int]) -> list[int]:
1522
>>> selection_sort([-2, -5, -45])
1623
[-45, -5, -2]
1724
"""
18-
1925
length = len(collection)
2026
for i in range(length - 1):
2127
min_index = i
@@ -28,7 +34,9 @@ def selection_sort(collection: list[int]) -> list[int]:
2834

2935

3036
if __name__ == "__main__":
37+
import doctest
38+
39+
doctest.testmod()
3140
user_input = input("Enter numbers separated by a comma:\n").strip()
3241
unsorted = [int(item) for item in user_input.split(",")]
33-
sorted_list = selection_sort(unsorted)
34-
print("Sorted List:", sorted_list)
42+
print(selection_sort(unsorted))

0 commit comments

Comments
 (0)