Based on the implementation of the array binary search algorithm, the array binary algorithm
Implementation of an array Binary Search Algorithm
Binary Search
Search
Algorithm
Zhao Zhenjiang
Binary Search is also called semi-query,Advantages
Yescompare timesLess
, Search speedFast
, AverageGood performance
; ItsDisadvantages
Yes. The table to be queried isOrdered table
And insertion and deletion are difficult. Therefore, the half-fold lookup method is suitable for searching frequently ordered lists without frequent changes.First
Suppose that the elements in the table are arranged in ascending order,Compare the keywords recorded in the middle of the table with the search keywords
, If bothEqual
.Successful
Otherwise, the table is recorded using the intermediate positionSplit into two subtables:
, IfThe keyword of the intermediate position record is greater than the Search Keyword
, Then proceedSearch for the previous sub-table
OtherwiseFind the next sub-table
.Repeat the above process
Until the matching record is found to make the search successful, or until the sub-table does not exist, the search fails.
A picture to illustrate the process
Binary Search for Common versions
The time complexity isO(N)
// A common Binary Search Method Based on arrays. Non-recursive public static int binaryChop (int [] arr, int n) {int low = 0; // indicates the left boundary of the array, or you can declare the left boundary after the split. Int up = arr. length; // indicates the right boundary of the array, or the right boundary after the split, you can declare it as right. Int mid = 0; // The center value of the locked array // start binary search while (low <up) {// loop end condition. When the left boundary exceeds the right boundary, that is to say, the target mid = (low + up)/2; // The middle value is not found. Don't ask me why I think (low + up)/2 myself! V_V if (n <arr [mid]) // if the median value is greater than the target value, cut down the right part of the array. Up = mid-1; else if (n> arr [mid]) // if the median value is smaller than the target value, cut down the left part of the array. Low = mid + 1; if (arr [mid] = n) // coincidentally, this is the middle value. (This is lucky, most of it depends on, finally found you) return mid;} return NO_VALUE ;}
Binary Search Time of recursive versions
Complexity isO(logN)
// Binary Search Method Based on array, recursive version even! Public static int binaryChopRecursive (int [] arr, int n, int low, int up) {int mid = (low + up)/2; // recursive end base value (do not ask what the base value is. This is a term. Actually, recursion always has an end condition) if (low> up) return NO_VALUE; if (n <arr [mid]) // if the median value is greater than the target value, cut down the right part of the array. Up = mid-1; else if (n> arr [mid]) // if the median value is smaller than the target value, cut down the left part of the array. Low = mid + 1; if (arr [mid] = n) // coincidentally, this is the middle value. (This is lucky, most of it depends on, finally, I found you.) return mid; return binaryChopRecursive (arr, n, low, up );}
Complete code, Search class and a test class
Search.java
Package Search;/***** @ author River (Zhao Zhenjiang) **/public class Search {// defines a static variable, public static final int NO_VALUE =-1; // -------------------------------------------------------- // ---------------- binary search method not found! ---------------------- // ------------------------------------------------ // The binary search method is used to quickly index a value in a sequence. The name is very interesting. binary: binary chop: Split, cut, this is an image of __^ // -------------------------------------------- // an ordinary Binary Search Method Based on arrays, non-recursive public static int binaryChop (int [] arr, int n) {int low = 0; // indicates the left boundary of the array or the left boundary after the split. You can also declare it as left. Int up = arr. length; // indicates the right boundary of the array, or the right boundary after the split, you can declare it as right. Int mid = 0; // The center value of the locked array // start binary search while (low <up) {// loop end condition. When the left boundary exceeds the right boundary, that is to say, the target mid = (low + up)/2; // The middle value is not found. Don't ask me why I think (low + up)/2 myself! V_V if (n <arr [mid]) // if the median value is greater than the target value, cut down the right part of the array. Up = mid-1; else if (n> arr [mid]) // if the median value is smaller than the target value, cut down the left part of the array. Low = mid + 1; if (arr [mid] = n) // coincidentally, this is the middle value. (This is lucky, most of it depends on, finally, I found you.) return mid;} return NO_VALUE;} // Binary Search Method Based on array, recursive version even! Public static int binaryChopRecursive (int [] arr, int n, int low, int up) {int mid = (low + up)/2; // recursive end base value (do not ask what the base value is. This is a term. Actually, recursion always has an end condition) if (low> up) return NO_VALUE; if (n <arr [mid]) // if the median value is greater than the target value, cut down the right part of the array. Up = mid-1; else if (n> arr [mid]) // if the median value is smaller than the target value, cut down the left part of the array. Low = mid + 1; if (arr [mid] = n) // coincidentally, this is the middle value. (This is lucky, most of it depends on, finally found you) return mid; return binaryChopRecursive (arr, n, low, up);} // else // ------------------- The end Binary Search Method --------------- // ----------- The test class is BinaryChopTest. java -----------//----------------------------------------------}
BinaryChopTest.java
Package SearchTests; public class BinaryChopTest {// test the binary search method public static void main (String [] args) {// Initialize an array, note that an ordered array int [] myArr = {1, 2, 3, 4, 5, 6, 7, 8}; // defines two result variables. Here, the value to be searched is directly substituted into the function, if you are an obsessive-compulsive disorder, you can also use the category class to input pai_^ int result1 = Search from the keyboard. binaryChop (myArr, 3); int result2 = Search. binaryChopRecursive (myArr, 8, 0, myArr. length); if (result1! =-1 & result2! =-1) {System. out. println ("search successful! "+ MyArr [result1] +" in the array "+ result1 +" location "); System. out. println (" recursive version, search successful! "+ MyArr [result2] +" in the array "+ result2 +" location ");} else System. out. println (" not found! ");}}