In this article, we will discuss how to implement QuickSort using random pivoting. In QuickSort we first partition the array in place such that all elements to the left of the pivot element are smaller, while all elements to the right of the pivot are greater that the pivot. Then we recursively call the same procedure for left and right subarrays.
Unlike merge sort, we don’t need to merge the two sorted arrays. Thus Quicksort requires lesser auxiliary space than Merge Sort, which is why it is often preferred to Merge Sort. Using a randomly generated pivot we can further improve the time complexity of QuickSort.
We have discussed at two popular methods for partitioning the arrays-Hoare’s vs Lomuto partition scheme
It is advised that the reader has read that article or knows how to implement the QuickSort using either of the two partition schemes.
Algorithm for random pivoting using Lomuto Partitioning
partition(arr, lo, hi) pivot = arr[hi] i = lo // place for swapping for j := lo to hi – 1 do if arr[j] <= pivot then swap arr[i] with arr[j] i = i + 1 swap arr[i] with arr[hi] return i partition_r(arr, lo, hi) r = Random Number from lo to hi Swap arr[r] and arr[hi] return partition(arr, lo, hi) quicksort(arr, lo, hi) if lo < hi p = partition_r(arr, lo, hi) quicksort(arr, lo , p-1) quicksort(arr, p+1, hi)
Implementation using Lomuto Partitioning:
Sorted array: 1 5 7 8 9 10
Algorithm for random pivoting using Hoare Partitioning
partition(arr, lo, hi) pivot = arr[lo] i = lo - 1 // Initialize left index j = hi + 1 // Initialize right index // Find a value in left side greater // than pivot do i = i + 1 while arr[i] pivot if i >= j then return j swap arr[i] with arr[j] partition_r(arr, lo, hi) r = Random number from lo to hi Swap arr[r] and arr[lo] return partition(arr, lo, hi) quicksort(arr, lo, hi) if lo < hi p = partition_r(arr, lo, hi) quicksort(arr, lo, p) quicksort(arr, p+1, hi)
Implementation using Hoare’s Partitioning:
Sorted array: 1 5 7 8 9 10
- Using random pivoting we improve the expected or average time complexity to O (N log N). The Worst-Case complexity is still O ( N^2 ).
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Implement random-0-6-Generator using the given random-0-1-Generator
- Comparisons involved in Modified Quicksort Using Merge Sort Tree
- Merge two sorted arrays in O(1) extra space using QuickSort partition
- When does the worst case of Quicksort occur?
- QuickSort on Doubly Linked List
- QuickSort on Singly Linked List
- Can QuickSort be implemented in O(nLogn) worst case time complexity?
- 3-Way QuickSort (Dutch National Flag)
- QuickSort Tail Call Optimization (Reducing worst case space to Log n )
- Hoare's vs Lomuto partition scheme in QuickSort
- Why quicksort is better than mergesort ?
- Dual pivot Quicksort
- C++ Program for QuickSort
- Java Program for QuickSort
- Python Program for QuickSort
- Generic Implementation of QuickSort Algorithm in C
- Stable QuickSort
- Generating Random String Using PHP
- Select a random number from stream, with O(1) space
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.