• 【转】Java实现折半查找(二分查找)的递归和非递归算法


    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://wintys.blog.51cto.com/425414/94051

    Java二分查找实现,欢迎大家提出交流意见.
    /**
    *名称: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") );
        }
    }
    博客地址: http://www.cnblogs.com/dwf07223,本文以学习、研究和分享为主,欢迎转载,转载请务必保留此出处。若本博文中有不妥或者错误处请不吝赐教。

  • 相关阅读:
    codeforces 985 F. Isomorphic Strings
    Educational Codeforces Round 44
    codeforces 979D
    ARC060 Digit Sum II
    Iroha and Haiku II
    Unhappy Hacking II
    Just h-index 2018湘潭邀请赛
    [HAOI2007]理想的正方形
    P1231 教辅的组成
    最小割数学形式
  • 原文地址:https://www.cnblogs.com/dwf07223/p/3372870.html
Copyright © 2020-2023  润新知