关于我们

质量为本、客户为根、勇于拼搏、务实创新

< 返回新闻公共列表

如何把原素插进井然有序数组中

发布时间:2020-03-09 00:00:00

折半查找的运用

public static void main(String[] args) {int[] arr = {4,6,6,7,8,9,10};
        System.out.println(getIndexOf(arr,5));
    }public static int getIndexOf(int[] arr, int key){int min = 0,
                max = arr.length-1,
                mid;while (min<=max){
            mid = (max+min)>>1;if ( key > arr[mid])
                min = mid+1;else if (key < arr[mid])
                max = mid-1;elsereturn mid;
        }return min;
    }

 


/template/Home/Zkeys/PC/Static