@@ -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
3036if __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