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)
| 13 | |
| 14 | |
| 15 | def 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 | |
| 45 | if __name__ == '__main__': |