Binary search algorithm is one of the more frequent algorithms used in ordered arrays, in the non-contact binary lookup algorithm, the most common approach is to traverse the array, compare each element, and its time is O (n). But the binary lookup algorithm is better because it has a lookup time of O (LGN), such as the array {1, 2, 3, 4, 5, 6, 7, 8, 9}, find element 6, executed with binary lookup algorithm, in the order of:
1. The first step is to find the intermediate element, that is, 5, because 5<6, 6 is necessarily in the array element after 5, then it is found in {6, 7, 8, 9},
2. Looking for the median of {6, 7, 8, 9}, which is 7,7>6, 6 should be in the array element to the left of 7, then only 6 is found.
The binary search algorithm is to continuously divide the array into half, and compare the intermediate elements with goal each time.
Realization of--java code with binary search method
1, premise: The premise of binary search is to find the array must be sorted, our implementation here is the default ascending
2, principle: The array is divided into three parts, in order is the median (the so-called median is the middle of the array value) before, median, median value, the value to be looked up and the median of the array to compare, if less than the median value is in front of the median value is found, if the value is greater than the median value after the Then there is a recursive procedure, in which the first half or the second part continues to be decomposed into three parts. may not be described very clearly, if you do not understand can go online to find. It can be seen from the description that this algorithm is suitable for recursive implementation, can be used recursively can be used to achieve the loop. So our implementation is divided into recursive and cyclic two, can be based on the code to understand the algorithm
3, implementation: code as follows
[Java]View PlainCopy
- Package org.cyxl.algorithm.search;
- /**
- * Two-point search
- * @author CYXL
- *
- */
- Public class BinarySearch {
- private int rcount=0;
- private int lcount=0;
- /**
- * Number of times to get recursion
- * @return
- */
- public int Getrcount () {
- return rcount;
- }
- /**
- * Number of times to get loops
- * @return
- */
- public int Getlcount () {
- return lCount;
- }
- /**
- * Performs a recursive binary lookup, returning the position where the value first appears
- * @param sorteddata sorted array
- * @param start position
- * @param End Position
- * @param findvalue need to find the value
- * @return The position of the value in the array, starting at 0. No return-1 found
- */
- public int searchrecursive (int[] sorteddata,int start,int end,int findvalue) /c4>
- {
- rcount++;
- if (start<=end)
- {
- //Middle position
- int middle= (start+end) >>1; //equivalent (Start+end)/2
- //Middle value
- int Middlevalue=sorteddata[middle];
- if (findvalue==middlevalue)
- {
- //equals median return directly
- return middle;
- }
- Else if (findvalue<middlevalue)
- {
- //Less than mid value when looking in front of the median value
- return searchrecursive (sorteddata,start,middle-1,findvalue);
- }
- Else
- {
- //greater than median is found after median value
- return searchrecursive (sorteddata,middle+1,end,findvalue);
- }
- }
- Else
- {
- //Not found
- return-1;
- }
- }
- /**
- * Loop binary lookup, returns the position of the first occurrence of the value
- * @param sorteddata sorted array
- * @param findvalue need to find the value
- * @return The position of the value in the array, starting at 0. No return-1 found
- */
- public int Searchloop (int[] sorteddata,int findvalue)
- {
- int start=0;
- int end=sorteddata.length-1;
- While (start<=end)
- {
- lcount++;
- //Middle position
- int middle= (start+end) >>1; //equivalent (Start+end)/2
- //Middle value
- int Middlevalue=sorteddata[middle];
- if (findvalue==middlevalue)
- {
- //equals median return directly
- return middle;
- }
- Else if (findvalue<middlevalue)
- {
- //Less than mid value when looking in front of the median value
- end=middle-1;
- }
- Else
- {
- //greater than median is found after median value
- Start=middle+1;
- }
- }
- //Not found
- return-1;
- }
- }
4. Test code
[Java]View PlainCopy
- 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: This way of finding the use of the situation is a sorted array. You can see that recursion and looping are the same number of times.
Algorithm of _001_ binary search algorithm