1, the premise: The premise of binary lookup is to find the array must be sorted, our implementation here defaults to ascending
2, principle: The array is divided into three parts, followed by the median (the so-called median is the value of the middle of the array) before the median, the median value of the value to be looked for and the value of the group, if less than the median is found in front of the median value, if the value is greater than the median is found after the median, equal to the median when directly returned. Then there is a recursive process, which continues to decompose the first half or the second half into three parts. May be described is not very clear, if not understand can go online to find. It can be seen from the description that this algorithm is suitable for the recursive implementation, can be used recursively can be implemented with the loop. So our implementation is divided into recursion and loop, can be based on the code to understand the algorithm
3, implementation: code as follows
Package org.cyxl.algorithm.search;
/** * Binary search * @author CYXL * */public class BinarySearch {private int rcount=0;
private int lcount=0;
/** * Get Recursion number * @return * * public int getrcount () {return rcount;
/** * Get the number of loops * @return/public int getlcount () {return lcount; /** * Performs a recursive binary lookup that returns the first occurrence of this value * @param sorteddata ordered array * @param start position * @param end position * @param fi Ndvalue need to find value * @return The position of the value in the array, starting from 0.
Cannot find return-1/public int searchrecursive (int[] sorteddata,int start,int end,int findvalue) {rcount++; if (start<=end) {//Intermediate position int middle= (start+end) >>1;//equivalent (Start+end)/2//Median int Middlevalue=sortedda
Ta[middle];
if (findvalue==middlevalue) {//equals median value directly returns return middle; If else if (findvalue<middlevalue) {//less than the median value, find return searchrecursive in front of the median (sorteddata,start,middle-1,findval
UE); else {//greater than median to find return searchrecursive after the median value (sorteddata,middle+1,eNd,findvalue);
} else {//could not find return-1; /** * Loops Binary lookup, returns the first occurrence of this value * @param sorteddata ordered array * @param findvalue the value to be found * @return The position of the value in the array, starting from 0.
Could not find return-1 */public int searchloop (int[] sorteddata,int findvalue) {int start=0;
int end=sorteddata.length-1;
while (start<=end) {lcount++; Intermediate position int middle= (start+end) >>1;
Equivalent to (start+end)/2//median int middlevalue=sorteddata[middle];
if (findvalue==middlevalue) {//equals median value directly returns return middle;
If else if (findvalue<middlevalue) {//less than the median value, look for end=middle-1 in front of the median value;
else {//greater than median to find start=middle+1 after the median value;
}//could not find return-1; }
}
4. Test code
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, Summary: The use of this way to find the occasion of the sorted array. You can see that recursion and loops are the same number of times.