ProgrammingHard

A quicksort with a poor pivot choice on already sorted input of n elements has what time?

  1. A. O(n log n), because the input is sorted
  2. B. O(n), because the pivot is always the median
  3. C. O(log n), because the recursion halves
  4. D. O(n^2), because each partition splits off only one element

Correct answer: D. O(n^2), because each partition splits off only one element

Each partition removes one element, which makes n levels of O(n) work. Random or median pivots avoid this case.

More C Programming questions

Exams that ask this topic