直接插入排序算法:
将一个记录插入到已经排好序的有序表中,从而得到一个新的、记录数增1的有序表。
public class InsertSort {
public static void Sort(int[] L) {
int i;
int j;
for (i = 2; i < L.length; i++) {
if (L[i] < L[i - 1]) {
L[0] = L[i];
for (j = i - 1; L[j ] > L[0]; j -- )
L[j + 1] = L[j ];
L[j + 1] = L[0] ;
}
}
}
/**
* @param args
*/
public static void main(String[] args) {
int[] test = { 0 , 2, 4, 8, 6 , 56, 3, 45, 8, 32 };
Sort(test);
for (int i = 1; i < test.length; i++) {
System.out.print(test[i] + " ");
}
}