Like Merge Sort, QuickSort is a Divide and Conquer algorithm. It picks an element as pivot and partitions the given array around the picked pivot. There are many different versions of quickSort that pick pivot in different ways.

  1. Always pick first element as pivot.
  2. Always pick last element as pivot (implemented below)
  3. Pick a random element as pivot.
  4. Pick median as pivot.

The key process in quickSort is partition(). Target of partitions is, given an array and an element x of array as pivot, put x at its correct position in sorted array and put all smaller elements (smaller than x) before x, and put all greater elements (greater than x) after x. All this should be done in linear time.

Pseudo Code for recursive QuickSort function :

/* low  --> Starting index,  high  --> Ending index */
quickSort(arr[], low, high)
{
    if (low < high)
    {
        /* pi is partitioning index, arr[p] is now
           at right place */
        pi = partition(arr, low, high);

        quickSort(arr, low, pi - 1);  // Before pi
        quickSort(arr, pi + 1, high); // After pi
    }
}

 

Quick Sort

[ad type=”banner”]

Partition Algorithm
There can be many ways to do partition, following pseudo code adopts the method given in CLRS book. The logic is simple, we start from the leftmost element and keep track of index of smaller (or equal to) elements as i. While traversing, if we find a smaller element, we swap current element with arr[i]. Otherwise we ignore current element.

/* low  --> Starting index,  high  --> Ending index */
quickSort(arr[], low, high)
{
    if (low < high)
    {
        /* pi is partitioning index, arr[p] is now
           at right place */
        pi = partition(arr, low, high);

        quickSort(arr, low, pi - 1);  // Before pi
        quickSort(arr, pi + 1, high); // After pi
    }
}

Pseudo code for partition()

/* This function takes last element as pivot, places
   the pivot element at its correct position in sorted
    array, and places all smaller (smaller than pivot)
   to left of pivot and all greater elements to right
   of pivot */
partition (arr[], low, high)
{
    // pivot (Element to be placed at right position)
    pivot = arr[high];  
 
    i = (low - 1)  // Index of smaller element

    for (j = low; j <= high- 1; j++)
    {
        // If current element is smaller than or
        // equal to pivot
        if (arr[j] <= pivot)
        {
            i++;    // increment index of smaller element
            swap arr[i] and arr[j]
        }
    }
    swap arr[i + 1] and arr[high])
    return (i + 1)
}
[ad type=”banner”]

Illustration of partition() :

arr[] = {10, 80, 30, 90, 40, 50, 70}
Indexes:  0   1   2   3   4   5   6 

low = 0, high =  6, pivot = arr[h] = 70
Initialize index of smaller element, i = -1

Traverse elements from j = low to high-1
j = 0 : Since arr[j] <= pivot, do i++ and swap(arr[i], arr[j])
i = 0 
arr[] = {10, 80, 30, 90, 40, 50, 70} // No change as i and j 
                                     // are same

j = 1 : Since arr[j] > pivot, do nothing
// No change in i and arr[]

j = 2 : Since arr[j] <= pivot, do i++ and swap(arr[i], arr[j])
i = 1
arr[] = {10, 30, 80, 90, 40, 50, 70} // We swap 80 and 30 

j = 3 : Since arr[j] > pivot, do nothing
// No change in i and arr[]

j = 4 : Since arr[j] <= pivot, do i++ and swap(arr[i], arr[j])
i = 2
arr[] = {10, 30, 40, 90, 80, 50, 70} // 80 and 40 Swapped
j = 5 : Since arr[j] <= pivot, do i++ and swap arr[i] with arr[j] 
i = 3 
arr[] = {10, 30, 40, 50, 80, 90, 70} // 90 and 50 Swapped 

We come out of loop because j is now equal to high-1.
Finally we place pivot at correct position by swapping
arr[i+1] and arr[high] (or pivot) 
arr[] = {10, 30, 40, 50, 70, 90, 80} // 80 and 70 Swapped 

Now 70 is at its correct place. All elements smaller than
70 are before it and all elements greater than 70 are after
it.

Recommended: Please solve it on “PRACTICE” first, before moving on to the solution.

[ad type=”banner”]

Implementation:
Following are C++, Java and Python implementations of QuickSort.

C++

[pastacode lang=”c” manual=”%2F*%20C%20implementation%20QuickSort%20*%2F%0A%23include%3Cstdio.h%3E%0A%20%0A%2F%2F%20A%20utility%20function%20to%20swap%20two%20elements%0Avoid%20swap(int*%20a%2C%20int*%20b)%0A%7B%0A%20%20%20%20int%20t%20%3D%20*a%3B%0A%20%20%20%20*a%20%3D%20*b%3B%0A%20%20%20%20*b%20%3D%20t%3B%0A%7D%0A%20%0A%2F*%20This%20function%20takes%20last%20element%20as%20pivot%2C%20places%0A%20%20%20the%20pivot%20element%20at%20its%20correct%20position%20in%20sorted%0A%20%20%20%20array%2C%20and%20places%20all%20smaller%20(smaller%20than%20pivot)%0A%20%20%20to%20left%20of%20pivot%20and%20all%20greater%20elements%20to%20right%0A%20%20%20of%20pivot%20*%2F%0Aint%20partition%20(int%20arr%5B%5D%2C%20int%20low%2C%20int%20high)%0A%7B%0A%20%20%20%20int%20pivot%20%3D%20arr%5Bhigh%5D%3B%20%20%20%20%2F%2F%20pivot%0A%20%20%20%20int%20i%20%3D%20(low%20-%201)%3B%20%20%2F%2F%20Index%20of%20smaller%20element%0A%20%0A%20%20%20%20for%20(int%20j%20%3D%20low%3B%20j%20%3C%3D%20high-%201%3B%20j%2B%2B)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%2F%2F%20If%20current%20element%20is%20smaller%20than%20or%0A%20%20%20%20%20%20%20%20%2F%2F%20equal%20to%20pivot%0A%20%20%20%20%20%20%20%20if%20(arr%5Bj%5D%20%3C%3D%20pivot)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20i%2B%2B%3B%20%20%20%20%2F%2F%20increment%20index%20of%20smaller%20element%0A%20%20%20%20%20%20%20%20%20%20%20%20swap(%26arr%5Bi%5D%2C%20%26arr%5Bj%5D)%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%20%20%20swap(%26arr%5Bi%20%2B%201%5D%2C%20%26arr%5Bhigh%5D)%3B%0A%20%20%20%20return%20(i%20%2B%201)%3B%0A%7D%0A%20%0A%2F*%20The%20main%20function%20that%20implements%20QuickSort%0A%20arr%5B%5D%20–%3E%20Array%20to%20be%20sorted%2C%0A%20%20low%20%20–%3E%20Starting%20index%2C%0A%20%20high%20%20–%3E%20Ending%20index%20*%2F%0Avoid%20quickSort(int%20arr%5B%5D%2C%20int%20low%2C%20int%20high)%0A%7B%0A%20%20%20%20if%20(low%20%3C%20high)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%2F*%20pi%20is%20partitioning%20index%2C%20arr%5Bp%5D%20is%20now%0A%20%20%20%20%20%20%20%20%20%20%20at%20right%20place%20*%2F%0A%20%20%20%20%20%20%20%20int%20pi%20%3D%20partition(arr%2C%20low%2C%20high)%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Separately%20sort%20elements%20before%0A%20%20%20%20%20%20%20%20%2F%2F%20partition%20and%20after%20partition%0A%20%20%20%20%20%20%20%20quickSort(arr%2C%20low%2C%20pi%20-%201)%3B%0A%20%20%20%20%20%20%20%20quickSort(arr%2C%20pi%20%2B%201%2C%20high)%3B%0A%20%20%20%20%7D%0A%7D%0A%20%0A%2F*%20Function%20to%20print%20an%20array%20*%2F%0Avoid%20printArray(int%20arr%5B%5D%2C%20int%20size)%0A%7B%0A%20%20%20%20int%20i%3B%0A%20%20%20%20for%20(i%3D0%3B%20i%20%3C%20size%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20%20printf(%22%25d%20%22%2C%20arr%5Bi%5D)%3B%0A%20%20%20%20printf(%22%5Cn%22)%3B%0A%7D%0A%20%0A%2F%2F%20Driver%20program%20to%20test%20above%20functions%0Aint%20main()%0A%7B%0A%20%20%20%20int%20arr%5B%5D%20%3D%20%7B10%2C%207%2C%208%2C%209%2C%201%2C%205%7D%3B%0A%20%20%20%20int%20n%20%3D%20sizeof(arr)%2Fsizeof(arr%5B0%5D)%3B%0A%20%20%20%20quickSort(arr%2C%200%2C%20n-1)%3B%0A%20%20%20%20printf(%22Sorted%20array%3A%20%5Cn%22)%3B%0A%20%20%20%20printArray(arr%2C%20n)%3B%0A%20%20%20%20return%200%3B%0A%7D%0A” message=”c” highlight=”” provider=”manual”/]

Java

[pastacode lang=”java” manual=”%2F%2F%20Java%20program%20for%20implementation%20of%20QuickSort%0Aclass%20QuickSort%0A%7B%0A%20%20%20%20%2F*%20This%20function%20takes%20last%20element%20as%20pivot%2C%0A%20%20%20%20%20%20%20places%20the%20pivot%20element%20at%20its%20correct%0A%20%20%20%20%20%20%20position%20in%20sorted%20array%2C%20and%20places%20all%0A%20%20%20%20%20%20%20smaller%20(smaller%20than%20pivot)%20to%20left%20of%0A%20%20%20%20%20%20%20pivot%20and%20all%20greater%20elements%20to%20right%0A%20%20%20%20%20%20%20of%20pivot%20*%2F%0A%20%20%20%20int%20partition(int%20arr%5B%5D%2C%20int%20low%2C%20int%20high)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20int%20pivot%20%3D%20arr%5Bhigh%5D%3B%20%0A%20%20%20%20%20%20%20%20int%20i%20%3D%20(low-1)%3B%20%2F%2F%20index%20of%20smaller%20element%0A%20%20%20%20%20%20%20%20for%20(int%20j%3Dlow%3B%20j%3Chigh%3B%20j%2B%2B)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20If%20current%20element%20is%20smaller%20than%20or%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20equal%20to%20pivot%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20(arr%5Bj%5D%20%3C%3D%20pivot)%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20i%2B%2B%3B%0A%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20swap%20arr%5Bi%5D%20and%20arr%5Bj%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20int%20temp%20%3D%20arr%5Bi%5D%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20arr%5Bi%5D%20%3D%20arr%5Bj%5D%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20arr%5Bj%5D%20%3D%20temp%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%7D%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20swap%20arr%5Bi%2B1%5D%20and%20arr%5Bhigh%5D%20(or%20pivot)%0A%20%20%20%20%20%20%20%20int%20temp%20%3D%20arr%5Bi%2B1%5D%3B%0A%20%20%20%20%20%20%20%20arr%5Bi%2B1%5D%20%3D%20arr%5Bhigh%5D%3B%0A%20%20%20%20%20%20%20%20arr%5Bhigh%5D%20%3D%20temp%3B%0A%20%0A%20%20%20%20%20%20%20%20return%20i%2B1%3B%0A%20%20%20%20%7D%0A%20%0A%20%0A%20%20%20%20%2F*%20The%20main%20function%20that%20implements%20QuickSort()%0A%20%20%20%20%20%20arr%5B%5D%20–%3E%20Array%20to%20be%20sorted%2C%0A%20%20%20%20%20%20low%20%20–%3E%20Starting%20index%2C%0A%20%20%20%20%20%20high%20%20–%3E%20Ending%20index%20*%2F%0A%20%20%20%20void%20sort(int%20arr%5B%5D%2C%20int%20low%2C%20int%20high)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20if%20(low%20%3C%20high)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F*%20pi%20is%20partitioning%20index%2C%20arr%5Bpi%5D%20is%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20now%20at%20right%20place%20*%2F%0A%20%20%20%20%20%20%20%20%20%20%20%20int%20pi%20%3D%20partition(arr%2C%20low%2C%20high)%3B%0A%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20Recursively%20sort%20elements%20before%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20partition%20and%20after%20partition%0A%20%20%20%20%20%20%20%20%20%20%20%20sort(arr%2C%20low%2C%20pi-1)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20sort(arr%2C%20pi%2B1%2C%20high)%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F*%20A%20utility%20function%20to%20print%20array%20of%20size%20n%20*%2F%0A%20%20%20%20static%20void%20printArray(int%20arr%5B%5D)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20int%20n%20%3D%20arr.length%3B%0A%20%20%20%20%20%20%20%20for%20(int%20i%3D0%3B%20i%3Cn%3B%20%2B%2Bi)%0A%20%20%20%20%20%20%20%20%20%20%20%20System.out.print(arr%5Bi%5D%2B%22%20%22)%3B%0A%20%20%20%20%20%20%20%20System.out.println()%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F%2F%20Driver%20program%0A%20%20%20%20public%20static%20void%20main(String%20args%5B%5D)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20int%20arr%5B%5D%20%3D%20%7B10%2C%207%2C%208%2C%209%2C%201%2C%205%7D%3B%0A%20%20%20%20%20%20%20%20int%20n%20%3D%20arr.length%3B%0A%20%0A%20%20%20%20%20%20%20%20QuickSort%20ob%20%3D%20new%20QuickSort()%3B%0A%20%20%20%20%20%20%20%20ob.sort(arr%2C%200%2C%20n-1)%3B%0A%20%0A%20%20%20%20%20%20%20%20System.out.println(%22sorted%20array%22)%3B%0A%20%20%20%20%20%20%20%20printArray(arr)%3B%0A%20%20%20%20%7D%0A%7D%0A%2F*This%20code%20is%20contributed%20by%20Rajat%20Mishra%20*%2F%0A” message=”” highlight=”” provider=”manual”/]

Python

[pastacode lang=”python” manual=”%23%20Python%20program%20for%20implementation%20of%20Quicksort%20Sort%0A%20%0A%23%20This%20function%20takes%20last%20element%20as%20pivot%2C%20places%0A%23%20the%20pivot%20element%20at%20its%20correct%20position%20in%20sorted%0A%23%20array%2C%20and%20places%20all%20smaller%20(smaller%20than%20pivot)%0A%23%20to%20left%20of%20pivot%20and%20all%20greater%20elements%20to%20right%0A%23%20of%20pivot%0Adef%20partition(arr%2Clow%2Chigh)%3A%0A%20%20%20%20i%20%3D%20(%20low-1%20)%20%20%20%20%20%20%20%20%20%23%20index%20of%20smaller%20element%0A%20%20%20%20pivot%20%3D%20arr%5Bhigh%5D%20%20%20%20%20%23%20pivot%0A%20%0A%20%20%20%20for%20j%20in%20range(low%20%2C%20high)%3A%0A%20%0A%20%20%20%20%20%20%20%20%23%20If%20current%20element%20is%20smaller%20than%20or%0A%20%20%20%20%20%20%20%20%23%20equal%20to%20pivot%0A%20%20%20%20%20%20%20%20if%20%20%20arr%5Bj%5D%20%3C%3D%20pivot%3A%0A%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20increment%20index%20of%20smaller%20element%0A%20%20%20%20%20%20%20%20%20%20%20%20i%20%3D%20i%2B1%0A%20%20%20%20%20%20%20%20%20%20%20%20arr%5Bi%5D%2Carr%5Bj%5D%20%3D%20arr%5Bj%5D%2Carr%5Bi%5D%0A%20%0A%20%20%20%20arr%5Bi%2B1%5D%2Carr%5Bhigh%5D%20%3D%20arr%5Bhigh%5D%2Carr%5Bi%2B1%5D%0A%20%20%20%20return%20(%20i%2B1%20)%0A%20%0A%23%20The%20main%20function%20that%20implements%20QuickSort%0A%23%20arr%5B%5D%20–%3E%20Array%20to%20be%20sorted%2C%0A%23%20low%20%20–%3E%20Starting%20index%2C%0A%23%20high%20%20–%3E%20Ending%20index%0A%20%0A%23%20Function%20to%20do%20Quick%20sort%0Adef%20quickSort(arr%2Clow%2Chigh)%3A%0A%20%20%20%20if%20low%20%3C%20high%3A%0A%20%0A%20%20%20%20%20%20%20%20%23%20pi%20is%20partitioning%20index%2C%20arr%5Bp%5D%20is%20now%0A%20%20%20%20%20%20%20%20%23%20at%20right%20place%0A%20%20%20%20%20%20%20%20pi%20%3D%20partition(arr%2Clow%2Chigh)%0A%20%0A%20%20%20%20%20%20%20%20%23%20Separately%20sort%20elements%20before%0A%20%20%20%20%20%20%20%20%23%20partition%20and%20after%20partition%0A%20%20%20%20%20%20%20%20quickSort(arr%2C%20low%2C%20pi-1)%0A%20%20%20%20%20%20%20%20quickSort(arr%2C%20pi%2B1%2C%20high)%0A%20%0A%23%20Driver%20code%20to%20test%20above%0Aarr%20%3D%20%5B10%2C%207%2C%208%2C%209%2C%201%2C%205%5D%0An%20%3D%20len(arr)%0AquickSort(arr%2C0%2Cn-1)%0Aprint%20(%22Sorted%20array%20is%3A%22)%0Afor%20i%20in%20range(n)%3A%0A%20%20%20%20print%20(%22%25d%22%20%25arr%5Bi%5D)%2C%0A%20%0A” message=”” highlight=”” provider=”manual”/]

Output:

Sorted array:
1 5 7 8 9 10
[ad type=”banner”]

Analysis of QuickSort
Time taken by QuickSort in general can be written as following.

 T(n) = T(k) + T(n-k-1) + \theta(n)

The first two terms are for two recursive calls, the last term is for the partition process. k is the number of elements which are smaller than pivot.
The time taken by QuickSort depends upon the input array and partition strategy. Following are three cases.

Worst Case: The worst case occurs when the partition process always picks greatest or smallest element as pivot. If we consider above partition strategy where last element is always picked as pivot, the worst case would occur when the array is already sorted in increasing or decreasing order. Following is recurrence for worst case.

 T(n) = T(0) + T(n-1) + \theta(n)
which is equivalent to  
 T(n) = T(n-1) + \theta(n)

The solution of above recurrence is \theta(n2).

Best Case: The best case occurs when the partition process always picks the middle element as pivot. Following is recurrence for best case.

 T(n) = 2T(n/2) + \theta(n)

The solution of above recurrence is \theta(nLogn). It can be solved using case 2 of Master Theorem.

Average Case:
To do average case analysis, we need to consider all possible permutation of array and calculate time taken by every permutation which doesn’t look easy.
We can get an idea of average case by considering the case when partition puts O(n/9) elements in one set and O(9n/10) elements in other set. Following is recurrence for this case.

 T(n) = T(n/9) + T(9n/10) + \theta(n)

Solution of above recurrence is also O(nLogn)

Although the worst case time complexity of QuickSort is O(n2) which is more than many other sorting algorithms like Merge Sort and Heap Sort, QuickSort is faster in practice, because its inner loop can be efficiently implemented on most architectures, and in most real-world data. QuickSort can be implemented in different ways by changing the choice of pivot, so that the worst case rarely occurs for a given type of data. However, merge sort is generally considered better when data is huge and stored in external storage.

[ad type=”banner”]

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 which 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 remaining occurrences. In 3 Way QuickSort, an array arr[l..r] is divided in 3 parts:
a) arr[l..i] elements less than pivot.
b) arr[i+1..j-1] elements equal to pivot.
c) arr[j..r] elements greater than pivot.
See this for implementation.

How to implement QuickSort for Linked Lists?
QuickSort on Singly Linked List
QuickSort on Doubly Linked List

Can we implement QuickSort Iteratively?
Yes, please refer Iterative Quick Sort.

Why Quick Sort is preferred over MergeSort for sorting Arrays
Quick Sort in its general form is an in-place sort (i.e. it doesn’t require any extra storage) whereas merge sort requires O(N) extra storage, N denoting the array size which may be quite expensive. Allocating and de-allocating the extra space used for merge sort increases the running time of the algorithm. Comparing average complexity we find that both type of sorts have O(NlogN) average complexity but the constants differ. For arrays, merge sort loses due to the use of extra O(N) storage space.

Most practical implementations of Quick Sort use randomized version. The randomized version has expected time complexity of O(nLogn). The worst case is possible in randomized version also, but worst case doesn’t occur for a particular pattern (like sorted array) and randomized Quick Sort works well in practice.

Quick Sort is also a cache friendly sorting algorithm as it has good locality of reference when used for arrays.

Quick Sort is also tail recursive, therefore tail call optimizations is done.

Why MergeSort is preferred over QuickSort for Linked Lists?
In case of linked lists the case is different mainly due to difference in memory allocation of arrays and linked lists. Unlike arrays, linked list nodes may not be adjacent in memory. Unlike array, in linked list, we can insert items in the middle in O(1) extra space and O(1) time. Therefore merge operation of merge sort can be implemented without extra space for linked lists.

In arrays, we can do random access as elements are continuous in memory. Let us say we have an integer (4-byte) array A and let the address of A[0] be x then to access A[i], we can directly access the memory at (x + i*4). Unlike arrays, we can not do random access in linked list. Quick Sort requires a lot of this kind of access. In linked list to access i’th index, we have to travel each and every node from the head to i’th node as we don’t have continuous block of memory. Therefore, the overhead increases for quick sort. Merge sort accesses data sequentially and the need of random access is low.

[ad type=”banner”]

Tagged in:

, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,