Average Performance: О(n2)
Best Case Performance:О(n)
Worst Case Performance: О(n2)
Classification: stable, in-place, online
Insertion sort is a simple sorting algorithm: a comparison sort in which the sorted array (or list) is built one entry at a time. It is much less efficient on large lists than more advanced algorithms such as quicksort, heapsort, or merge sort. However, insertion sort provides several advantages: [from Wiki]Best Case Performance:О(n)
Worst Case Performance: О(n2)
Classification: stable, in-place, online
- Simple implementation
- Efficient for (quite) small data sets
- Adaptive (i.e., efficient) for data sets that are already substantially sorted: the time complexity is O(n + d), where d is the number of inversions
- More efficient in practice than most other simple quadratic (i.e., O(n2)) algorithms such as selection sort or bubble sort; the best case (nearly sorted input) is O(n)
- Stable; i.e., does not change the relative order of elements with equal keys
- In-place; i.e., only requires a constant amount O(1) of additional memory space
- Online; i.e., can sort a list as it receives it
Below source code is written in Java, but you can use the function insertionSort() without any modification. Just write your main() function with input array and call insertionSort() with two parameters: array and array size.
public class CodeTest {
static int counter1 =0;
static int counter2 =0;
public static void main(String[] args) {
int input[] = {94,34,53,21,100,102, 21, 53,45, 456, 3, 32,1,5,4};
System.out.print("Before Sorting: ");
for(int i = 0; i<input.length;i++)
System.out.print(input[i] + " ");
insertionSort(input, input.length);
System.out.print("\nAfter Sorting: ");
for(int i = 0; i<input.length;i++)
System.out.print(input[i] + " ");
System.out.println("\n Outerfor loop: " + counter1 + " times, inner while loop: " + counter2 + " times");
}
public static void insertionSort(int input[], int length) {
int i, j, key;
for(i = 1; i<length;i++) {
counter1++;
key = input[i];
j = i - 1;
while (j>=0 && input[j] > key) {
counter2++;
input[j+1] = input[j];
j--;
}
input[j+1] = key;
}
}
}
static int counter1 =0;
static int counter2 =0;
public static void main(String[] args) {
int input[] = {94,34,53,21,100,102, 21, 53,45, 456, 3, 32,1,5,4};
System.out.print("Before Sorting: ");
for(int i = 0; i<input.length;i++)
System.out.print(input[i] + " ");
insertionSort(input, input.length);
System.out.print("\nAfter Sorting: ");
for(int i = 0; i<input.length;i++)
System.out.print(input[i] + " ");
System.out.println("\n Outerfor loop: " + counter1 + " times, inner while loop: " + counter2 + " times");
}
public static void insertionSort(int input[], int length) {
int i, j, key;
for(i = 1; i<length;i++) {
counter1++;
key = input[i];
j = i - 1;
while (j>=0 && input[j] > key) {
counter2++;
input[j+1] = input[j];
j--;
}
input[j+1] = key;
}
}
}
Insertion Sort in Java/C of an Integer Array