1,996 views

Question: What is worst case space complexity of quick sort?

Everywhere it is showing O(logn).

I know that Quick sort algorithm doesn't request extra space except for stack records.

so, Space complexity will be O(logn) if partition is being done by half or n/2:n/2. So logn stack records.

But if partition is being done by ratio 1:n-1 which is worst case, wouldn't it be requesting for O(n) stack records?

yes in worst case, when tree is divided in n,n-1,n-2......1 then stack size will be maximum as O(n).

worst case space complexity of any general (naive) implementation of Quicksort is O(N) which occurs if the division is in the ratio you mentioned i.e 1:N-1 .  you can refer this https://stackoverflow.com/questions/15034897/in-place-quick-sort-has-on-or-ologn-space-complexity

It also says that the worst case space complexity can be restricted to O(logN) by implementing a Tail call on the longer portion of the array

I think you are right if the partition is always done in the ratio of 1:n-1 then the sorting algorithm would take O(n) stack records.
@surajumang08 This is the answer I was expecting.

"It also says that the worst case space complexity can be restricted to O(logN) by implementing a Tail call on the longer portion of the array"

This was introduced in 1978 by Sedgewick, Right?

Can you please elaborate the statement?

O(n2) worst case tym complexity of quick sort when input is sorted

by

### 1 comment

@ eyeamgj I am not asking for time complexity but space complexity of quick sort in worst case

With a little effort, we can limit the maximum depth of recursion to approximately Olg(n), even in the worst case.

• Then the extra space required will be O(lg(n)) — insignificant compared to the O(n) space for the array being sorted.
•  Note, however, the worst case running time remains O($n^{2}$)

voidquicksort2( T[] A, Integer left, Integer right)
{
while ( left < right )
{
q = partition(A,left,right);
if ( q-left < right-q )
{
quicksort2(A,left,q–1);  // Recursive call sorts smaller sublist
left = q + 1; // Loop back to sort larger sublist
}
else
{
quicksort2(A,q+1,right); // Recursive call sorts smaller sublist
right =q-1;     // Loop back to sort larger sublist
}
}

Here with better programming using Tail call, we can limit the O(n) stack frame in worst case. Each time quicksort2() calls itself recursively, it does so only for the smaller of the two sublists.  This means that each time the depth of recursion rises by one, the size of the input to quicksort2() is cut at least in half.  Thus the depth of recursion cannot exceed lg(n).

That is the reason behind everywhere quicksort space complexity is written as O(log n). Because we can optimize the standard quicksort using tail recursion to limit the stack frame as maximum log n, still the time complexity in worst case remains same.

You are correct.

A simple implementation of QuickSort makes two calls to itself and in worst case requires O(n) space on function call stack. The worst case happens when the selected pivot always divides the array such that one part has 0 elements and other part has n-1 elements.

With tail call optimization and other things we can reduce stack space to O(log n).

by