ProgrammingModerate

For N = 10^6 integers in the range 0 to 10^6, which sorting method is correct and fast enough?

  1. A. Counting sort with an array of 10^6 + 1 counts, O(N + K) time
  2. B. Bubble sort, O(N^2) time, about 5 x 10^11 steps
  3. C. Insertion sort, O(N^2) time with memmove on every shift
  4. 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

Exams that ask this topic