ProgrammingModerateProduct-company MCQ pattern

Which statement about sorting 10^6 floats with sorted() in CPython is correct?

  1. A. Timsort is O(n log n) in the worst case and close to O(n) when the input is already sorted
  2. B. It always runs in O(n) because each float comparison takes constant time
  3. C. It is quicksort and so is O(n squared) in the worst case
  4. D. It is O(n log n) only when the input has no duplicates

Correct answer: A. Timsort is O(n log n) in the worst case and close to O(n) when the input is already sorted

sorted() uses Timsort, which detects existing runs and merges them, so already-sorted input takes about n comparisons. The worst case is still n log n comparisons.

More Python Programming questions

Exams that ask this topic