二分排序的思想:
1)、给定下界:low,给定上界:high,中:mid
2)、将插入的数和mid位置的数组元素进行比较,如果比mid位置的大则low=mid+1,否则high=mid-1
3)、之后需要从插入数插入位置开始的数组元素全部往后摞一位。腾出hight+1存放插入数。
public void insertSort2(int a[]){ for(int i=0;i<a.length-1;i++){ int temp=a[i+1]; int low=0; int high=i; int mid; while(low<=high){ mid=(low+high)/2; if(a[mid]<temp){ low=mid+1; }else{ high=mid-1; } } for(int j=i;j>high;j--){ a[j+1]=a[j];//j+1等价于给定范围j=i+1; } a[high+1]=temp; } print(a); }