Prerequisite : Tail Call Elimination
In QuickSort, partition function is in-place, but we need extra space for recursive function calls. 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. For example, in below code, if we choose last element as pivot, we get worst case for sorted arrays (See this for visualization)
Can we reduce the auxiliary space for function call stack?
We can limit the auxiliary space to O(Log n). The idea is based on tail call elimination. As seen in the previous post, we can convert the code so that it makes one recursive call. For example, in the below code, we have converted the above code to use a while loop and have reduced the number of recursive calls.
Although we have reduced number of recursive calls, the above code can still use O(n) auxiliary space in worst case. In worst case, it is possible that array is divided in a way that the first part always has n-1 elements. For example, this may happen when last element is choses as pivot and array is sorted in decreasing order.
We can optimize the above code to make a recursive call only for the smaller part after partition. Below is implementation of this idea.
Further Optimization :
In the above code, if left part becomes smaller, then we make recursive call for left part. Else for the right part. In worst case (for space), when both parts are of equal sizes in all recursive calls, we use O(Log n) extra space.
This article is contributed by Dheeraj Jain. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- When does the worst case of Quicksort occur?
- Can QuickSort be implemented in O(nLogn) worst case time complexity?
- Tail Call Elimination
- Find a permutation that causes worst case of Merge Sort
- Case-specific sorting of Strings in O(n) time and O(1) space
- Tail Recursion
- C++ Program for QuickSort
- Stable QuickSort
- Why quicksort is better than mergesort ?
- Java Program for QuickSort
- QuickSort using Random Pivoting
- Dual pivot Quicksort
- Python Program for QuickSort
- Generic Implementation of QuickSort Algorithm in C
- 3-Way QuickSort (Dutch National Flag)
- QuickSort on Doubly Linked List
- QuickSort on Singly Linked List
- Hoare's vs Lomuto partition scheme in QuickSort
- Comparisons involved in Modified Quicksort Using Merge Sort Tree