二分查找java代码

public int find(long searchKey){
        
        int i;
        int begin = 0;
        int end = nElems - 1;
                
        while(true){
            i = (begin + end) / 2;
            
            if (searchKey == a[i]){
                return i;
            } else if (begin > end) {
                return -1;
            } if (searchKey > a[i]) {
                begin = i + 1if (searchKey < a[i]) {
                end = i - 1; 
            }
        }
    }

其中a是待查有序数组,searchKey是待查数值。

查询次数为num = log2(a.length)即数组长度的对数

相关文章

背景:计算机内部用补码表示二进制数。符号位1表示负数,0表...
大家好,我们现在来讲解关于加密方面的知识,说到加密我认为不...
相信大家在大学的《算法与数据结构》里面都学过快速排序(Qui...
加密在编程中的应用的是非常广泛的,尤其是在各种网络协议之...
前言我的目标是写一个非常详细的关于diff的干货,所以本文有...
对称加密算法 所有的对称加密都有一个共同的特点:加密和...