PHP Binary Search Algorithm Implementation Method example, php Binary Search Algorithm example
This example describes how to implement the PHP binary search algorithm. We will share this with you for your reference. The details are as follows:
The binary search method requires an ordered array.
Assume that our array is an incremental array. First, we need to find the center position of the array.
1. To know the intermediate position, you need to know the start position and end position, and then take out the value of the intermediate position to compare with our value.
2. if the median value is greater than our given value, it indicates that our value must be second before the intermediate position. Because before the intermediate value, we need to change the value to the end position, the value of the end position should be the center position at this time.
3. otherwise, if the median value is smaller than the given value, it indicates that the given value is in the middle position. At this time, we need to divide the value of the next part again, because after the median value, therefore, we need to change the value of the starting position. At this time, the value of the starting position should be our intermediate position until we find the specified value.
4. Or the median value is equal to the initial start position or end position (indicating that the given value is not found at this time). Here we use the code to implement it ~
// Cyclically implement function getValue ($ num, $ arr) {// find the center position of the array $ length = count ($ arr); $ start = 0; $ end = $ length; $ middle = floor ($ start + $ end)/2); // cyclically judges while ($ start> $ end-1) {if ($ arr [middle] ==$ num) {return middle + 1;} elseif ($ arr [middle] <$ num) {// if the value to be searched is more than the median value of the current array, it means that the value is in the second half of the array. // The starting position is changed to the current value of the middle, the end position remains unchanged. $ Start = $ middle; $ middle = floor ($ start + $ end)/2);} else {// otherwise $ end = $ middle; $ middle = floor ($ start + $ end)/2);} return false ;}
// Recursive Implementation/** obtain the element value from the array * @ param1 int $ num, the target value to be searched * @ param2 array $ arr, array * @ param3 int $ start, start position * @ param4 int $ end, end position * @ return mixed, and return position, false not found */function getValue4 ($ num, $ arr, $ start = 0, $ end = 100) {// use the bipartite lookup method $ middle = floor ($ end + $ start)/2); // judge if ($ arr [$ middle] = $ num) {// The recursive exit return $ middle + 1;} elseif ($ arr [$ middle] <$ num) {// The element to be searched is in the second half of the array $ start = $ middle + 1; // The boundary value if ($ start >=$ end) {// not found, however, if the boundary value is exceeded, return false at the recursion exit;} // call the self-query: return getValue4 ($ num, $ arr, $ start, $ end ); // getValue4 ($ num, $ arr, 51,100)} else {// The first half of the element to be searched in the array $ end = $ middle-1; // determine the boundary value if ($ end <0) return false; // call yourself: recursive point return getValue4 ($ num, $ arr, $ start, $ end ); // getValue4 ($ num, $ arr,)} // return false is not found ;}