Data Structures19 sections · 729 units
Open in Course

The K-th Element Problem

Finding rank k

Given an array, find the k-th largest (or smallest) element. Approaches:

1.1. Sort: O(nlogn)O(n \log n) time, O(1)O(1) extra space.

2.2. Min-heap of size kk: O(nlogk)O(n \log k) time, O(k)O(k) space.

3.3. Quickselect: O(n)O(n) average, O(n2)O(n^2) worst. The heap approach is effective and reliable. For k-th largest, maintain a min-heap of the kk largest elements seen so far.