1
- """
2
- This is a pure Python implementation of the selection sort algorithm
3
-
4
- For doctests run following command:
5
- python -m doctest -v selection_sort.py
6
- or
7
- python3 -m doctest -v selection_sort.py
8
-
9
- For manual testing run:
10
- python selection_sort.py
11
- """
12
-
13
-
14
1
def selection_sort (collection : list [int ]) -> list [int ]:
15
- """Pure implementation of the selection sort algorithm in Python
16
- :param collection: some mutable ordered collection with heterogeneous
17
- comparable items inside
18
- :return: the same collection ordered by ascending
2
+ """
3
+ Sorts a list in ascending order using the selection sort algorithm.
19
4
5
+ :param collection: A list of integers to be sorted.
6
+ :return: The sorted list.
20
7
21
8
Examples:
22
9
>>> selection_sort([0, 5, 3, 2, 2])
@@ -31,16 +18,17 @@ def selection_sort(collection: list[int]) -> list[int]:
31
18
32
19
length = len (collection )
33
20
for i in range (length - 1 ):
34
- least = i
21
+ min_index = i
35
22
for k in range (i + 1 , length ):
36
- if collection [k ] < collection [least ]:
37
- least = k
38
- if least != i :
39
- collection [least ], collection [i ] = ( collection [i ], collection [least ])
23
+ if collection [k ] < collection [min_index ]:
24
+ min_index = k
25
+ if min_index != i :
26
+ collection [i ], collection [min_index ] = collection [min_index ], collection [i ]
40
27
return collection
41
28
42
29
43
30
if __name__ == "__main__" :
44
31
user_input = input ("Enter numbers separated by a comma:\n " ).strip ()
45
32
unsorted = [int (item ) for item in user_input .split ("," )]
46
- print (selection_sort (unsorted ))
33
+ sorted_list = selection_sort (unsorted )
34
+ print ("Sorted List:" , sorted_list )
0 commit comments