Usage: Perform selection sort for a list of integers. Click the Next button to
find the smallest element (highlighted in red) and swap this element with the first
element (highlighted in orange) in the the unsorted sublist. The elements that are
already sorted are highlighted in light green.
Click the Reset button to start over with a new random list of the specified size (min 3 and max 20). The Custom Input button enables you to enter a custom list.
i: 1
↓
i
currentMin
currentMinIndex
A list is filled with random numbers.
1 def selectionSort(lst):
2 foriinrange(len(lst) - 1):
3 # Find the minimum in the lst[i..len(lst)-1]
4 currentMin = min(lst[i : ])
5 currentMinIndex = i + lst[i: ].index(currentMin)
6
7 # Swap lst[i] with lst[currentMinIndex] if necessary;
8 if currentMinIndex != i:
9 lst[currentMinIndex], lst[i] = lst[i], currentMin