← Visualizers
Two Pointers · Partitioning LeetCode 215

Kth Largest Element in an Array / visualized

Quickselect, not a heap — because the point of this problem is partitioning. One Lomuto sweep with pivot at the end and two pointers (i, the boundary of the “less than pivot” prefix, and j, the reader) drops the pivot at its final sorted index. Compare that index to target = n − k and you know which side the answer is on, so you recurse into one side and throw the other away forever. That discard is the whole algorithm; everything greyed out below is work you never do.

Execution

idle
Press Run to begin.
0 / 0
Speed

Java · running line

pivot i — < pivot boundary j — reader final sorted position discarded — never read again
Average O(n) — each partition is expected to halve the live range, and n + n/2 + n/4 + … = 2n. Worst case is O(n²) when the pivot is always extreme (run the already-sorted example and watch the range shrink by one); a randomised pivot makes that vanishingly unlikely. A size-k heap instead gives a guaranteed O(n log k) with O(k) space, and — unlike quickselect — never reorders the input.