MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / selection_sort

Function selection_sort

sorts/selection_sort.py:1–27  ·  view source on GitHub ↗

Sorts a list in ascending order using the selection sort algorithm. :param collection: A list of integers to be sorted. :return: The sorted list. Examples: >>> selection_sort([0, 5, 3, 2, 2]) [0, 2, 2, 3, 5] >>> selection_sort([]) [] >>> selection_sort([-2, -

(collection: list[int])

Source from the content-addressed store, hash-verified

1def selection_sort(collection: list[int]) -> list[int]:
2 """
3 Sorts a list in ascending order using the selection sort algorithm.
4
5 :param collection: A list of integers to be sorted.
6 :return: The sorted list.
7
8 Examples:
9 >>> selection_sort([0, 5, 3, 2, 2])
10 [0, 2, 2, 3, 5]
11
12 >>> selection_sort([])
13 []
14
15 >>> selection_sort([-2, -5, -45])
16 [-45, -5, -2]
17 """
18
19 length = len(collection)
20 for i in range(length - 1):
21 min_index = i
22 for k in range(i + 1, length):
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]
27 return collection
28
29
30if __name__ == "__main__":

Callers 1

selection_sort.pyFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected