java 二分法算法的實例
java 二分法算法的實例
1、前提:二分查找的前提是需要查找的數(shù)組必須是已排序的,我們這里的實現(xiàn)默認為升序
2、原理:將數(shù)組分為三部分,依次是中值(所謂的中值就是數(shù)組中間位置的那個值)前,中值,中值后;將要查找的值和數(shù)組的中值進行比較,若小于中值則在中值前面找,若大于中值則在中值后面找,等于中值時直接返回。然后依次是一個遞歸過程,將前半部分或者后半部分繼續(xù)分解為三部分??赡苊枋龅貌皇呛芮宄羰遣焕斫饪梢匀ゾW(wǎng)上找。從描述上就可以看出這個算法適合用遞歸來實現(xiàn),可以用遞歸的都可以用循環(huán)來實現(xiàn)。所以我們的實現(xiàn)分為遞歸和循環(huán)兩種,可以根據(jù)代碼來理解算法
3、實現(xiàn):
代碼如下
package org.cyxl.algorithm.search;
/**
* 二分查找
* @author cyxl
*
*/
public class BinarySearch {
private int rCount=0;
private int lCount=0;
/**
* 獲取遞歸的次數(shù)
* @return
*/
public int getrCount() {
return rCount;
}
/**
* 獲取循環(huán)的次數(shù)
* @return
*/
public int getlCount() {
return lCount;
}
/**
* 執(zhí)行遞歸二分查找,返回第一次出現(xiàn)該值的位置
* @param sortedData 已排序的數(shù)組
* @param start 開始位置
* @param end 結(jié)束位置
* @param findValue 需要找的值
* @return 值在數(shù)組中的位置,從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;
}
}
/**
* 循環(huán)二分查找,返回第一次出現(xiàn)該值的位置
* @param sortedData 已排序的數(shù)組
* @param findValue 需要找的值
* @return 值在數(shù)組中的位置,從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;
}
}
4、測試代碼
package org.cyxl.algorithm.search.test;
import org.cyxl.algorithm.search.BinarySearch;
import org.junit.Test;
public class BinarySearchTest {
@Test
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());
}
}
5、總結(jié):這種查找方式的使用場合為已排序的數(shù)組??梢园l(fā)現(xiàn)遞歸和循環(huán)的次數(shù)是一樣的
以上就是java 二分法的實例詳解,如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
java并發(fā)編程之深入理解Synchronized的使用
文詳細講述了線程、進程的關(guān)系及在操作系統(tǒng)中的表現(xiàn),這是多線程學習必須了解的基礎(chǔ)。本文將接著講一下Java線程同步中的一個重要的概念synchronized,希望能夠給你有所幫助2021-06-06
java基本教程之常用的實現(xiàn)多線程的兩種方式 java多線程教程
下面開始學習“常用的實現(xiàn)多線程的2種方式”:Thread 和 Runnable。之所以說是常用的,是因為通過還可以通過java.util.concurrent包中的線程池來實現(xiàn)多線程2014-01-01
Java中JSONObject與JSONArray的使用示例小結(jié)
JSONObject-lib包是一個beans,collections,maps,java arrays和xml和JSON互相轉(zhuǎn)換的包,本文給大家介紹Java中JSONObject與JSONArray的使用示例小結(jié),感興趣的朋友一起看看吧2025-02-02

