二分尋找演算法

來源:互聯網
上載者:User

標籤:元素   部分   lan   length   system   sub   count   進一步   樣本   

二分尋找又稱折半尋找,優點是比較次數少,尋找速度快,平均效能好;其缺點是要求待查表為有序表,且插入刪除困難。因此,折半尋找方法適用於不經常變動而尋找頻繁的有序列表。首先,假設表中元素是按升序排列,將表中間位置記錄的關鍵字與尋找關鍵字比較,如果兩者相等,則尋找成功;否則利用中間位置記錄將表分成前、後兩個子表,如果中間位置記錄的關鍵字大於尋找關鍵字,則進一步尋找前一子表,否則進一步尋找後一子表。重複以上過程,直到找到滿足條件的記錄,使尋找成功,或直到子表不存在為止,此時尋找不成功。

二分尋找的基本思想是將n個元素分成大致相等的兩部分,取a[n/2]與x做比較,如果x=a[n/2],則找到x,演算法中止;如果x<a[n/2],則只要在數組a的左半部分繼續搜尋x,如果x>a[n/2],則只要在數組a的右半部搜尋x.、

一下是java開發樣本

package twoG;/** * 二分演算法  :  優點-->比較次數少,尋找速度快,平均效能好。  缺點-->要求待查表為有序表,且插入刪除困難。 *                 適用於不經常變動而尋找頻繁的有序列表 *  * 二分尋找的基本思想是將n個元素分成大致相等的倆個部分,去a[n/2]與x作比較,如果x=a[n/2],演算法終止, * 如果x<a[n/2],則要在x的左半部分搜尋x,如果x>a[n/2],則要在x的右半部分搜尋x * @author DELL * */public class BinarySearch {    private int rCount = 0;    private int lCount = 0;    /**     * 擷取遞迴的次數     */    public int getrCount(){        return rCount;    }    /**     * 擷取迴圈次數     *      */    public int getlCount(){        return lCount;    }    /**     * 執行遞迴二分演算法,返回第一次出現該值的位置     * sortedDate 已經排序的數組     * start 開始位置     * end  結束位置     * findValue  需要找的值     * return   在數組中的位置,從0開始,找不到返回-1     *      *      */    public int searchRecursive(int[] sortedData,int start,int end,int findValue)      {          rCount++;    //遞迴次數        if(start<=end)          {              //中間位置              int middle=(start+end)>>1;    //相當於(start+end)/2              //中值              int middleValue=sortedData[middle];                            if(findValue==middleValue)              {                  //等於中值直接返回                  return middle;              }              else if(findValue<middleValue)              {                  //小於中值時在中值前面找                  return searchRecursive(sortedData,start,middle-1,findValue);              }              else              {                  //大於中值在中值後面找                  return searchRecursive(sortedData,middle+1,end,findValue);              }          }          else          {              //找不到              return -1;          }      }            /**      * 迴圈二分尋找,返回第一次出現該值的位置      * @param sortedData    已排序的數組      * @param findValue     需要找的值      * @return              值在數組中的位置,從0開始。找不到返回-1      */      public int searchLoop(int[] sortedData,int findValue)      {          int start=0;          int end=sortedData.length-1;                    while(start<=end)          {              lCount++;   //迴圈次數            //中間位置              int middle=(start+end)>>1;    //相當於(start+end)/2              //中值              int middleValue=sortedData[middle];                            if(findValue==middleValue)              {                  //等於中值直接返回                  return middle;              }              else if(findValue<middleValue)              {                  //小於中值時在中值前面找                  end=middle-1;              }              else              {                  //大於中值在中值後面找                  start=middle+1;              }          }          //找不到          return -1;      }       }
package twoG;public class BinarySearchTest {    public void testSearch()      {          BinarySearch bs=new BinarySearch();                    int[] sortedData={1,2,3,4,5,6,6,7,8,8,9,10};          int findValue=9;          int length=sortedData.length;          /**         * 遞迴二分演算法           */        int pos=bs.searchRecursive(sortedData, 0, length-1, findValue);          System.out.println("Recursice:"+findValue+" found in pos "+pos+";count:"+bs.getrCount());          /**         * 迴圈二分尋找         */        int pos2=bs.searchLoop(sortedData, findValue);           System.out.println("Loop:"+findValue+" found in pos "+pos+";count:"+bs.getlCount());      }      public static void main(String[] args){        BinarySearchTest t = new BinarySearchTest();        t.testSearch();    }}

 

二分尋找演算法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.