Open In App

Some Frequently Asked Questions (FAQs) about Quick Sort

Last Updated : 24 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Below are some of the most frequently asked questions on Quick Sort:

1. Hoare’s vs Lomuto Partition

Please note that the above implementation is Lomuto Partition. A more optimized implementation of QuickSort is Hoare’s partition which is more efficient than Lomuto’s partition scheme because it does three times less swaps on average.

2. How to pick any element as pivot?

With one minor change to the above code, we can pick any element as pivot. For example, to make the first element as pivot, we can simply swap the first and last elements and then use the same code. Same thing can be done to pick any random element as a pivot 

3. Is QuickSort stable?

The default implementation is not stable. However, any sorting algorithm can be made stable by considering indices as a comparison parameter. 

4. Is QuickSort In-place?

As per the broad definition of an in-place algorithm, it qualifies as an in-place sorting algorithm as it uses extra space only for storing recursive function calls but not for manipulating the input. 

5. What is 3-Way QuickSort?

In simple QuickSort algorithm, we select an element as pivot, partition the array around pivot and recur for subarrays on left and right of pivot. 
Consider an array that has many redundant elements. For example, {1, 4, 2, 4, 2, 4, 1, 2, 4, 1, 2, 2, 2, 2, 4, 1, 4, 4, 4}. If 4 is picked as pivot in Simple QuickSort, we fix only one 4 and recursively process the remaining occurrences. In 3 Way QuickSort, an array arr[l..r] is divided in 3 parts: 

  • arr[l..i] elements less than pivot. 
  • arr[i+1..j-1] elements equal to pivot. 
  • arr[j..r] elements greater than pivot. 

See this for implementation.

6. How to implement QuickSort for Linked Lists?

QuickSort on Singly Linked List 
QuickSort on Doubly Linked List

7. Can we implement QuickSort Iteratively?

Yes, please refer to Iterative Quick Sort

8. How to optimize QuickSort so that it takes O(log N) extra space in the worst case? 

As the recursion call is performed at the end of the recursive function, we can use the concept of tail recursion to optimize the space taken by Quicksort. Please refer to QuickSort Tail Call Optimization (Reducing worst case space to log N).


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads