Java實現折半尋找(二分尋找)的遞迴和非遞迴演算法

來源:互聯網
上載者:User

轉 : http://wintys.blog.51cto.com/425414/94051

 

/**
*名稱:BinarySearch
*功能:實現了折半尋找(二分尋找)的遞迴和非遞迴演算法.
*說明:
*     1、要求所尋找的數組已有序,並且其中元素已實現Comparable<T>介面,如Integer、String等.
*    2、非遞迴尋找使用search();,遞迴尋找使用searchRecursively();
*
*本程式僅供編程學習參考
*
*@author:   Winty
*@date:     2008-8-11
*@email:    wintys@gmail.com
*/

class BinarySearch<T extends Comparable<T>> {
    private T[]  data;//要排序的資料

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.