Last active
December 21, 2015 17:39
-
-
Save wynn5a/6341957 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class InsertionSort { | |
public static void sort(int[] array) { | |
for (int i = 1; i < array.length; i++) { | |
int key = array[i]; | |
// insert a[j] into the sorted sequence A[0,j-1] | |
int j = i - 1; | |
while (j >= 0 && array[j] > key) { | |
array[j + 1] = array[j]; | |
j--; | |
} | |
array[j + 1] = key; | |
Utils.printArray(array); | |
} | |
} | |
} | |
class Utils { | |
public static void printArray(int[] arr) { | |
System.out.print("Array :"); | |
for (int i = 0; i < arr.length; i++) { | |
int j = arr[i]; | |
System.out.print(j + " "); | |
} | |
System.out.println(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment