Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does QuickSort use O(log(n)) extra space?

I have implemented the below quicksort algorithm. Online I've read that it has a space requirement of O(log(n)). Why is this the case? I'm not creating any extra data structures.

Is it because my recursion will use some extra space on the stack? If this is the case, is it possible to do it with less memory by not having it be recursive (instead making it iterative)?

private static void quickSort (int[] array, int left, int right) {     int index = partition(array, left, right);      //Sort left half     if (left < index - 1)         quickSort(array, left, index - 1);      //Sort right half     if (index < right)         quickSort(array, index , right); }  private static int partition (int array[], int left, int right) {     int pivot = array[(left + right) / 2]; //Pick pivot point     while (left <= right) {         //Find element on left that should be on right         while (array[left] < pivot)             left++;          //Find element on right that should be on left         while (array[right] > pivot)             right--;          //Swap elements and move left and right indices         if (left <= right) {             int temp = array[left];             array[left] = array[right];             array[right] = temp;             left++;             right--;         }     }     return left; } 
like image 949
Joey Franklin Avatar asked Sep 24 '12 21:09

Joey Franklin


People also ask

Does Quicksort require extra space?

Although quicksort doesn't use auxiliary space to store array elements, additional space is required for creating stack frames in recursive calls. This happens when the pivot element is the largest or smallest element of the array in every recursive call. The size of the subarray after partitioning will be n-1 and 1.

Why merge sort requires additional space?

Merge Sort, like other sorting algorithms, does not work in-place. An in-place algorithm is a sorting algorithm in which the sorted items occupy the same storage as the original ones. But Merge Sort makes copies of the left and right half of the given array. It requires a linear amount of extra storage space.

What is the space complexity of Quicksort algorithm?

The space complexity of quicksort is O(n*logn).


2 Answers

Correct, the extra space are the log(n) stack frames. From the Wikipedia article of Quicksort:

There is a more complex version which [...] can achieve the complete sort using O(log n) space (not counting the input) on average (for the call stack).

While you could implement quicksort iteratively (i.e., using a loop instead of recursion), you would then need to maintain an auxiliary stack, because Quicksort has two recursive calls and not just one.

Finally, as other answers have pointed out, O(log(n)) is for pretty much all practical applications very, very small. Every constant factor, like the overhead of your data structure, will have a greater impact on memory usage.

like image 110
rolve Avatar answered Sep 20 '22 07:09

rolve


To get rid of the recursive call you would have to use a stack data structure in your code, and it would still occupy log(n) space.

like image 34
japreiss Avatar answered Sep 21 '22 07:09

japreiss