public class InsertSort { public static void main(String args[]) { int a[] = {10,5,10,2}; System.out.println("this is insertsort:"); InsertSort(a); output(a); } public static void output(int a[])//输出 { int i; for(i = 0; i < a.length; i++) { System.out.print(a[i] + " "); } System.out.println(); } public static void InsertSort(int a[])//直接插入排序 { int i, j; for(i = 1; i < a.length; i++) { int temp = a[i];//保存a[i]的值 j = i - 1; while(j >= 0 && temp < a[j])//把a[i]放到合适的位置 { a[j + 1] = a[j]; j--; } a[j + 1] = temp; } } }