Created
August 9, 2012 19:44
-
-
Save felipernb/3307444 to your computer and use it in GitHub Desktop.
Insertion Sort in Java
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
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