ProgrammingHard

An algorithm does O(n) work on each of the n levels of a recursion that removes one element per level. Which total is correct?

  1. A. O(n^2), since the per-level work sums to 1 + 2 + ... + n
  2. B. O(n), since each level is linear
  3. C. O(n log n), like merge sort
  4. D. O(2^n), like naive recursion

Correct answer: A. O(n^2), since the per-level work sums to 1 + 2 + ... + n

Summing the per-level costs gives a quadratic total. Merge sort halves the input each level, which gives n log n instead.

More C Programming questions

Exams that ask this topic