Example analysis of binary search algorithm implemented by PHP

  • 2021-08-28 19:32:47
  • OfStack

In this paper, the 2-point search algorithm realized by PHP is described as an example. Share it for your reference, as follows:

The 2-point lookup method requires the array to be an ordered array

Suppose our array is an incremental array, first we need to find the middle position of the array.

1. To know the middle position, we need to know the starting position and the ending position, and then take the value of the middle position and compare it with our value.
2. If the middle value is greater than our given value, it means that our value is before the middle position, and then we need to change 2 points again. Because before the middle, the value we need to change is the value of the end position, and the value of the end position should be our middle position at this time.
3. On the contrary, if the intermediate value is less than our given value, it means that the given value is after the intermediate position. At this time, we need to divide the value of the last part by 2 points again. Because after the intermediate value, the value we need to change is the value of the starting position, and the value of the starting position should be our intermediate position at this time until we find the specified value.
4. Or the intermediate value is equal to the initial starting position, or the end position (at this time, the given value is not found), let's implement it in code ~


// Loop implementation 
function getValue($num,$arr)
{
// Find the middle position of the array 
$length=count($arr);
$start=0;
$end=$length;
$middle=floor(($start+$end)/2);
// Cyclic judgment 
while($start>$end-1)
{
if($arr[middle]==$num)
{
return middle+1;
}elseif($arr[middle]<$num)
{
// If the value you are looking for is typed more than the middle value of the current array, it means that the value is in the second half of the array 
// So the starting position becomes the current middle The value of, end The position remains unchanged. 
$start=$middle;
$middle=floor(($start+$end)/2);
}else{
// Conversely 
$end=$middle;
$middle=floor(($start+$end)/2);
}}
return false;
}


// Loop implementation 
function getValue($num,$arr)
{
// Find the middle position of the array 
$length=count($arr);
$start=0;
$end=$length;
$middle=floor(($start+$end)/2);
// Cyclic judgment 
while($start>$end-1)
{
if($arr[middle]==$num)
{
return middle+1;
}elseif($arr[middle]<$num)
{
// If the value you are looking for is typed more than the middle value of the current array, it means that the value is in the second half of the array 
// So the starting position becomes the current middle The value of, end The position remains unchanged. 
$start=$middle;
$middle=floor(($start+$end)/2);
}else{
// Conversely 
$end=$middle;
$middle=floor(($start+$end)/2);
}}
return false;
}

For more readers interested in PHP related contents, please check the topics of this site: "PHP Data Structure and Algorithm Tutorial", "php Programming Algorithm Summary", "php String (string) Usage Summary", "PHP Array (Array) Operation Skills Complete Book", "PHP Common Traversal Algorithms and Skills Summary" and "PHP Mathematical Operation Skills Summary"

I hope this article is helpful to everyone's PHP programming.


Related articles: