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])
| 1 | def 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 | |
| 30 | if __name__ == "__main__": |