Divide and conquer is a three-step strategy that turns a large problem into several small ones of the same kind.
The General Method
A divide and conquer algorithm works in three steps. Divide the problem of size n into a smaller sub-problems of size n/b. Conquer by solving each sub-problem recursively; if it is small enough, solve it directly. Combine the sub-solutions into the solution of the original problem. The running time follows the recurrence T(n) = a·T(n/b) + f(n), where f(n) is the cost of dividing and combining, and is solved with the Master theorem of Unit I. The method suits problems in which the sub-problems are independent, so they do not overlap. It also suits parallel computers, since sub-problems can be solved at the same time.
Definition: Divide and conquer. A design technique that divides a problem into smaller independent sub-problems of the same type, solves them recursively and combines their solutions.
Binary Search
Binary search finds a key in a sorted array. Compare the key with the middle element; if equal, stop; if the key is smaller, search the left half; otherwise search the right half. Each comparison halves the range, so T(n) = T(n/2) + 1 and, by the Master theorem (a = 1, b = 2, f = 1, Case 2), T(n) = Θ(log n). The best case is 1 comparison (key in the middle), the worst case is about ⌊log₂ n⌋ + 1 comparisons, and the space is O(1) if written with a loop. For one million elements at most 20 comparisons are needed, whereas sequential search may need a million. The price is that the array must be sorted and must allow direct access.
Finding the Maximum and Minimum
The straightforward method scans the array twice, making (n − 1) + (n − 2) = 2n − 3 comparisons. The divide and conquer method splits the array into halves, finds the maximum and minimum of each half, then compares the two maxima and the two minima: 2 more comparisons. For a one-element array no comparison is needed, and for two elements one comparison. The recurrence is T(n) = 2T(n/2) + 2 with T(2) = 1, and for n a power of 2 it solves to T(n) = 3n/2 − 2 comparisons. For n = 8 that is 10 comparisons against 13 for the simple method. Both are Θ(n), but the divide and conquer version does about 25 percent fewer comparisons.
Why the Method Works
Dividing is worthwhile when combining is cheap compared with the savings made by working on small pieces. Binary search combines at no cost and discards half the data at each step; max-min saves comparisons because partial results are reused. When the sub-problems overlap, as in Fibonacci, divide and conquer recomputes work and dynamic programming is better.
Table 2.1: Divide and conquer so far
| Algorithm | Recurrence | Complexity |
|---|---|---|
| Binary search | T(n) = T(n/2) + 1 | Θ(log n) |
| Max and min | T(n) = 2T(n/2) + 2 | 3n/2 − 2 comparisons |
| Merge sort | T(n) = 2T(n/2) + n | Θ(n log n) |
| Quick sort (best) | T(n) = 2T(n/2) + n | Θ(n log n) |
| Strassen | T(n) = 7T(n/2) + n² | Θ(n^2.81) |
Example 2.1: Binary search trace
Problem: Search for 31 in the sorted array 3, 8, 12, 19, 25, 31, 40, 47 (indices 0 to 7).
Solution:
- low = 0, high = 7, mid = 3, a[3] = 19 < 31, so low = 4.
- low = 4, high = 7, mid = 5, a[5] = 31 = key: found.
- Only 2 comparisons were needed, against 6 for sequential search.
Result: Key found at index 5 after 2 probes; worst case for 8 elements is 4 probes (⌊log₂ 8⌋ + 1).
def bsearch(a, key):
low, high, probes = 0, len(a) - 1, 0
while low <= high:
mid = (low + high) // 2
probes += 1
if a[mid] == key:
return mid, probes
if a[mid] < key:
low = mid + 1
else:
high = mid - 1
return -1, probes
a = [3, 8, 12, 19, 25, 31, 40, 47]
print(bsearch(a, 31))
print(bsearch(a, 47))
print(bsearch(a, 5))
Output
(5, 2)
(7, 4)
(-1, 3)
Common mistakes
- Applying binary search to an unsorted array.
- Writing mid = (low + high)/2 without integer division, or overflowing with large indexes.
- Forgetting the base cases of one and two elements in max-min.
- Using divide and conquer where sub-problems overlap heavily.
Key takeaways
- Divide, conquer and combine give T(n) = aT(n/b) + f(n).
- Binary search is Θ(log n) and needs a sorted array with direct access.
- Max-min by divide and conquer takes 3n/2 − 2 comparisons for n a power of 2.
- The method needs independent sub-problems.
Practice questions
- State the three steps of divide and conquer.
- What is the worst-case number of comparisons in binary search?
- How many comparisons find max and min of 8 elements by divide and conquer?
Answers to the practice questions
- Divide into sub-problems, conquer them recursively, combine their solutions.
- About ⌊log₂ n⌋ + 1.
- 3 × 8/2 − 2 = 10.
Sources
- Official BCA syllabus Design and Analysis of Algorithms, course code 26BCA404DS03, Unit II
