ProgrammingVery hard
An in-place sort on 10^6 ints must not use more than a few kilobytes of extra memory. Which algorithm fits?
- A. Merge sort with a full temporary array, which uses O(n) extra memory
- B. Counting sort with a count array of size 10^9
- C. Heapsort, which is O(n log n) time and O(1) extra memory
- 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
- A quicksort with a poor pivot choice on already sorted input of n elements has what time?
- A hash table with 2^20 slots holds 2 x 10^5 keys and uses a good hash. What is the expected cost of a successful lookup?
- For n = 10^5, which expression correctly counts the pairs (i, j) with i < j where a[i] + a[j] equals a target, in O(n log n) time?
- A recursive function is called with depth 10^6 in C on a typical 8 MB stack with 64-byte frames. Is this safe?
- A bit set of 64 flags is packed in one unsigned long long. Which expression tests flag 17?
- What is the first printed value of this loop? for (int i = 3; i > 0; i--) printf("%d", i);