Skip to content

Commit 2c171b0

Browse files
authored
docs: add time and space complexity to selection_sort docstring
Added Time Complexity O(n^2) and Space Complexity O(1) documentation to the selection_sort function docstring, improving code documentation for beginners as requested in issue #13948.
1 parent a051ab5 commit 2c171b0

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

sorts/selection_sort.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ def selection_sort(collection: list[int]) -> list[int]:
55
:param collection: A list of integers to be sorted.
66
:return: The sorted list.
77
8+
9+
Time Complexity: O(n^2) - Due to the nested loops, where n is the length
10+
of the collection. The outer loop runs n-1 times, and the inner loop
11+
runs n-i-1 times for each iteration.
12+
Space Complexity: O(1) - Only a constant amount of extra space is used
13+
for variables (length, i, min_index, k).
814
Examples:
915
>>> selection_sort([0, 5, 3, 2, 2])
1016
[0, 2, 2, 3, 5]

0 commit comments

Comments
 (0)