ProgrammingModerate
For N = 10^6 integers in the range 0 to 10^6, which sorting method is correct and fast enough?
- A. Counting sort with an array of 10^6 + 1 counts, O(N + K) time
- B. Bubble sort, O(N^2) time, about 5 x 10^11 steps
- C. Insertion sort, O(N^2) time with memmove on every shift
- D. Selection sort, O(N^2) time
Correct answer: A. Counting sort with an array of 10^6 + 1 counts, O(N + K) time
A count array of size K = 10^6 + 1 gives linear time, and the sort is stable if done right. The three comparison sorts are quadratic here.
More C Programming questions
- A queue is needed for 10^5 operations on an array. Which dequeue is correct and fast enough?
- Growing a dynamic array by a fixed 1 element every time it fills (capacity + 1) costs what in the worst case for N pushes?
- Which method is correct and fast enough for answering 10^5 range-sum queries on an array of 10^5 integers?
- Which method is correct and fast enough for n choose r modulo p = 10^9 + 7 for 10^6 queries with n up to 10^6?
- To compute the sum of 1 through n = 10^9 without overflow in C, which code is correct?
- Which method finds all primes up to 10^6 in time that is correct and fast enough?