Skip to content

Instantly share code, notes, and snippets.

@wynn5a
Last active December 21, 2015 17:39
Show Gist options
  • Save wynn5a/6341957 to your computer and use it in GitHub Desktop.
Save wynn5a/6341957 to your computer and use it in GitHub Desktop.
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