Welcome to today's lesson! We're diving into Binary Search, a clever technique for locating specific elements within a sorted list. By repeatedly dividing the search interval in half, we can find the targeted item. It's akin to flipping through a dictionary — instead of going page by page, you'd start in the middle and then narrow down the section in half until you find your desired word.
Binary Search begins at the midpoint of a sorted list, halving the search area at each step until it locates the target. For example, if we were to look for the number 9
in a sorted list ranging from 1
to 10
, we would start at the midpoint, 5
. Since 9
is larger than the midpoint, we discard all numbers less than or equal to 5
and focus on the second half, leaving us with numbers 6
to 10
.
Next, we find the midpoint of this subset, which is 8
. Since 9
is greater than 8
, we eliminate numbers 6
through 8
, leaving us with just 9
and 10
. Now, the midpoint of this final subset is 9
, which matches our target. Thus, we've located the number 9
after three steps, demonstrating how Binary Search efficiently narrows the search range with each comparison.
Let's see how Binary Search can be implemented in PHP, taking a recursive approach. This process involves a function calling itself — with a base case in place to prevent infinite loops — and a recursive case to solve smaller parts of the problem.
php1function binarySearch(array $arr, int $start, int $end, int $target): int { 2 if ($start > $end) return -1; // Base case 3 $mid = $start + (int)(($end - $start) / 2); // Find the midpoint 4 if ($arr[$mid] == $target) return $mid; // Target found 5 if ($arr[$mid] > $target) // If the target is less than the midpoint 6 return binarySearch($arr, $start, $mid - 1, $target); // Search the left half 7 return binarySearch($arr, $mid + 1, $end, $target); // Else, search the right half 8}
Within this code, the base case is defined first. If the start index is greater than the end index, it indicates the search area is exhausted, resulting in a -1
return. The code then locates the midpoint. If the midpoint equals our target, it’s returned. Depending on whether the target is less or more than the midpoint, the search continues within the left or right half, respectively.
Let's analyze the time complexity of Binary Search, which measures how much the time an algorithm takes increases with the input size. Notably, Binary Search halves the list at every step, necessitating log(n)
steps for an array of size n
. Therefore, the time complexity of Binary Search is O(log n).
Both the recursive and iterative versions of the Binary Search algorithm have a time complexity of O(log(n))
, which makes them both very efficient.
In PHP, recursion uses the stack to store function calls. As every recursive call adds a layer onto the system call stack, there is a risk of exceeding the recursion limit (ini_get('max_execution_depth')
) and causing a stack overflow if it recurses too deeply.
Conversely, iterative solutions typically use fewer resources, as they do not incur the overhead from multiple function calls. Moreover, iterative methods often help prevent issues related to recursion depth and stack overflow. We'll discuss this iterative approach in the practice exercises.
Finally, the choice between recursion and iteration can depend on the specifics of the problem being tackled, the performance characteristics of the specific system you're working on, and personal or team preferences. Both methods have their place in a programmer's toolkit.
Binary Search is an intelligent method for locating specific items within a sorted list. By repeatedly narrowing down the search area, it finds the target until the search area is reduced to zero.
Finally, the key to mastering these concepts lies in practice. Buckle up for upcoming practice exercises involving Binary Search! Starting with straightforward tasks, we will gradually navigate toward more complex problems that showcase the strength of Binary Search. See you soon!