zoukankan      html  css  js  c++  java
  • 浅谈顺序、折半查找

    线性表查找的实现原理

      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]);
    }
    

    }

  • 相关阅读:
    爬虫练习:使用xpath 下载站长之家简历封面模板
    爬虫练习:Xpath基本使用
    爬虫练习:使用xpath爬取彼岸图网美女图
    爬虫练习:使用re模块爬取 糗图百科 图片
    爬虫练习:使用bs4爬取诗词名句网的《三国演义》
    爬虫练习:BS4基本使用
    爬虫练习:使用requests模块 爬取化妆品生产许可信息管理系统服务平台
    爬虫练习:贴吧 https://tieba.baidu.com/f?kw=友谊已走到尽头
    爬虫练习:使用 bs4以及正则,urllib.request 爬取 豆瓣TOP250爬虫
    记录kettle错误:无法从套接字读取更多数据 / TNS包错误
  • 原文地址:https://www.cnblogs.com/qiuyong/p/6842134.html
Copyright © 2011-2022 走看看