An Implementation Of Array Binary Search In PHP

19th June 2018

I have been doing some reading and watching lectures of programming theory recently and I was reminded of this algorithm I learned about in university. Binary searching an array is a divide and conquer algorithm that takes an array and searches for a value in that array by splitting the array into halves. The algorithm works like this.

  • Given a sorted array, find the midpoint.
  • If the value at the mid point is greater than the value being searched for then the value must be in the first half of the array.
  • If the value at the mid point is less than the value being searched for then the value must be in the first half of the array.
  • Take the half of the array in question and repeat the first step by re-pointing the mid point.
  • Repeat this until the length of the array is 1 or 0. If the array length is 1 then this is the value. If the length of the array is 0 then the value was not in the array.

There is some implementation details around ensuring that the middle value is correct and to avoid running off the end of the array.

Here is an implementation of the algorithm in PHP.

  1. /**
  2.  * Use binary search to find a key of a value in an array.
  3.  *
  4.  * @param array $array
  5.  * The array to search for the value.
  6.  * @param int $value
  7.  * A value to be searched.
  8.  *
  9.  * @return int|null
  10.  * Returns the key of the value in the array, or null if the value is not found.
  11.  */
  12. function binarySearch($array, $value) {
  13. // Set the left pointer to 0.
  14. $left = 0;
  15. // Set the right pointer to the length of the array -1.
  16. $right = count($array) - 1;
  17.  
  18. while ($left <= $right) {
  19. // Set the initial midpoint to the rounded down value of half the length of the array.
  20. $midpoint = (int) floor(($left + $right) / 2);
  21.  
  22. if ($array[$midpoint] < $value) {
  23. // The midpoint value is less than the value.
  24. $left = $midpoint + 1;
  25. } elseif ($array[$midpoint] > $value) {
  26. // The midpoint value is greater than the value.
  27. $right = $midpoint - 1;
  28. } else {
  29. // This is the key we are looking for.
  30. return $midpoint;
  31. }
  32. }
  33. // The value was not found.
  34. return NULL;
  35. }

To run some tests on this algorithm I ran the following code. All of which prints true.

  1. // Generate an array.
  2. $array = range(0, 10);
  3.  
  4. // Loop through the array, searching for each value.
  5. foreach ($array as $key => $value) {
  6. echo var_export(binarySearch($array, $value) === $value, TRUE) . PHP_EOL;
  7. }
  8.  
  9. // Search for values outside of the array.
  10. echo var_export(binarySearch($array, -1) === NULL, TRUE) . PHP_EOL;
  11. echo var_export(binarySearch($array, 11) === NULL, TRUE) . PHP_EOL;

This is a neat little algorithm that is great for searching over arrays of sorted data where the keys of the array are sequential. Much more performant than simply looping through the array to find the value.

Comments

Permalink

Why do you count right as count($array) - 1. Why do you need this -1 ?

Valeriy (Mon, 03/30/2020 - 12:27)

Permalink

This is because the count() function will return the length of the array, but if we want to get the last item in the array using this value it will be 1 item beyond the end of the array.

The array I'm creating here starts from 0 so although the length is 10, the index of the last item is 9.

philipnorton42 (Tue, 03/31/2020 - 15:17)

Add new comment

The content of this field is kept private and will not be shown publicly.