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, p-1, hi) quicksort(arr, p+1, hi)
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, p, hi) quicksort(arr, p+1, hi)
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 ).
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.
- C++ Program for QuickSort
- Why quicksort is better than mergesort ?
- Stable QuickSort
- Dual pivot Quicksort
- Java Program for QuickSort
- Python Program for QuickSort
- Generic Implementation of QuickSort Algorithm in C
- QuickSort on Singly Linked List
- When does the worst case of Quicksort occur?
- QuickSort on Doubly Linked List
- 3-Way QuickSort (Dutch National Flag)
- Hoare's vs Lomuto partition scheme in QuickSort
- Can QuickSort be implemented in O(nLogn) worst case time complexity?
- Comparisons involved in Modified Quicksort Using Merge Sort Tree
- QuickSort Tail Call Optimization (Reducing worst case space to Log n )
- Implement random-0-6-Generator using the given random-0-1-Generator
- Generating Random String Using PHP
- Binomial Random Variables
- Generate a random permutation of 1 to N
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.