Binary search is a highly efficient algorithm used to find a specific item in a sorted list. Unlike linear search which checks every element one by one, binary search works by repeatedly dividing the search space in half. This makes it much faster, especially for large datasets. Let's see how it works with an example where we search for the number 23 in this sorted array.
Let's trace through the first step of binary search. We start with the entire array from index 0 to 9. We calculate the middle index as 4. The element at index 4 is 16. We compare our target 23 with 16. Since 23 is greater than 16, we know our target must be in the right half of the array. So we eliminate the left half including the middle element.
Now we move to step 2. Since we eliminated the left half, our new search space is from index 5 to 9. We calculate the new middle index as 7. The element at index 7 is 56. We compare our target 23 with 56. Since 23 is less than 56, we know our target must be in the left half of our current search space. So we eliminate the right half including the middle element.
In step 3, our search space has narrowed down to just indices 5 and 6. We calculate the middle index as 5. The element at index 5 is 23, which exactly matches our target! We have successfully found the target element at index 5. This demonstrates how binary search efficiently narrows down the search space with each step, finding the target in just 3 comparisons instead of checking all 10 elements.
Binary search is one of the most fundamental algorithms in computer science. It efficiently finds a target value in a sorted array by repeatedly dividing the search space in half. This divide-and-conquer approach makes it much faster than linear search, especially for large datasets. Let's see how it works with an example where we search for the value 20 in this sorted array.
Let's trace through the binary search algorithm step by step. We start by setting low pointer to index 0 and high pointer to index 9. Then we calculate the middle index as 4. The middle element is 15, which is less than our target 20. Since the array is sorted, we know our target must be in the right half, so we move the low pointer to mid plus 1.
Now our search space is reduced to indices 5 through 9. We calculate the new middle index as 7. The element at index 7 is 28, which is greater than our target 20. So we move the high pointer to mid minus 1, which is index 6. Now the middle index is 5, and the element at index 5 is exactly 20 - our target! Binary search successfully found the target in just 3 steps.
Here's the complete implementation of binary search in Python. We start by initializing low to 0 and high to the last index. In each iteration, we calculate the middle index using integer division. We then compare the middle element with our target. If they match, we return the index. If the middle element is smaller than the target, we search the right half by moving low to mid plus 1. Otherwise, we search the left half by moving high to mid minus 1. We continue until we find the target or the search space is exhausted.
Binary search has excellent time complexity of O(log n), meaning it can search through a million elements in just about 20 steps. The space complexity is O(1) as it only uses a constant amount of extra memory. The key requirement is that the array must be sorted. Here's the implementation: we maintain low and high pointers, calculate the middle, compare with target, and adjust our search space accordingly. This logarithmic efficiency makes binary search ideal for large datasets where linear search would be too slow.