ProgrammingVery hard

An in-place sort on 10^6 ints must not use more than a few kilobytes of extra memory. Which algorithm fits?

  1. A. Merge sort with a full temporary array, which uses O(n) extra memory
  2. B. Counting sort with a count array of size 10^9
  3. C. Heapsort, which is O(n log n) time and O(1) extra memory
  4. D. Copying the array into a list and sorting the list

Correct answer: C. Heapsort, which is O(n log n) time and O(1) extra memory

Heapsort works in place with O(1) extra memory and O(n log n) time. Merge sort needs an O(n) buffer, which is about 4 MB here.

More C Programming questions

Exams that ask this topic