Graph Theory37 sections · 1633 units
Open in Course

Why O(n log n) Works

(Doubling argument)

Each time an element is moved from one set to another, the new set is at least as large as the old one. So the set size at least doubles. Starting from size 11, doubling kk times reaches size 2k2^k. To reach nn, you need k=log2nk = \log_2 n doublings.

An element cannot be moved more than logn\log n times. Since there are nn elements total, and each moves at most logn\log n times, total operations are O(nlogn)O(n \log n). This is the proof of the complexity bound.

Space complexity is O(n)O(n) for the data structures used.