隨筆 - 117  文章 - 72  trackbacks - 0

          聲明:原創(chuàng)作品(標(biāo)有[原]字樣)轉(zhuǎn)載時(shí)請(qǐng)注明出處,謝謝。

          常用鏈接

          常用設(shè)置
          常用軟件
          常用命令
           

          訂閱

          訂閱

          留言簿(7)

          隨筆分類(130)

          隨筆檔案(123)

          搜索

          •  

          積分與排名

          • 積分 - 155988
          • 排名 - 389

          最新評(píng)論

          Java二分查找實(shí)現(xiàn),歡迎大家提出交流意見.
          /**
          *名稱:BinarySearch
          *功能:實(shí)現(xiàn)了折半查找(二分查找)的遞歸和非遞歸算法.
          *說明:
          *     1、要求所查找的數(shù)組已有序,并且其中元素已實(shí)現(xiàn)Comparable<T>接口,如Integer、String等.
          *    2、非遞歸查找使用search();,遞歸查找使用searchRecursively();
          *
          *本程序僅供編程學(xué)習(xí)參考
          *
          *@author:   Winty
          *@date:     2008-8-11
          *@email:    [email]wintys@gmail.com[/email]
          */


          class BinarySearch<T extends Comparable<T>> {
              private T[]  data;//要排序的數(shù)據(jù)

              public BinarySearch(T[] data){
                  this.data = data;
              }

              public int search(T key){
                  int low;
                  int high;
                  int mid;

                  if(data == null)
                      return -1;

                  low = 0;
                  high = data.length - 1;

                  while(low <= high){
                      mid = (low + high) / 2;
                      System.out.println("mid " + mid + " mid value:" + data[mid]);///
                      
                      if(key.compareTo(data[mid]) < 0){
                          high = mid - 1;
                      }else if(key.compareTo(data[mid]) > 0){
                          low = mid + 1;
                      }else if(key.compareTo(data[mid]) == 0){
                          return mid;
                      }
                  }

                  return -1;
              }

              private int doSearchRecursively(int low , int high , T key){
                  int mid;
                  int result;

                  if(low <= high){
                      mid = (low + high) / 2;
                      result = key.compareTo(data[mid]);
                      System.out.println("mid " + mid + " mid value:" + data[mid]);///
                      
                      if(result < 0){
                          return doSearchRecursively(low , mid - 1 , key);
                      }else if(result > 0){
                          return doSearchRecursively(mid + 1 , high , key);
                      }else if(result == 0){
                          return mid;
                      }
                  }
                  
                  return -1;
              }

              public int searchRecursively(T key){
                  if(data ==null)return -1;

                  return doSearchRecursively(0 , data.length - 1 , key);
              }

              public static void main(String[] args){
                  Integer[] data = {1 ,4 ,5 ,8 ,15 ,33 ,48 ,77 ,96};
                  BinarySearch<Integer> binSearch = new BinarySearch<Integer>(data);
                  //System.out.println("Key index:" + binSearch.search(33) );

                  System.out.println("Key index:" + binSearch.searchRecursively(3) );

                  //String [] dataStr = {"A" ,"C" ,"F" ,"J" ,"L" ,"N" ,"T"};
                  //BinarySearch<String> binSearch = new BinarySearch<String>(dataStr);
                  //System.out.println("Key index:" + binSearch.search("A") );
              }
          }


          文章來源:http://wintys.blog.51cto.com/425414/94051
          posted on 2009-03-18 12:02 天堂露珠 閱讀(503) 評(píng)論(0)  編輯  收藏 所屬分類: Java
          主站蜘蛛池模板: 南漳县| 昌都县| 游戏| 岳普湖县| 唐海县| 保靖县| 格尔木市| 达日县| 洛宁县| 故城县| 天门市| 舞钢市| 孟村| 夏河县| 芦山县| 凤阳县| 页游| 化州市| 苏尼特右旗| 芜湖县| 芷江| 双城市| 宁远县| 镇坪县| 岚皋县| 博野县| 罗江县| 河北省| 全椒县| 应城市| 宜良县| 瑞丽市| 玛曲县| 花莲市| 临潭县| 常熟市| 嵊泗县| 灵石县| 尼勒克县| 饶河县| 德钦县|