Binary search in PHP using divide and conquer algorithm

As the algorithm divide and conquer is also applied to algorithms that reduce each problem to only one sub-problem, the binary search algorithm is considered as divide and conquer algorithm.

In binary search the search space is reduced in each step where the sub-problem is of roughly half the original size. The binary search is also called decrease and conquer algorithm as search space is reduced in each step. In binary search the sub-problem can be solved using iterative or recursive approaches. Following is the iterative approach for solving binary search in PHP
function binarySearch($sourceArr, $keyword)
{
  $start  = 0;
  $end    = count($sourceArr) - 1;
  while ($start <= $end)
  {
    $middle = ($start + $end) / 2;
    if($keyword == $sourceArr[$middle]) {
      return $middle;
    }
    elseif($keyword < $sourceArr[$middle]) {
      $end = $middle - 1;
    }
    else{
      $start = $middle + 1;
    }
  }
  return -1;
}

$sourceArr = range(1, 100, 7);
$keyword   = $sourceArr[array_rand($sourceArr)];  
$position  = binarySearch($sourceArr, $keyword);
print_r($sourceArr);
echo ($position >= 0) ? 'Keyword ' . $keyword . ' is found at position: ' . $position :  'Your keyword is not found.';
The output of the above iterative binary search algorithm is:

Output


Array
(
    [0] => 1
    [1] => 8
    [2] => 15
    [3] => 22
    [4] => 29
    [5] => 36
    [6] => 43
    [7] => 50
    [8] => 57
    [9] => 64
    [10] => 71
    [11] => 78
    [12] => 85
    [13] => 92
    [14] => 99
)
Keyword 85 is found at position: 12


Comments