Skip to content

Instantly share code, notes, and snippets.

@felipernb
Created August 9, 2012 19:44
Show Gist options
  • Save felipernb/3307444 to your computer and use it in GitHub Desktop.
Save felipernb/3307444 to your computer and use it in GitHub Desktop.
Insertion Sort in Java
package com.feliperibeiro.kata;
public class InsertionSort {
/**
* In-place Insertion Sort - O(n^2)
*/
public void sort(int[] items) {
for (int i = 1; i < items.length; i++) {
int n = items[i];
int pos = i;
while (pos > 0 && items[pos-1] > n) {
items[pos] = items[--pos];
}
items[pos] = n;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment