Skip to content

Commit 411c45b

Browse files
Merge pull request #2895 from Monisha-204/py_doctest
Contains doctests for the selection_sort function to verify correctness across edge cases.
2 parents 8eda96b + e3a1fd2 commit 411c45b

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

sorting_algos.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,44 @@
22

33

44
def selection_sort(arr: list) -> list:
5+
"""Sorts a list in ascending order using the selection sort algorithm.
6+
7+
Args:
8+
arr: List of comparable elements (e.g., integers, strings).
9+
10+
Returns:
11+
The sorted list (in-place modification).
12+
13+
Examples:
14+
>>> selection_sort([])
15+
[]
16+
>>> selection_sort([1])
17+
[1]
18+
>>> selection_sort([1, 2, 3, 4])
19+
[1, 2, 3, 4]
20+
>>> selection_sort([4, 3, 2, 1])
21+
[1, 2, 3, 4]
22+
>>> selection_sort([3, 1, 3, 2])
23+
[1, 2, 3, 3]
24+
>>> selection_sort([5, 5, 5, 5])
25+
[5, 5, 5, 5]
26+
>>> selection_sort([2, 4, 1, 3])
27+
[1, 2, 3, 4]
28+
>>> selection_sort([-1, -3, 0, 2])
29+
[-3, -1, 0, 2]
30+
>>> selection_sort([0, -5, 3, -2, 1])
31+
[-5, -2, 0, 1, 3]
32+
>>> selection_sort(["banana", "apple", "cherry"])
33+
['apple', 'banana', 'cherry']
34+
>>> selection_sort(["Apple", "banana", "Cherry"])
35+
['Apple', 'Cherry', 'banana']
36+
>>> selection_sort([2147483647, -2147483648, 0])
37+
[-2147483648, 0, 2147483647]
38+
"""
39+
540
"""TC : O(n^2)
641
SC : O(1)"""
42+
743
n = len(arr)
844
for i in range(n):
945
for j in range(i + 1, n):

0 commit comments

Comments
 (0)