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 void insertionSort(int list[], int arraySize)
2 {
3 for (int i = 1; i < arraySize; i++)
4 {
5 /** insert list[i] into a sorted sublist list[0..i-1] so that
6 list[0..i] is sorted. */
7 int currentElement = list[i];
8 int k;
9 for (k = i - 1; k >= 0 && list[k] > currentElement; k--)
10 list[k + 1] = list[k];
11
12 // Insert the current element into list[k+1]
13 list[k + 1] = currentElement;
14 }
15 }