Usage: Perform insertion sort for a list of integers.
Click the Next button to insert the current element to a sorted sublist.
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
currentElement
A list is filled with random numbers.
1 def insertionSort(lst):
2 for i in range(1, len(lst)):
3 # insert lst[i] into a sorted sublist lst[0..i-1] so that
4 # lst[0..i] is sorted.
5 currentElement = lst[i]
6 k = i - 1
7 while k >= 0 and lst[k] > currentElement:
8 lst[k + 1] = lst[k]
10 k -= 1
10
11 # Insert the current element into lst[k+1]
12 lst[k + 1] = currentElement