Skip to content

Instantly share code, notes, and snippets.

@apremalal
Created February 18, 2013 06:16
Show Gist options
  • Save apremalal/4975407 to your computer and use it in GitHub Desktop.
Save apremalal/4975407 to your computer and use it in GitHub Desktop.
Insertion sort implementation in java.
public class InsertionSort {
public static void main(String a[]) {
int data[] = { 9, 2, 6, 1, 8, 10, 1, 0 };
int sortedarr[] = insertionSort(data);
for (int x : sortedarr) {
System.out.print(x + " ");
}
}
public static int[] insertionSort(int array[]) {
int key, j;
for (int i = 1; i < array.length; i++) {
key = array[i];
j = i - 1;
while ((j >= 0) && (array[j] > key)) {
array[j + 1] = array[j];
j--;
}
array[j + 1] = key;
}
return array;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment