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

Function selection_sort

sorts/selection_sort.py:15–42  ·  view source on GitHub ↗

Pure implementation of the selection sort algorithm in Python :param collection: some mutable ordered collection with heterogeneous comparable items inside :return: the same collection ordered by ascending Examples: >>> selection_sort([0, 5, 3, 2, 2]) [0, 2, 2, 3, 5] >

(collection)

Source from the content-addressed store, hash-verified

13
14
15def selection_sort(collection):
16 """Pure implementation of the selection sort algorithm in Python
17 :param collection: some mutable ordered collection with heterogeneous
18 comparable items inside
19 :return: the same collection ordered by ascending
20
21
22 Examples:
23 >>> selection_sort([0, 5, 3, 2, 2])
24 [0, 2, 2, 3, 5]
25
26 >>> selection_sort([])
27 []
28
29 >>> selection_sort([-2, -5, -45])
30 [-45, -5, -2]
31 """
32
33 length = len(collection)
34 for i in range(length - 1):
35 least = i
36 for k in range(i + 1, length):
37 if collection[k] < collection[least]:
38 least = k
39 collection[least], collection[i] = (
40 collection[i], collection[least]
41 )
42 return collection
43
44
45if __name__ == '__main__':

Callers 1

selection_sort.pyFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected