Machine-translated by Google. Questions are authored in English.
Top

Discussion

What is the space complexity of merge sort algorithm ?

  • A.O(1)
  • B.O(log n)
  • C.O(n)
  • D.O(n log n)

Answer: C

The space complexity of merge sort is O(n).

The standard array merge sort copies the two halves into temporary arrays to merge them, which needs auxiliary space proportional to n. The recursion stack adds only O(log n), so the total is dominated by the O(n) buffer.

O(1) extra space is achieved by in-place sorts such as heapsort. O(log n) is roughly what quick sort needs for its recursion. O(n log n) is merge sort's running time, not its space.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.