diff --git a/InsertionSort.java b/InsertionSort.java index e27a9b3..012bcf8 100644 --- a/InsertionSort.java +++ b/InsertionSort.java @@ -1,33 +1,50 @@ -// Java program for implementation of Insertion Sort -class InsertionSort{ - void sort(int arr[]){ - int n = arr.length; - for (int i=1; i=0 && arr[j] > key){ - arr[j+1] = arr[j]; - j = j-1; - } - arr[j+1] = key; - } - } - - static void printArray(int arr[]){ - int n = arr.length; - for (int i=0; i 0 && temp < A[j-1]) { + + // moving the left side element to one position forward. + A[j] = A[j-1]; + j= j - 1; + + } + // moving current element to its correct position. + A[ j ] = temp; + } +} + + /* A utility function to print array of size n*/ + static void printArray(int arr[]) + { + int n = arr.length; + for (int i=0; i