Skip to content

Instantly share code, notes, and snippets.

@Kwisses
Created November 20, 2017 20:31
Show Gist options
  • Save Kwisses/1b8870bcff6f9609d0286cfc32570de9 to your computer and use it in GitHub Desktop.
Save Kwisses/1b8870bcff6f9609d0286cfc32570de9 to your computer and use it in GitHub Desktop.
InsertionSort - [Java]
package insertionsort;
class IS7 {
private static void printArray(int[] array) {
for(int i: array) {
System.out.print(i + " ");
}
System.out.println();
}
private static void insertionSort(int[] array) {
int key, value;
for(int i=1; i < array.length; i++) {
key = i;
value = array[i];
while(key > 0 && value < array[key - 1]) {
array[key] = array[key - 1];
key--;
}
array[key] = value;
}
}
public static void main(String args[]) {
int[] array = { 5, 4, 3, 2, 1 };
printArray(array);
insertionSort(array);
printArray(array);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment