Divide and conquer
Solving a problem by splitting it into smaller problems of the same kind, solving each one and combining the results.
Example
To sort 8 numbers, merge sort splits them into 2 groups of 4, then 4 of 2, sorts them and merges them back.
Test yourself: Divide and conquer
Divide and conquer
To build a 1,000-piece puzzle as a group, each person builds a corner and then they are joined. That is ___ and conquer.
4 people build 250 pieces each separately, and at the end the 4 parts are joined.
Divide and conquer
If the subproblems repeat and depend on each other, as in Fibonacci, what happens with divide and conquer?
Fibonacci of 30 split this way makes over 1 million calls for 30 distinct values. Dynamic programming fits better there.
Divide and conquer
Sorting 2 halves of 50 with bubble sort and merging them costs about 2 × 50² + 100 = ___ steps, against 10,000 without splitting.
2 × 2,500 + 100 = 5,100: almost half. It is still O(n²), but with a smaller constant.
0/3
â–¶ Play