import java.util.Arrays;
/**
* @author captain
*
*/
public class InsertSort {
public static void insertSort(int[] array, int nextIndex) {
if (nextIndex > array.length - 1) {
return;
}
for (int i = 0; i < nextIndex; i++) {
if (array[i] > array[nextIndex]) {
swap(array, i, nextIndex);
}
}
nextIndex++;
insertSort(array, nextIndex);
}
public static void swap(int[] array, int i, int j) {
int tmp = array[i];
array[i] = array[j];
array[j] = tmp;
}
/**
* @param args
*/
public static void main(String[] args) {
int[] testArray = {57, 68, 59, 52, 4, 4, 6, 7, 3, 22, 33, 44, 66};
insertSort(testArray, 1);
System.out.println(Arrays.toString(testArray));
}
}
网友评论