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
↓
3
41
42
75
33
45
45
45
45
45
45
45
45
45
45
45
45
45
45
45
i
currentElement
A new random list is created
1 publicstaticvoid insertionSort(int[] list) {
2 for (int i = 1; i < list.length; i++) {
3 /** insert list[i] into a sorted sublist list[0..i-1] so that
4 list[0..i] is sorted. */
5 int currentElement = list[i];
6 int k;
7 for (k = i - 1; k >= 0 && list[k] > currentElement; k--) {
8 list[k + 1] = list[k];
9 }
10
11 // Insert the current element into list[k+1]
12 list[k + 1] = currentElement;
13 }
14 }