Binary Search in PHP

Binary search is a search algorithm used to find the position of a target value within a sorted array efficiently. It works by repeatedly dividing the search range in half and comparing the middle element with the target value.

How Binary Search Works

The binary search algorithm follows these steps

  • Start with the entire sorted array and set left pointer to first element, right pointer to last element.

  • Calculate the middle index as the average of left and right pointers (integer division).

  • Compare the value at the middle index with the target value.

  • If the middle value equals the target, return the index (search successful).

  • If target is greater than middle value, eliminate left half by updating left pointer to mid + 1.

  • If target is smaller than middle value, eliminate right half by updating right pointer to mid - 1.

  • Repeat until target is found or search range is empty (left > right).

  • If search range is empty, return -1 (target not found).

Binary search has O(log n) time complexity, making it highly efficient for large sorted arrays.

Method 1 Using Iteration

The iterative approach uses a while loop to repeatedly divide the search range ?

<?php
function binarySearch($arr, $target) {
   $left = 0;
   $right = count($arr) - 1;
   
   while ($left <= $right) {
      $mid = floor(($left + $right) / 2);
      
      // Check if the target value is found at the middle index
      if ($arr[$mid] === $target) {
         return $mid;
      }
      
      // If the target is greater, ignore the left half
      if ($arr[$mid] < $target) {
         $left = $mid + 1;
      }
      // If the target is smaller, ignore the right half
      else {
         $right = $mid - 1;
      }
   }
   
   // Target value not found in the array
   return -1;
}

// Example usage
$sortedArray = [2, 5, 8, 12, 16, 23, 38, 56, 72, 91];

// Search for existing value
$targetValue = 91;
$resultIndex = binarySearch($sortedArray, $targetValue);
if ($resultIndex === -1) {
   echo "Target value $targetValue not found in the array.<br>";
} else {
   echo "Target value $targetValue found at index $resultIndex.<br>";
}

// Search for non-existing value
$targetValue = 42;
$resultIndex = binarySearch($sortedArray, $targetValue);
if ($resultIndex === -1) {
   echo "Target value $targetValue not found in the array.<br>";
} else {
   echo "Target value $targetValue found at index $resultIndex.<br>";
}
?>
Target value 91 found at index 9.
Target value 42 not found in the array.

Method 2 Using Recursion

The recursive approach uses function calls to divide the search range ?

<?php
function binarySearchRecursive($arr, $target, $left, $right) {
   if ($left > $right) {
      // Target value not found in the array
      return -1;
   }
   
   $mid = floor(($left + $right) / 2);
   
   // Check if the target value is found at the middle index
   if ($arr[$mid] === $target) {
      return $mid;
   }
   
   // If the target is greater, search the right half
   if ($arr[$mid] < $target) {
      return binarySearchRecursive($arr, $target, $mid + 1, $right);
   }
   
   // If the target is smaller, search the left half
   return binarySearchRecursive($arr, $target, $left, $mid - 1);
}

// Wrapper function for the recursive binary search
function binarySearch($arr, $target) {
   $left = 0;
   $right = count($arr) - 1;
   return binarySearchRecursive($arr, $target, $left, $right);
}

// Example usage
$sortedArray = [2, 5, 8, 12, 16, 23, 38, 56, 72, 91];
$targetValue = 16;
$resultIndex = binarySearch($sortedArray, $targetValue);

if ($resultIndex === -1) {
   echo "Target value $targetValue not found in the array.";
} else {
   echo "Target value $targetValue found at index $resultIndex.";
}
?>
Target value 16 found at index 4.

Comparison

Aspect Iterative Recursive
Memory Usage O(1) O(log n)
Implementation Uses loop Uses function calls
Performance Slightly faster Function call overhead

Conclusion

Binary search efficiently finds elements in sorted arrays with O(log n) complexity. The iterative approach is generally preferred for better memory usage, while the recursive method offers cleaner, more intuitive code.

Updated on: 2026-03-15T10:25:48+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements