Algorithms · Visualized

Closest Pair

Divide & Conquer

Break a problem into independent subproblems, solve each, and combine the answers.

Finds the two closest points among a set of points in a plane faster than the O(n²) brute force. Time: O(n log n) average, O(n log n), versus O(n²) brute force worst; space: O(n) — auxiliary arrays for sorting/strip.

Big-O: Classic D&C recursions: Merge Sort, Quick Sort, Maximum Subarray, Fast Power.

Closest Pair of Points

Divide & Conquer

Finds the two closest points among a set of points in a plane faster than the O(n²) brute force. Divide and conquer splits the points by a vertical line into left and right halves, recursively finds the closest pair in each half, then checks a narrow 'strip' near the dividing line for any closer cross-boundary pair.

How Closest Pair Works

  1. Sort points by x-coordinate, then split them into a left half and a right half.
  2. Recursively find the closest pair distance within the left half.
  3. Recursively find the closest pair distance within the right half.
  4. Let 'd' be the smaller of those two distances; build a narrow strip of points within distance 'd' of the dividing line.
  5. Check points within that strip (sorted by y-coordinate) against each other — thanks to geometry, each point only needs to be compared against a handful of nearby points, not all of them — and return the overall minimum distance found.

Closest Pair Worked Example

Input: [[2, 3], [12, 30], [40, 50], [5, 1], [12, 10], [3, 4]]

Sort by x, split into left and right groups. Left group finds (2,3) and (3,4) as its closest pair, distance ≈1.41. Right group finds its own closest pair. Compare against the strip near the dividing x-value for any closer cross pair. The overall closest pair across the whole set turns out to be (2,3) and (3,4).

Closest PairO(n²) brute
Find the two points with the smallest Euclidean distance.
ABCDEF
Step 1 / 20
Default Current / Compare Swap / Write Settled Active / Path Pivot / Reject Locked (hollow) Target
Closest Pair of Points: check every pair with brute force (small set).
Python · Divide & Conquer in Code
import math

def closest_pair(points):
    best = float('inf')
    n = len(points)
    for i in range(n):
        for j in range(i + 1, n):
            d = math.dist(points[i], points[j])
            if d < best:
                best = d
    return best

Behind the Scenes Execution

Press ▶ Run in the visualizer — the call stack, registers and memory will replay here in sync with the code, line by line.

Closest Pair Time & Space Complexity

BestO(n log n)
AverageO(n log n)
WorstO(n log n), versus O(n²) brute force
SpaceO(n) — auxiliary arrays for sorting/strip

When to Use Closest Pair

  • Collision detection in simulations and games (finding nearby objects fast).
  • Geographic systems: finding the two closest cities, stores, or sensors from a large set of coordinates.

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits

Keep the momentum going

Step through a related structure, pattern or course — the ideas reinforce each other and stick faster.