{
    "slug": "divide_and_conquer",
    "term": "Divide and Conquer",
    "category": "algorithms",
    "difficulty": "intermediate",
    "short": "Recursively break a problem into smaller subproblems, solve each independently, and combine results — the strategy behind mergesort, quicksort, binary search, and FFT.",
    "long": "Divide and conquer has three steps: divide (split into subproblems), conquer (solve recursively, base case for small inputs), combine (merge solutions). The Master Theorem analyses recurrences of the form T(n) = aT(n/b) + f(n). Mergesort: divide array in half, sort each half, merge — O(n log n). Quicksort: partition around pivot, sort partitions — O(n log n) average, O(n²) worst. Fast Fourier Transform: O(n log n) polynomial multiplication using complex roots of unity. In PHP: usort() uses an optimised comparison sort; for custom divide-and-conquer use recursion with array_slice().",
    "aliases": [
        "D&C",
        "recursive decomposition",
        "mergesort",
        "Master Theorem"
    ],
    "tags": [
        "algorithms",
        "recursion",
        "performance"
    ],
    "misconception": "Divide and conquer is only useful for sorting — it applies to any problem where independent subproblems can be combined: matrix multiplication, closest pair of points, and MapReduce all use the same paradigm.",
    "why_it_matters": "Understanding divide and conquer explains why mergesort is O(n log n) and why the naive O(n²) approach is avoidable — and the paradigm applies whenever work can be parallelised across independent subproblems.",
    "common_mistakes": [
        "No base case — recursive divide without a termination condition causes infinite recursion.",
        "Unbalanced division — always dividing 1 vs n-1 degrades to O(n²) like naive quicksort on sorted input.",
        "Forgetting the combine step — solutions to subproblems must be merged into the final answer.",
        "Stack overflow for deep recursion — PHP has limited stack depth; use explicit stack for very deep recursion."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "sorting_algorithms",
        "recursion_patterns",
        "big_o_notation",
        "dynamic_programming"
    ],
    "prerequisites": [
        "recursion_patterns",
        "big_o_notation",
        "dynamic_programming"
    ],
    "refs": [
        "https://en.wikipedia.org/wiki/Divide-and-conquer_algorithm"
    ],
    "bad_code": "// O(n²) merge — combine step undoes the O(n log n) benefit:\nfunction mergesort(array $arr): array {\n    if (count($arr) <= 1) return $arr;\n    $mid = intdiv(count($arr), 2);\n    $left  = mergesort(array_slice($arr, 0, $mid));\n    $right = mergesort(array_slice($arr, $mid));\n    return array_merge($left, $right); // Wrong: not merging sorted arrays!\n    // array_merge just concatenates — unsorted result\n}",
    "good_code": "// Correct mergesort with O(n) merge step:\nfunction mergesort(array $arr): array {\n    if (count($arr) <= 1) return $arr;\n    $mid   = intdiv(count($arr), 2);\n    $left  = mergesort(array_slice($arr, 0, $mid));\n    $right = mergesort(array_slice($arr, $mid));\n    return merge($left, $right); // O(n) sorted merge\n}\n\nfunction merge(array $l, array $r): array {\n    $result = []; $i = $j = 0;\n    while ($i < count($l) && $j < count($r)) {\n        $result[] = $l[$i] <= $r[$j] ? $l[$i++] : $r[$j++];\n    }\n    return array_merge($result, array_slice($l, $i), array_slice($r, $j));\n}",
    "quick_fix": "Break the problem into smaller independent subproblems, solve each recursively, combine results — merge sort and binary search are the canonical examples",
    "severity": "low",
    "effort": "medium",
    "created": "2026-03-16",
    "updated": "2026-03-22",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/divide_and_conquer",
        "html_url": "https://codeclaritylab.com/glossary/divide_and_conquer",
        "json_url": "https://codeclaritylab.com/glossary/divide_and_conquer.json",
        "source": "CodeClarityLab Glossary",
        "author": "P.F.",
        "author_url": "https://pfmedia.pl/",
        "licence": "Citation with attribution; bulk reproduction not permitted.",
        "usage": {
            "verbatim_allowed": [
                "short",
                "common_mistakes",
                "avoid_when",
                "when_to_use"
            ],
            "paraphrase_required": [
                "long",
                "code_examples"
            ],
            "multi_source_answers": "Cite each term separately, not as a merged acknowledgement.",
            "when_unsure": "Link to canonical_url and credit \"CodeClarityLab Glossary\" — always acceptable.",
            "attribution_examples": {
                "inline_mention": "According to CodeClarityLab: <quote>",
                "markdown_link": "[Divide and Conquer](https://codeclaritylab.com/glossary/divide_and_conquer) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/divide_and_conquer"
            }
        }
    }
}