Divide & Conquer in Searching
In the previous lesson, we learned the core idea of the Divide and Conquer strategy.
Now it is time to see how this strategy works specifically in searching problems.
Searching is one of the most common tasks in computer science — finding a value, a record, or a position.
Why Searching Needs Optimization
Imagine searching for a name in a list of one million records.
If you check each record one by one, the process becomes slow and inefficient.
This is where Divide and Conquer completely changes performance.
Linear Search vs Divide & Conquer Search
Linear Search examines elements one after another.
Its time complexity is:
O(n)
This means the time grows directly with the size of the data.
Divide and Conquer searching, on the other hand, cuts the problem size at every step.
Binary Search – The Best Example
Binary Search is the most popular example of Divide and Conquer in searching.
It works only on sorted data.
At each step:
- The middle element is checked
- Half of the data is discarded
- The search continues in one half
Because the data size is halved every time, Binary Search runs in O(log n).
Binary Search – Step-by-Step Thinking
Let us break the logic mentally.
Suppose we are searching for the number 42 in a sorted list.
Instead of scanning from the start, we immediately jump to the middle.
If the middle value is larger, we discard the right half.
If it is smaller, we discard the left half.
This is Divide and Conquer in action.
Binary Search – Recursive Implementation
def binary_search(arr, left, right, target):
if left > right:
return -1
mid = (left + right) // 2
if arr[mid] == target:
return mid
elif arr[mid] > target:
return binary_search(arr, left, mid - 1, target)
else:
return binary_search(arr, mid + 1, right, target)
numbers = [3, 7, 15, 21, 42, 55, 68]
index = binary_search(numbers, 0, len(numbers) - 1, 42)
print("Found at index:", index)
Each recursive call reduces the problem size by half.
Why Binary Search Is Efficient
Binary Search reduces the search space exponentially.
For example:
- 1,000,000 elements → ~20 comparisons
- 1,000 elements → ~10 comparisons
This dramatic improvement is why Divide and Conquer is preferred for large datasets.
When Divide & Conquer Searching Fails
Divide and Conquer searching does not always work.
Binary Search fails when:
- The data is not sorted
- The dataset changes frequently
- Random access is not available
In such cases, Linear Search may still be required.
Real-World Example
Search engines use Divide and Conquer ideas internally.
Indexes are created so that large portions of irrelevant data are ignored immediately.
This is why search results appear almost instantly.
Mini Practice
Think about the following:
- Why does Binary Search require sorted data?
- Can Divide and Conquer be used in text search?
Exercises
Exercise 1:
What is the main advantage of Divide and Conquer in searching?
Exercise 2:
Why is Binary Search faster than Linear Search?
Exercise 3:
What condition must be satisfied before using Binary Search?
Quick Quiz
Q1. What is the time complexity of Binary Search?
Q2. Can Divide and Conquer be applied to unsorted data?
In the next lesson, we will apply Divide and Conquer to sorting problems and see how it leads to powerful algorithms like Merge Sort.