编辑代码

import java.util.Arrays;
 
public class BinsertSort {
    public static void main(String[] args){
        int[] a={2,6,3,8,1,4,2,7};
        BInsertSort(a);//调用方法
    }
 
    private static void BInsertSort(int[] s){
        for(int i=1;i<s.length;i++){
            int temp=s[i];//保存要插入的数的数值
            int low=0;//设置查找区间初始值 下区间值
            int high=i-1;//上区间值
            while(low<=high){//查找结束条件
                int mid=(high+low)/2;//折半,中间值
                if(s[mid]>temp){//待插入值小于中间值
                    high=mid-1;//上区间值变为中间值-1
                }
                else {//待插入值大于中间值
                    low=mid+1;//下区间值变为中间值+1
                }
            }
            for (int j=i-1;j>=low;j--){
                s[j+1]=s[j];//将low及low之前的数向前移动一位
            }
            s[low]=temp;//low就是待插入值的适当插入点
        }
        System.out.println(Arrays.toString(s));//输出排好序的数组
    }
}