Skip to content
IT glossary
AlgorithmsUpdated December 3, 2026

Divide and conquer

Разделяй и властвуй

This strategy solves a problem by splitting it into smaller problems of the same kind, solving those separately, and using their answers to build the final result.

The idea

Suppose you need to sort a deck of numbered cards. Split it in two, sort each pile, and then take the smaller of the two top cards until one pile runs out. Append the cards left in the other pile. Sorting each pile is the same problem on a smaller input, so you can keep splitting until there is only one card per pile. This is the idea behind merge sort, a classic divide-and-conquer algorithm.

How it works

  • Divide: turn the input into smaller instances of the same problem. The split doesn't have to be even, but every subproblem must be smaller.
  • Conquer: solve those instances the same way. Stop splitting at a base case, such as an empty array or a one-element array, where the answer is immediate.
  • Combine: use the smaller answers to produce the answer for the original input. The cost of this step depends on the algorithm: merging sorted arrays takes a pass over both arrays, but some algorithms need little or no work after the subproblems are solved.

Example: merge sort

sort(a):
    if length(a) <= 1:
        return a
    left, right = split_into_halves(a)
    return merge(sort(left), sort(right))

merge repeatedly selects the smaller of the two next available elements, then appends whatever remains when one side runs out. For example, merging [2, 7] with [1, 5] gives [1, 2, 5, 7]. Concatenating them instead would leave 1 after 7, so two sorted halves alone aren't enough.

Merge sort has about log₂ n levels for an input of n elements. The merges at any one level process n elements in total, so the running time is Θ(n log n). A typical array implementation uses an O(n) auxiliary buffer, and the recursive version also needs O(log n) call stack space.

Where it helps and where it doesn't

Quicksort puts the work on the other side of the recursive calls: it first partitions the data around a pivot, so the resulting sections can be sorted without a merge. Repeatedly choosing a pivot that leaves one section nearly as large as the input can lead to Θ(n²) running time. Splitting an input is not enough to make the algorithm fast.

Recursion is an implementation choice, not a requirement. Bottom-up merge sort starts with single-element runs and repeatedly merges adjacent pairs. Conversely, not every recursive algorithm is divide and conquer. Binary search is often grouped with it informally, but it follows only one smaller subproblem and has no answers to combine. If the same subproblems keep recurring, consider dynamic programming to reuse their results instead of recomputing them.

In a coding interview

State what each call returns, which inputs it handles directly, and why the split and combine steps preserve correctness. Then count both the recursive calls and the work outside them. For merge sort on a power-of-two input, T(n) = 2T(n/2) + Θ(n): there are two half-size calls, plus linear work to merge. Reporting O(log n) just because that is the recursion depth confuses stack depth with running time.