ProgrammingHard

To find the k-th largest of 10^6 records by score, which approach is correct and fast enough?

  1. A. Full bubble sort on all records, O(N^2)
  2. B. Nested loops comparing every pair, O(N^2)
  3. C. Quickselect or a min-heap of size k, about O(N log k) or O(N) on average
  4. D. Insertion sort on the whole array, O(N^2) moves

Correct answer: C. Quickselect or a min-heap of size k, about O(N log k) or O(N) on average

A heap of size k or quickselect avoids the quadratic work. At N = 10^6, N^2 is about 10^12 operations and is far too slow within a typical limit.

More C Programming questions

Exams that ask this topic