What is the space complexity of the quicksort algorithm ?
2024
What is the space complexity of the quicksort algorithm ?
- A.
O(1)
- B.
O(log n)
- C.
O(n)
- D.
O(n2)
Attempted by 83 students.
Show answer & explanation
Correct answer: B
The space complexity of Quicksort is determined by the maximum depth of its recursive call stack. In the average case, Quicksort partitions the array into two roughly equal halves at each step, resulting in a recursion depth of O(log n). Since the algorithm sorts in-place with only constant extra space for variables, the dominant factor is this stack depth. Therefore, the correct answer is O(log n). Option A (O(1)) is incorrect because Quicksort requires stack space for recursion, unlike iterative algorithms. Option C (O(n)) represents the worst-case scenario where partitions are highly unbalanced, leading to a recursion depth of n. Option D (O(n^2)) refers to time complexity in the worst case, not space.