浅谈顺序折半查找

Posted 码农皮邱

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了浅谈顺序折半查找相关的知识,希望对你有一定的参考价值。

线性表查找的实现原理

  1、线性表查找:顺序查找、折半查找。

  2、顺序查找的实现思想

    遍历全表,判断值是否相等,俗称蛮力法。

  3、折半查找

    步骤一:设置初始查找取件:left=0;right=n;

    步骤二:测试查找区间[left,right]是否存在,若不存在,则查找失败,否则

    步骤三:取中间位置mid=(left+right)/2;比较target与array[mid],有三种情况

          若target<array[mid],right=mid-1;查找在mid左半区继续进行,返回步骤二

          若target>array[mid],left=mid+1;查找在mid右半区继续进行,返回步骤二

          若target=array[mid],则查找成功,返回记录在表中的位置mid。

顺序查找的代码实现

public class SequenceList {
    //数组的蛮力查找
    public static int sequentialSearch(int [] array,int target){
        int length=array.length-1;
        while(array[length]!=target&&length>=0)
            length--;
        if(array[length]==target)
            return length;
        return -1;
    }
</span><span style="color: #008000;">//</span><span style="color: #008000;">单链表的蛮力查找</span>
<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">int</span> sequetialSearchByLinkedList(Node first,<span style="color: #0000ff;">int</span><span style="color: #000000;"> target){
    </span><span style="color: #0000ff;">if</span>(<span style="color: #0000ff;">null</span>==<span style="color: #000000;">first)
        </span><span style="color: #0000ff;">return</span> -1<span style="color: #000000;">;
    </span><span style="color: #0000ff;">int</span> count=0<span style="color: #000000;">;
    Node p</span>=<span style="color: #000000;">first;
    </span><span style="color: #0000ff;">while</span>(p.getData()!=<span style="color: #000000;">target){
        count</span>++<span style="color: #000000;">;
        p</span>=<span style="color: #000000;">p.getNext();
    }
    </span><span style="color: #0000ff;">if</span>(p.getData()==<span style="color: #000000;">target)
        </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> count;
    </span><span style="color: #0000ff;">return</span> -1<span style="color: #000000;">;
}

}

折半查找的代码实现

 

public class SequenceList {
    public static int binarySearch(int [] array,int target){
        if(array==null||array.length<0){
            return -1;
        }
        int left=0;
        int right=array.length;
        while(left<=right){
            int mid=(left+right)/2;
            if(target>array[mid]){
                left=mid+1;
            }else if(target<array[mid]){
                right=mid-1;
            }else{
                return mid;
            }    
        }
        return -1;
    }
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">int</span> recursiveBinarySearch(<span style="color: #0000ff;">int</span> [] array,<span style="color: #0000ff;">int</span> target,<span style="color: #0000ff;">int</span> left,<span style="color: #0000ff;">int</span><span style="color: #000000;"> right){
    </span><span style="color: #0000ff;">if</span>(array==<span style="color: #0000ff;">null</span>||array.length&lt;0<span style="color: #000000;">){
        </span><span style="color: #0000ff;">return</span> -1<span style="color: #000000;">;
    }
    </span><span style="color: #0000ff;">if</span>(left&gt;<span style="color: #000000;">right){
        </span><span style="color: #0000ff;">return</span> -1<span style="color: #000000;">;
    }</span><span style="color: #0000ff;">else</span><span style="color: #000000;">{
        </span><span style="color: #0000ff;">int</span> mid=(left+right)/2<span style="color: #000000;">;
        </span><span style="color: #0000ff;">if</span>(target&lt;<span style="color: #000000;">array[mid]){
            </span><span style="color: #0000ff;">return</span> recursiveBinarySearch(array, target, left, mid-1<span style="color: #000000;">);
        }</span><span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span>(target&gt;<span style="color: #000000;">array[mid]){
            </span><span style="color: #0000ff;">return</span> recursiveBinarySearch(array, target, mid+1<span style="color: #000000;">, right);
        }</span><span style="color: #0000ff;">else</span><span style="color: #000000;">{
            </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> mid;
        }
    }
}

</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> main(String[] args) {
    </span><span style="color: #0000ff;">int</span> [] array=<span style="color: #0000ff;">new</span> <span style="color: #0000ff;">int</span>[]{7,14,18,21,23,29,31,35,38,42,46,49,52<span style="color: #000000;">};
    
    Arrays.sort(array);
    </span><span style="color: #0000ff;">int</span> j=SequenceList.binarySearch(array,11<span style="color: #000000;">);
    System.out.println(array[j]);
    
    </span><span style="color: #0000ff;">int</span> k=SequenceList.recursiveBinarySearch(array, 66,0<span style="color: #000000;">,array.length);
    System.out.println(array[k]);
}

}

以上是关于浅谈顺序折半查找的主要内容,如果未能解决你的问题,请参考以下文章

C语言折半查找法详细代码(假如有10个已排好序的数)

数据结构 查找表的顺序查找折半查找插值查找以及Fibonacci查找

顺序查找折半查找的区别与联系

顺序查找和折半查找

顺序查找和折半查找

顺序查找 折半查找 分块查找