java 二分法算法的實例
java 二分法算法的實例
1、前提:二分查找的前提是需要查找的數(shù)組必須是已排序的,我們這里的實現(xiàn)默認為升序
2、原理:將數(shù)組分為三部分,依次是中值(所謂的中值就是數(shù)組中間位置的那個值)前,中值,中值后;將要查找的值和數(shù)組的中值進行比較,若小于中值則在中值前面找,若大于中值則在中值后面找,等于中值時直接返回。然后依次是一個遞歸過程,將前半部分或者后半部分繼續(xù)分解為三部分??赡苊枋龅貌皇呛芮宄?,若是不理解可以去網上找。從描述上就可以看出這個算法適合用遞歸來實現(xiàn),可以用遞歸的都可以用循環(huán)來實現(xiàn)。所以我們的實現(xiàn)分為遞歸和循環(huán)兩種,可以根據代碼來理解算法
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 結束位置 * @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、總結:這種查找方式的使用場合為已排序的數(shù)組??梢园l(fā)現(xiàn)遞歸和循環(huán)的次數(shù)是一樣的
以上就是java 二分法的實例詳解,如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關文章
Mybatis中動態(tài)SQL,if,where,foreach的使用教程詳解
MyBatis的動態(tài)SQL是基于OGNL表達式的,它可以幫助我們方便的在SQL語句中實現(xiàn)某些邏輯。這篇文章主要介紹了Mybatis中動態(tài)SQL,if,where,foreach的使用教程,需要的朋友可以參考下2017-11-11RxJava2.x+ReTrofit2.x多線程下載文件的示例代碼
本篇文章主要介紹了RxJava2.x+ReTrofit2.x多線程下載文件的示例代碼,具有一定的參考價值,有興趣的可以了解一下2017-09-09詳解Spring Boot Oauth2緩存UserDetails到Ehcache
這篇文章主要介紹了詳解Spring Boot Oauth2緩存UserDetails到Ehcache,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-08-08Java 實現(xiàn)完整功能的學生管理系統(tǒng)實例
讀萬卷書不如行萬里路,只學書上的理論是遠遠不夠的,只有在實戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用Java實現(xiàn)一個完整版學生管理系統(tǒng),大家可以在過程中查缺補漏,提升水平2021-11-11