Java实现折半查找(二分查找)的递归和非递归算法
生活随笔
收集整理的這篇文章主要介紹了
Java实现折半查找(二分查找)的递归和非递归算法
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
Java二分查找實現(xiàn),歡迎大家提出交流意見.
/**
*名稱:BinarySearch
*功能:實現(xiàn)了折半查找(二分查找)的遞歸和非遞歸算法.
*說明:
*???? 1、要求所查找的數(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") );
????}
}
/**
*名稱:BinarySearch
*功能:實現(xiàn)了折半查找(二分查找)的遞歸和非遞歸算法.
*說明:
*???? 1、要求所查找的數(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") );
????}
}
轉(zhuǎn)載于:https://blog.51cto.com/wintys/94051
總結(jié)
以上是生活随笔為你收集整理的Java实现折半查找(二分查找)的递归和非递归算法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Repeater,DataList,Da
- 下一篇: [转]什么是RSS