Now when there are sorting algorithms already available like Merge Sort and Quick Sort which can sort a large number of elements in quick time and that too efficiently in most of the cases, we still might require to rely on other sorting techniques in some cases. Today we will discuss one such sorting technique called Insertion Sort. Insertion sort is one of the easiest and efficient sorting algorithms that is a comparison based sorting technique. Insertion sort is very advantageous in cases where the number of elements is small and can provide the best case time complexity of O(n)
.
Let’s discuss some of the major advantages and disadvantages of Insertion Sort.
O(n)
.O(n^2)
.The basic working of Insertion sort is fairly simple, what it does is picks an element and places it in its correct position. It does this for every element and finally, we get the sorted array.
Let’s look at the algorithm more deeply.
Let’s have a look at an example to get a clearer picture of the algorithm. The number that is in bold represent the sorted part of the array.
17 13 23 2 7 1 34
import java.util.Arrays;
public class Sorting {
void insertionSort(int arr[]) {
int lengthOfArray = arr.length; // Length of input array
int value; // to store current element
int pos; // to store position of current element
for (int i = 1; i < lengthOfArray; i++) {
pos = i - 1;
value = arr[i];
while (pos >= 0 && arr[pos] > value) {
arr[pos + 1] = arr[pos];
pos = pos - 1;
}
arr[pos + 1] = value;
// Printing value to show array after each iteration
System.out.println("Array after iteration " + i + ":" +
Arrays.toString(arr));
}
}
public static void main(String[] args) {
Sorting sort = new Sorting(); // creating object of class Sorting
int[] arr = {
17,
13,
23,
2,
7,
1,
34
};
sort.insertionSort(arr); // method call
System.out.println("Array after applying insertion sort : " +
Arrays.toString(arr));
}
}
Output
Array after iteration 1: [13, 17, 23, 2, 7, 1, 34]
Array after iteration 2: [13, 17, 23, 2, 7, 1, 34]
Array after iteration 3: [2, 13, 17, 23, 7, 1, 34]
Array after iteration 4: [2, 7, 13, 17, 23, 1, 34]
Array after iteration 5: [1, 2, 7, 13, 17, 23, 34]
Array after iteration 6: [1, 2, 7, 13, 17, 23, 34]
Array after applying insertion sort: [1, 2, 7, 13, 17, 23, 34]
For the sake of understanding let’s take another array as input.
Array: [9, 4, 6, 2, 7, 11, 3, 5]
This is the output that will be generated on passing the above array:
Array after iteration 1: [4, 9, 6, 2, 7, 11, 3, 5]
Array after iteration 2: [4, 6, 9, 2, 7, 11, 3, 5]
Array after iteration 3: [2, 4, 6, 9, 7, 11, 3, 5]
Array after iteration 4: [2, 4, 6, 7, 9, 11, 3, 5]
Array after iteration 5: [2, 4, 6, 7, 9, 11, 3, 5]
Array after iteration 6: [2, 3, 4, 6, 7, 9, 11, 5]
Array after iteration 7: [2, 3, 4, 5, 6, 7, 9, 11]
Array after applying insertion sort: [2, 3, 4, 5, 6, 7, 9, 11]
O(n^2)
O(n)
O(n^2)
O(1)
After studying the above algorithm and going through examples it is clear that insertion sort works best when the elements are nearly sorted or the input size is small. In these two cases, the insertion sort will perform better than most of the sorting algorithms. This is the reason insertion sort is also used as a base case for highly complex sorting algorithms like Merge Sort and Quick Sort.
Help us improve this content by editing this page on GitHub