DSA using Java - Insertion Sort



Overview

Insertion sort is a simple sorting algorithm. This sorting algorithm is in-place comparison based algorithm in which a item is taken, its suitable location is searched and this item is inserted at that particular location growing the sorted list. This algorithm is not suitable for large data sets as its average and worst case complexity are of O(n2) where n are no. of items.

Pseudocode

procedure insertionSort( A : array of items )
   int holePosition
   int valueToInsert
   for i = 1 to length(A) inclusive do:
      /* select value to be inserted */
      valueToInsert = A[i]
      holePosition = i
      /*locate hole position for the element to be inserted */
      while holePosition > 0 and A[i-1] > valueToInsert do:
         A[holePosition] = A[holePosition-1]
         holePosition = holePosition -1
      end while
      /* insert the number at hole position */
      A[holePosition] = valueToInsert
   end for
end procedure

Demo Program

package com.tutorialspoint.simplesort;

import java.util.Arrays;

public class InsertionSortDemo {
     
   public static void main(String[] args){
      int[] sourceArray = {4,6,3,2,1,9,7};
      System.out.println("Input Array: " 
         + Arrays.toString(sourceArray));
      printline(50);
      System.out.println("Output Array: " 
         + Arrays.toString(insertionSort(sourceArray)));
      printline(50);        
   }    

   public static void printline(int count){
      for(int i=0;i <count-1;i++){
         System.out.print("=");
      }
         System.out.println("=");
   }

   public static int[] insertionSort(int[] intArray){

      int valueToInsert;
      int holePosition;
      // loop through all numbers 
      for(int i=1; i < intArray.length; i++){ 
         // select a value to be inserted. 
         valueToInsert = intArray[i];
         // select the hole position where number is to be inserted 
         holePosition = i;
         // check if previous no. is larger than value to be inserted 
         while (holePosition > 0 && intArray[i-1] > valueToInsert){
            intArray[holePosition] = intArray[holePosition-1];
            holePosition--;
            System.out.println(" item moved :" + intArray[holePosition]);
         }

         if(holePosition!= i){
            System.out.println(" item inserted :" 
               + valueToInsert +", at position :" + holePosition);
            // insert the number at hole position 
            intArray[holePosition] = valueToInsert;   
         }

         System.out.println("iteration "+(i) +"#: " 
            + Arrays.toString(intArray));
      }
      return intArray;
   }
}

If we compile and run the above program then it would produce following result −

Input Array: [4, 6, 3, 2, 1, 9, 7]
==================================================
iteration 1#: [4, 6, 3, 2, 1, 9, 7]
 item moved :6
 item moved :4
 item inserted :3, at position :0
iteration 2#: [3, 4, 6, 2, 1, 9, 7]
 item moved :6
 item moved :4
 item moved :3
 item inserted :2, at position :0
iteration 3#: [2, 3, 4, 6, 1, 9, 7]
 item moved :6
 item moved :4
 item moved :3
 item moved :2
 item inserted :1, at position :0
iteration 4#: [1, 2, 3, 4, 6, 9, 7]
iteration 5#: [1, 2, 3, 4, 6, 9, 7]
 item moved :9
 item moved :6
 item inserted :7, at position :4
iteration 6#: [1, 2, 3, 4, 7, 6, 9]
Output Array: [1, 2, 3, 4, 7, 6, 9]
==================================================
Advertisements