It works by selecting an element from input array called 'pivot' and partition the array into two parts such a way that elements lesser than to pivot will be moved to one side of pivot and greater than pivot to the other side. By doing this, pivot element chosen is aligned to its position where it should belong when the array is sorted. The sub-arrays are then sorted recursively.
There are several ways of selecting a pivot.
- Selecting the first element from the array as pivot.
- Selecting the last element from the array as pivot.
- Selecting median indexed element from the array as pivot.
- Selecting any random element from the array as pivot.
Complexity analysis:
To sort an array of
QuickSort implementation using Lomuto's partition scheme QuickSort implementation using Hoare's partition scheme
In Lomuto partition scheme, last element in the array is typically chosen as pivot and partition the array such a way that, place smaller than pivot elements on one side of the array and greater than pivot elements to the other side of the array and finally place the pivot in the position where it should be when the array is fully sorted.
Steps involved in this implementation are...
- Pick right most element from array as pivot.
-
Initialize two variables
leftIndex andstoreIndex with left index of the array,leftIndex will be used to to iterate over the array andstoreIndex will be used to keep track of where in the array smaller or greater than to pivot elements are occurred and swap them withleftIndex element. -
At the end of loop,
storeIndex element andpivot element. - Now, we can conclude that pivot is positioned correctly where it should if the array is fully sorted and elements on one side smaller than pivot and elements on other side are greater than pivot.
- We will have to repeat above steps on left and right sub arrays with indices
[left,.....,pivot-1] and[pivot+1,.....,right] recursively.

Quicksort implementation using Lomuto partition scheme can be found at
Hoare's partition scheme uses two pointers, one pointer starts on left end of the array and the other starts on the right end of of the array, then move these two pointers toward each other until they detect an inversion, meaning that an element on one end pointer is less than pivot and and element on the other end pointer is greater than pivot; When this inversion occurred, if the index first pointer is less than second pointer then these elements should be swapped and continue the process, at some point these pointers cross each other, then we have found a valid partition and return that as partition index and repeat these steps on the sub arrays.
Steps involved in this implementation
-
Pick median element as pivot with
(left + right)/2 index, to avoid integer overflow we can change this toleft + (right-left)/2 . -
Initialize two variables
i = left - 1; andj = right + 1; that we are going to use to iterate from left end of array and right end of array respectively. -
Within a while infinite loop, do incrementing variable
i as long as ith index element is less than 'pivot' and do decrementing variablej as long as jth index element is greater than 'pivot'. -
When a pair of numbers found, ith and jth indexes that are less than pivot and greater than pivot,
if
i is less thanj then swap these two element to move 'less than pivot' elements to left side of the array and move 'greater than pivot' elements to right side. -
When
i andj cross each other, then return that index as partition index. -
Repeat above steps for sub arrays,
[left,.....,pivot] and[pivot=1,.....,right]
Note: Note that we are using do..... while loop to decrement or increment variables i and j because once the while condition is met to break the loop, we need to continue with other elements in the array as well until these pointers cross each other.
Note: Notice that in the sub arrays, we are including pivot as well as pivot may not be in its final position when the partition index is found.
QuickSort implementation using Hoare's partition scheme source code can be found at