Insertion Sorting Lecture 21
Insertion Sort Start from element 2 of list location 1 –In first iteration: Compare element 1 with all of its elements to its right If they are out of order, swap them The data till second position is now sorted. –In second iteration: Compare element 2 with all of its elements to its right If they are out of order, swap them The data till third position is now sorted. –In (n-1)th iteration: Compare element (n-1)th with all of its elements to its right If they are out of order, swap them The data till nth position is now sorted.
Insertion Sort (Example)
One step of insertion sort sortednext to be inserted 10 temp sorted less than 10
C Code of Insertion Sort void insertionSort(int[] array, int size) { int inner, outer; for (outer = 1; outer < size; outer++) { int temp = array[outer]; inner = outer; while (inner > 0) { if ( array [inner-1] <= temp ) break; array[inner] = array[inner - 1]; inner--; } array[inner] = temp; } }
C Code of Insertion Sort void insertionSort(int[] array, int size) { int inner, outer; for (outer = 1; outer < size; outer++) { int temp = array[outer]; inner = outer; while (inner > 0) { if ( array [inner-1] <= temp ) break; array[inner] = array[inner - 1]; inner--; } array[inner] = temp; } } outer = 1 temp = 2
C Code of Insertion Sort void insertionSort(int[] array, int size) { int inner, outer; for (outer = 1; outer < size; outer++) { int temp = array[outer]; inner = outer; while (inner > 0) { if ( array [inner-1] <= temp ) break; array[inner] = array[inner - 1]; inner--; } array[inner] = temp; } } outer = 1 temp = 2 inner = 1
C Code of Insertion Sort void insertionSort(int[] array, int size) { int inner, outer; for (outer = 1; outer < size; outer++) { int temp = array[outer]; inner = outer; while (inner > 0) { if ( array [inner-1] <= temp ) break; array[inner] = array[inner - 1]; inner--; } array[inner] = temp; } } outer = 1 temp = 2 inner = 1 array [inner-1] = 7 array [inner] = 2
C Code of Insertion Sort void insertionSort(int[] array, int size) { int inner, outer; for (outer = 1; outer < size; outer++) { int temp = array[outer]; inner = outer; while (inner > 0) { if ( array [inner-1] <= temp ) break; array[inner] = array[inner - 1]; inner--; } array[inner] = temp; } } outer = 1 temp = 2 inner = 1 array [inner-1] = 7 array [inner] = 7
C Code of Insertion Sort void insertionSort(int[] array, int size) { int inner, outer; for (outer = 1; outer < size; outer++) { int temp = array[outer]; inner = outer; while (inner > 0) { if ( array [inner-1] <= temp ) break; array[inner] = array[inner - 1]; inner--; } array[inner] = temp; } } outer = 1 temp = 2 inner = 0 array [inner-1] = 7 array [inner] = 7
C Code of Insertion Sort void insertionSort(int[] array, int size) { int inner, outer; for (outer = 1; outer < size; outer++) { int temp = array[outer]; inner = outer; while (inner > 0) { if ( array [inner-1] <= temp ) break; array[inner] = array[inner - 1]; inner--; } array[inner] = temp; } } outer = 1 temp = 2 inner = 0 array [inner-1] = 7 array [inner] = 2
C Code of Insertion Sort void insertionSort(int[] array, int size) { int inner, outer; for (outer = 1; outer < size; outer++) { int temp = array[outer]; inner = outer; while (inner > 0) { if ( array [inner-1] <= temp ) break; array[inner] = array[inner - 1]; inner--; } array[inner] = temp; } } outer = 2 temp = 8
C Code of Insertion Sort void insertionSort(int[] array, int size) { int inner, outer; for (outer = 1; outer < size; outer++) { int temp = array[outer]; inner = outer; while (inner > 0) { if ( array [inner-1] <= temp ) break; array[inner] = array[inner - 1]; inner--; } array[inner] = temp; } } outer = 2 temp = 8 inner = 2
C Code of Insertion Sort void insertionSort(int[] array, int size) { int inner, outer; for (outer = 1; outer < size; outer++) { int temp = array[outer]; inner = outer; while (inner > 0) { if ( array [inner-1] <= temp ) break; array[inner] = array[inner - 1]; inner--; } array[inner] = temp; } } outer = 2 temp = 8 inner = 2 array [inner-1] = 7
C Code of Insertion Sort void insertionSort(int[] array, int size) { int inner, outer; for (outer = 1; outer < size; outer++) { int temp = array[outer]; inner = outer; while (inner > 0) { if ( array [inner-1] <= temp ) break; array[inner] = array[inner - 1]; inner--; } array[inner] = temp; } } outer = 2 temp = 8 inner = 2 array [inner-1] = 7 array [inner] = 8
C Code of Insertion Sort void insertionSort(int[] array, int size) { int inner, outer; for (outer = 1; outer < size; outer++) { int temp = array[outer]; inner = outer; while (inner > 0) { if ( array [inner-1] <= temp ) break; array[inner] = array[inner - 1]; inner--; } array[inner] = temp; } } outer = 3 temp = 5
C Code of Insertion Sort void insertionSort(int[] array, int size) { int inner, outer; for (outer = 1; outer < size; outer++) { int temp = array[outer]; inner = outer; while (inner > 0) { if ( array [inner-1] <= temp ) break; array[inner] = array[inner - 1]; inner--; } array[inner] = temp; } } outer = 3 temp = 5 inner = 3
C Code of Insertion Sort void insertionSort(int[] array, int size) { int inner, outer; for (outer = 1; outer < size; outer++) { int temp = array[outer]; inner = outer; while (inner > 0) { if ( array [inner-1] <= temp ) break; array[inner] = array[inner - 1]; inner--; } array[inner] = temp; } } outer = 3 temp = 5 inner = 3 array [inner-1] = 8 array [inner] = 5
C Code of Insertion Sort void insertionSort(int[] array, int size) { int inner, outer; for (outer = 1; outer < size; outer++) { int temp = array[outer]; inner = outer; while (inner > 0) { if ( array [inner-1] <= temp ) break; array[inner] = array[inner - 1]; inner--; } array[inner] = temp; } } outer = 3 temp = 5 inner = 3 array [inner-1] = 8 array [inner] = 8
C Code of Insertion Sort void insertionSort(int[] array, int size) { int inner, outer; for (outer = 1; outer < size; outer++) { int temp = array[outer]; inner = outer; while (inner > 0) { if ( array [inner-1] <= temp ) break; array[inner] = array[inner - 1]; inner--; } array[inner] = temp; } } outer = 3 temp = 5 inner = 2 array [inner-1] = 7 array [inner] = 8
C Code of Insertion Sort void insertionSort(int[] array, int size) { int inner, outer; for (outer = 1; outer < size; outer++) { int temp = array[outer]; inner = outer; while (inner > 0) { if ( array [inner-1] <= temp ) break; array[inner] = array[inner - 1]; inner--; } array[inner] = temp; } } outer = 3 temp = 5 inner = 2 array [inner-1] = 7 array [inner] = 8
C Code of Insertion Sort void insertionSort(int[] array, int size) { int inner, outer; for (outer = 1; outer < size; outer++) { int temp = array[outer]; inner = outer; while (inner > 0) { if ( array [inner-1] <= temp ) break; array[inner] = array[inner - 1]; inner--; } array[inner] = temp; } } outer = 3 temp = 5 inner = 2 array [inner-1] = 7 array [inner] = 7
C Code of Insertion Sort void insertionSort(int[] array, int size) { int inner, outer; for (outer = 1; outer < size; outer++) { int temp = array[outer]; inner = outer; while (inner > 0) { if ( array [inner-1] <= temp ) break; array[inner] = array[inner - 1]; inner--; } array[inner] = temp; } } outer = 3 temp = 5 inner = 1 array [inner-1] = 2 array [inner] = 7
C Code of Insertion Sort void insertionSort(int[] array, int size) { int inner, outer; for (outer = 1; outer < size; outer++) { int temp = array[outer]; inner = outer; while (inner > 0) { if ( array [inner-1] <= temp ) break; array[inner] = array[inner - 1]; inner--; } array[inner] = temp; } } outer = 3 temp = 5 inner = 1 array [inner-1] = 2 array [inner] = 7
C Code of Insertion Sort void insertionSort(int[] array, int size) { int inner, outer; for (outer = 1; outer < size; outer++) { int temp = array[outer]; inner = outer; while (inner > 0) { if ( array [inner-1] <= temp ) break; array[inner] = array[inner - 1]; inner--; } array[inner] = temp; } } outer = 3 temp = 5 inner = 1 array [inner-1] = 2 array [inner] = 5
C Code of Insertion Sort void insertionSort(int[] array, int size) { int inner, outer; for (outer = 1; outer < size; outer++) { int temp = array[outer]; inner = outer; while (inner > 0) { if ( array [inner-1] <= temp ) break; array[inner] = array[inner - 1]; inner--; } array[inner] = temp; } } outer = 4 temp = 9
C Code of Insertion Sort void insertionSort(int[] array, int size) { int inner, outer; for (outer = 1; outer < size; outer++) { int temp = array[outer]; inner = outer; while (inner > 0) { if ( array [inner-1] <= temp ) break; array[inner] = array[inner - 1]; inner--; } array[inner] = temp; } } outer = 4 temp = 9 inner = 4
C Code of Insertion Sort void insertionSort(int[] array, int size) { int inner, outer; for (outer = 1; outer < size; outer++) { int temp = array[outer]; inner = outer; while (inner > 0) { if ( array [inner-1] <= temp ) break; array[inner] = array[inner - 1]; inner--; } array[inner] = temp; } } outer = 4 temp = 9 inner = 4 array [inner-1] = 8
C Code of Insertion Sort void insertionSort(int[] array, int size) { int inner, outer; for (outer = 1; outer < size; outer++) { int temp = array[outer]; inner = outer; while (inner > 0) { if ( array [inner-1] <= temp ) break; array[inner] = array[inner - 1]; inner--; } array[inner] = temp; } } outer = 4 temp = 9 inner = 4 array [inner-1] = 8
Summary Insertion Sort is usually the fastest of the tree. Why ? –If the data is found already sorted. –What would be time of the insertion sort if sorted list is given? –O(n). Bubble sort and Selection sort are equal in their time, both require O(n*n) iterations? But in worst case all the three algorithms have equal time O(n*n).