VOOZH about

URL: https://www.geeksforgeeks.org/dsa/iterative-quick-sort/

⇱ Iterative Quick Sort - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Iterative Quick Sort

Last Updated : 23 Jul, 2025

Following is a typical recursive implementation of Quick Sort that uses last element as pivot. 

Output:

2 2 4 6 9


The above implementation can be optimized in many ways
1) The above implementation uses the last index as a pivot. This causes worst-case behavior on already sorted arrays, which is a commonly occurring case. The problem can be solved by choosing either a random index for the pivot or choosing the middle index of the partition or choosing the median of the first, middle, and last element of the partition for the pivot. (See this for details)
2) To reduce the recursion depth, recur first for the smaller half of the array, and use a tail call to recurse into the other. 
3) Insertion sort works better for small subarrays. Insertion sort can be used for invocations on such small arrays (i.e. where the length is less than a threshold t determined experimentally). For example, this library implementation of Quicksort uses insertion sort below size 7. 

Despite the above optimizations, the function remains recursive and uses function call stack to store intermediate values of l and h. The function call stack stores other bookkeeping information together with parameters. Also, function calls involve overheads like storing activation records of the caller function and then resuming execution. The above function can be easily converted to an iterative version with the help of an auxiliary stack. Following is an iterative implementation of the above recursive code. 


Output:

1 2 2 3 3 3 4 5

Time Complexity: O(n*log(n))
Auxiliary Space: O(n)


The above-mentioned optimizations for recursive quicksort can also be applied to the iterative version.
1) Partition process is the same in both recursive and iterative. The same techniques to choose optimal pivot can also be applied to the iterative version.
2) To reduce the stack size, first push the indexes of smaller half.
3) Use insertion sort when the size reduces below an experimentally calculated threshold.
References:
https://en.wikipedia.org/wiki/Quicksort
This article is compiled by Aashish Barnwal and reviewed by GeeksforGeeks team.

Comment
Article Tags: