| 1 | def selection_sort(nums): |
| 2 | # This value of i corresponds to how many values were sorted |
| 3 | for i in range(len(nums)): |
| 4 | # We assume that the first item of the unsorted segment is the smallest |
| 5 | lowest_value_index = i |
| 6 | # This loop iterates over the unsorted items |
| 7 | for j in range(i + 1, len(nums)): |
| 8 | if nums[j] < nums[lowest_value_index]: |
| 9 | lowest_value_index = j |
| 10 | # Swap values of the lowest unsorted element with the first unsorted |
| 11 | # element |
| 12 | nums[i], nums[lowest_value_index] = nums[lowest_value_index], nums[i] |
| 13 | |
| 14 | |
| 15 | # Verify it works |