八大排序算法之 一 插入排序---折半插入排序算法

public class BinaryInsertSort {
    public static void main(String[] args){
        int[] array = {2,6,1,9,4,3,23,65,0,7};
        System.out.print("the array before is:");
        for(int i = 0; i < array.length; i++){
            System.out.print(array[i]+"   ");
        }
        System.out.println(" ");
        System.out.println("the array after is:");
        binaryInsertSort(array);
    }
    private static void binaryInsertSort(int[] array){
        int len = array.length;
        for(int i = 1; i < len; i++){
            int low = 0;
            int high = i - 1;
            int temp = array[i];
            while(low <= high){
                int middle = (low + high)/2;
                if(array[i] < array[middle]){
                    high = middle - 1;
                }else{
                    low = middle + 1;
                }    
            }                            
        for(int j = i;j > low; j--){
            array[j] = array[j-1];
        }
        array[low] = temp;
        for(int z = 0; z < array.length; z++){
            System.out.print(array[z]+"   ");
        }
        System.out.println(" ");
        }
    }
}

如图所示,是代码实现。

转载于:https://www.cnblogs.com/RunForLove/p/4357356.html

已标记关键词 清除标记
表情包
插入表情
评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页