Intro to the Insertion Sort Algorithm

  • The Insertion Sort algorithm will:

    • Selects the next unsorted element/key in the array

    • Insert (= exchange) it towards the front into its correct position

  • Consider the following input:

          
        2   9   8   4   7   6   1
    
    
    
    
    
    
    
    
    
    
    
    

Intro to the Insertion Sort Algorithm

  • The Insertion Sort algorithm will:

    • Selects the next unsorted element/key in the array

    • Insert (= exchange) it towards the front into its correct position

  • Suppose we have sorted the first 5 elements/keys of the array with Insertion Sort:

          
        2   9   8   4   7   6   1
    
        2   4   7   8   9   6   1
    
    
    
    
    
    
    
    
    
    

Intro to the Insertion Sort Algorithm

  • The Insertion Sort algorithm will:

    • Selects the next unsorted element/key in the array

    • Insert (= exchange) it towards the front into its correct position

  • The next iteration of the Insertion Sort algorithm proceeds as follows:

          
        2   9   8   4   7   6   1
    
        2   4   7   8   9   6   1  // Compare 9 with  6  --> swap
    
    
    
    
    
    
    
    
    
    

Intro to the Insertion Sort Algorithm

  • The Insertion Sort algorithm will:

    • Selects the next unsorted element/key in the array

    • Insert (= exchange) it towards the front into its correct position

  • The next iteration of the Insertion Sort algorithm proceeds as follows:

          
        2   9   8   4   7   6   1
    
        2   4   7   8   9   6   1  
    
        2   4   7   8   6   9   1  // Compare 8 with  6  --> swap
    
    
    
    
    
    
    
    

Intro to the Insertion Sort Algorithm

  • The Insertion Sort algorithm will:

    • Selects the next unsorted element/key in the array

    • Insert (= exchange) it towards the front into its correct position

  • The next iteration of the Insertion Sort algorithm proceeds as follows:

          
        2   9   8   4   7   6   1
    
        2   4   7   8   9   6   1  
    
        2   4   7   8   6   9   1  
    
        2   4   7   6   8   9   1  // Compare 7 with  6  --> swap
    
    
    
    
    
    

Intro to the Insertion Sort Algorithm

  • The Insertion Sort algorithm will:

    • Selects the next unsorted element/key in the array

    • Insert (= exchange) it towards the front into its correct position

  • The next iteration of the Insertion Sort algorithm proceeds as follows:

          
        2   9   8   4   7   6   1
    
        2   4   7   8   9   6   1  
    
        2   4   7   8   6   9   1  
    
        2   4   7   6   8   9   1  
    
        2   4   6   7   8   9   1  // Compare 4 with  6  --> STOP
    
    
    
    

The Insertion Sort Algorithm

We start writing the header of the InsertionSort( ) algorithm as a generic method:

    public static <T extends Comparable<T>> void sort( T[] arr )
    {
        int n = arr.length;

        for (int i = 1; i < n; i++)          // Repeat n-1 times
        {
	   // Compare arr[i] with each prior element
           for ( int j = i; j > 0; j-- )
	   {
               if ( arr[j-1].compareTo(arr[j]) > 0 ) // 9 6 -> out of place
                   exch( arr, j-1, j);  // Swap list[j-1] and list[j]
	       else
	           break;
           }
        }
    }

The Insertion Sort Algorithm

Repeat the steps n−1 times:

    public static <T extends Comparable<T>> void sort( T[] arr )
    {
        int n = arr.length;

        for (int i = 1; i < n; i++)          // Repeat n-1 times
        {
	   // Compare arr[i] with each prior element
           for ( int j = i; j > 0; j-- )
	   {
               if ( arr[j-1].compareTo(arr[j]) > 0 ) // 9 6 -> out of place
                   exch( arr, j-1, j);  // Swap list[j-1] and list[j]
	       else
	           break;
           }
        }
    }

The Insertion Sort Algorithm

Move element arr[i] to its correct position:

    public static <T extends Comparable<T>> void sort( T[] arr )
    {
        int n = arr.length;

        for (int i = 1; i < n; i++)          // Repeat n-1 times
        {
	   // Compare adjacent pairs starting at (i-1, i)
           for ( int j = i; j > 0; j-- )
	   {
               if ( arr[j-1].compareTo(arr[j]) > 0 ) // 9 6 -> out of place
                   exch( arr, j-1, j);  // Swap list[j-1] and list[j]
	       else
	           break;
           }
        }
    }

The Insertion Sort Algorithm

Move element arr[i] forward if it is out-of-place:

    public static <T extends Comparable<T>> void sort( T[] arr )
    {
        int n = arr.length;

        for (int i = 1; i < n; i++)          // Repeat n-1 times
        {
	   // Compare adjacent pairs starting at (i-1, i)
           for ( int j = i; j > 0; j-- )
	   {
               if ( arr[j-1].compareTo(arr[j]) > 0 ) // 9 6 -> out of place
                   exch( arr, j-1, j);  // Swap list[j-1] and list[j]
	       else
	           break;
           }
        }
    }

The Insertion Sort Algorithm

Finish the iteration if element arr[i] is in its correct position:

    public static <T extends Comparable<T>> void sort( T[] arr )
    {
        int n = arr.length;

        for (int i = 1; i < n; i++)          // Repeat n-1 times
        {
	   // Compare adjacent pairs starting at (i-1, i)
           for ( int j = i; j > 0; j-- )
	   {
               if ( arr[j-1].compareTo(arr[j]) > 0 ) // 9 6 -> out of place
                   exch( arr, j-1, j);  // Swap list[j-1] and list[j]
	       else
	           break;
           }
        }
    }

DEMO: demo/14-sort/03-bubble-sort/InsertionSort.java + Demo.java (Integer) + Demo2.java (String)

Runtime analysis of the Insertion Sort

  • The loop structure of the Insertion Sort algorithm (assuming array length = n):

      for (int i = 1; i < n; i++)
         for ( int j = i; j > 0; j-- )
             doPrimitive();  // Worst case analysis (ignore break)
    

    Analysis:

    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    

Runtime analysis of the Insertion Sort

  • The loop structure of the Insertion Sort algorithm (assuming array length = n):

      for (int i = 1; i < n; i++)
         for ( int j = i; j > 0; j-- )
             doPrimitive();  // Worst case analysis (ignore break)
    

    Analysis:

     The variable i takes on these values:
    
        i =    1    2    3     ....     n-1
    
    
    
    
    
    
    
    
    
    
    
    
    

Runtime analysis of the Insertion Sort

  • The loop structure of the Insertion Sort algorithm (assuming array length = n):

      for (int i = 1; i < n; i++)
         for ( int j = i; j > 0; j-- )
             doPrimitive();  // Worst case analysis (ignore break)
    

    Analysis:

     The variable j takes on these values for each i:
    
        i =    1    2    3     ....     n-1
    
        j =    1    2    3     ....     n-1
                    1    2     ....     n-2
    	             1	   ....     n-3
    	                            ...
    	                             1
    	  
    
    
    
    
    
    

Runtime analysis of the Insertion Sort

  • The loop structure of the Insertion Sort algorithm (assuming array length = n):

      for (int i = 1; i < n; i++)
         for ( int j = i; j > 0; j-- )
             doPrimitive();  // Worst case analysis (ignore break)
    

    Analysis:

     The variable j takes on these values for each i:
    
        i =    1    2    3     ....     n-1
    
        j =    1    2    3     ....     n-1
                    1    2     ....     n-2
    	             1	   ....     n-3
    	                            ...
    	                             1
    	  
     #Iterations = 1 + 2 + 3 + .... + (n-1)    (Triangular sum !)
    
    
    
    
    

Runtime analysis of the Insertion Sort

  • The loop structure of the Insertion Sort algorithm (assuming array length = n):

      for (int i = 1; i < n; i++)
         for ( int j = i; j > 0; j-- )
             doPrimitive();  // Worst case analysis (ignore break)
    

    Analysis:

     The variable j takes on these values for each i:
    
        i =    1    2    3     ....     n-1
    
        j =    1    2    3     ....     n-1
                    1    2     ....     n-2
    	             1	   ....     n-3
    	                            ...
    	                             1
    	  
     #Iterations = 1 + 2 + 3 + .... + (n-1)    (Triangular sum !)
                 = (n-1)n/2
    
     Runtime = O(n2)     (Same as Selection and Bubble sorts)
    
    

Additional properties of Insertion Sort algorithm

  • Recall: In-place

    • A sorting algorithm is in-place if it does not require another array to execute the algorithm

    The Insertion Sort algorithm is an in-place sorting algorithm


  • Recall:   Stable:

    • A sorting algorithm is stable if it preserves the relative order of equal keys in the array

    The Insertion Sort algorithm is stable

    (Because it only swap adjacent elements and will never swap 2 keys that are equal)