
package sorting;
public class InsertSort {
public static void getInsertSort(int[] a) {
if (a == null || a.length == 0) {
System.out.println("该数组为空!");
return;
}
int n = a.length;
int temp;
int j;
for (int i = 1; i < n; i++) {
temp = a[i];
j = i - 1;
for (; j >= 0 && a[j] > temp; --j) {
a[j + 1] = a[j];
}
a[j + 1] = temp;
}
}
public static void main(String[] args) {
int[] a = { 3, 5, 1, 2, 6, 4, 7, 11, 23, 44, 3, 34 };
getInsertSort(a);
System.out.print("直接插入排序:");
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
}
}