-
Notifications
You must be signed in to change notification settings - Fork 0
/
algo_selection_sort.py
37 lines (27 loc) · 1.24 KB
/
algo_selection_sort.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
"""
This implementation of Selection Sort:
* Avoids processing the sorted portion of the list by having loop + 1 as the start of
the inner loop range.
* Avoids boolean flag for early termination optimization since it only works for
lists that are already initially sorted, and not for lists that are sorted during
the process, so the overhead is avoided.
Time Complexity: Best O(n^2) | Avg O(n^2) | Worst O(n^2)
Space Complexity: Total O(n) | Aux O(1)
https://en.wikipedia.org/wiki/selection_sort
https://www.programiz.com/dsa/selection-sort
https://rosettacode.org/wiki/Sorting_algorithms/Selection_sort
"""
from sort_types import IntList, Algorithm
def selection_sort(nums: IntList) -> IntList:
"""Sorts the given integer list in ascending order."""
for loop in range(len(nums)):
min_val_idx = loop
# Loop through the unsorted portion and find the min value
for idx in range(loop + 1, len(nums)):
if nums[idx] < nums[min_val_idx]:
min_val_idx = idx
# Move min value of unsorted portion to the new end of the sorted portion
nums[loop], nums[min_val_idx] = nums[min_val_idx], nums[loop]
return nums
name = "selection sort"
algorithm = Algorithm(selection_sort, name)