Data Structure & Algorithm in Java

  1. Home
  2. Docs
  3. Data Structure & Alg...
  4. Sorting
  5. Insertion Sort

Insertion Sort

  • Insertion sort is a simple sorting algorithm that works similarly to the way you sort playing cards in your hands. The array is virtually split into a sorted and an unsorted part. Values from the unsorted part are picked and placed in the correct position in the sorted part.
  • It is a simple sorting algorithm that builds the final sorted array one item at a time by comparisons. It is much less efficient on large lists than more advanced algorithms such as quicksort, heapsort, or merge sort.
  • This algorithm is one of the simplest algorithms with a simple implementation
  • Basically, Insertion sort is efficient for small data values
  • Insertion sort is adaptive in nature, i.e. it is appropriate for data sets that are already partially sorted.

The simple steps of achieving the insertion sort are listed as follows :

Step 1 – If the element is the first element, assume that it is already sorted. Return 1.

Step2 – Pick the next element, and store it separately in a Key.

Step3 – Now, compare the key with all elements in the sorted array.

Step 4 – If the element in the sorted array is smaller than the current element, then move to the next element. Else, shift greater elements in the array towards the right.

Step 5 – Insert the value.

Step 6 – Repeat until the array is sorted.

Let’s take an unsorted array. To understand the insertion sort with an example:

image 59

Initially, the first two elements are compared in insertion sort.

image 60

Here, 31 is greater than 12. That means both elements are already in ascending order. So, for now, 12 is stored in a sorted sub-array.

image 61

Now, move to the next two elements and compare them.

image 62

Here, 25 is smaller than 31. So, 31 is not at correct position. Now, swap 31 with 25. Along with swapping, insertion sort will also check it with all elements in the sorted array.

image 63

Now, two elements in the sorted array are 12 and 25. Move forward to the next elements that are 31 and 8.

Both 31 and 8 are not sorted. So, swap them.

image 64

After swapping, elements 25 and 8 are unsorted. So swap them.

image 65

Now, elements 12 and 8 are unsorted. So swap them.

image 66

Now ,the next elements that are 32 and 17are unsorted. So swap them.

image 67

After swapping again array becomes unsorted. So, repeating the above process until the array becomes sorted.

So, the final sorted array is:

Insertion Sort Algorithm
public class InsertionSort {
    public static void main(String[] args) {
        int[] arr = {12, 11, 13, 5, 6};
        System.out.println("Original array:");
        System.out.println("Sorted array:");

    public static void insertionSort(int[] arr) {
        int n = arr.length;
        for (int i = 1; i < n; ++i) {
            int key = arr[i];
            int j = i - 1;

            /* Move elements of arr[0..i-1], that are
               greater than key, to one position ahead
               of their current position */
            while (j >= 0 && arr[j] > key) {
                arr[j + 1] = arr[j];
                j = j - 1;
            arr[j + 1] = key;

    public static void printArray(int[] arr) {
        for (int i = 0; i < arr.length; ++i) {
            System.out.print(arr[i] + " ");


Original array:
12 11 13 5 6 
Sorted array:
5 6 11 12 13 
  • The worst-case time complexity of the Insertion sort is O(N^2)
  • The average case time complexity of the Insertion sort is O(N^2)
  • The time complexity of the best case is O(N).

The auxiliary space complexity of Insertion Sort is O(1)

How can we help?

Leave a Reply

Your email address will not be published. Required fields are marked *