Quick sort algorithm

Quick sort Algorithm

Quick sort like merge sort is a sorting algorithm under divide and conquer paradigm of algorithms like merge sort. Basic idea of algorithm is to divide inputs around a pivot and then sort two smaller parts recursively and finally get original input sorted.

Selection of pivot

Entire idea of quick sort revolves around pivot. Pivot is an element in input around which input is arranged in such a way that all elements on left side are smaller and all elements on right side are greater than pivot. Question is how to find or select pivot and put it into correct position.

To make things simpler to start with, let’s assume first element of input is pivot element.

To put this pivot at correct position in input, start with next element of pivot in input space and find first element which is greater than pivot. Let that be ith position.

At the same time, start from end of array and find first element which is smaller than pivot. Let it be jth position.

If i and j have not crossed each other i.e i < j, then swap element at ith and jth positions, and continue moving right on input to find element greater than pivot and moving left to find element smaller than pivot.
Once i and j cross each other, swap pivot with element at jth position.  After this step, pivot will be at its correct position and array will be divided into two parts. All elements on left side will be less than pivot and all elements on right side will be greater than pivot.

Quick sort partition example

This is too much to process, I know! Let’s take an example and see how it does it work? We have an array as follows

quick sort

Let’s select first element as pivot, pivot = 3.

quick sort pivot selection

Start from next element of pivot, move towards right of array, till we see first element which is greater than pivot i.e. 3.

From end of array, move towards left till you find an element which is less than pivot.

Now, there are two indices, i and j, where A[i] > pivot and A[j] < pivot. See that i and j not yet crossed each other. Hence, we swap A[i] with A[j]. Array at the bottom of pic, shows resultant array after swap.

quick sort partition

Again, start with i+1 and follow the same rule : Stop when you find element greater than pivot. In this case, 10 is greater than 3, hence we stop.

Similarly, move left from end again, till we find an element which is less than pivot. In this case, we end up at index = 2 which is element 1.

Since, i > j, than means paths have been crossed. At this time, instead of swapping element at i and j index, swap element at j index with pivot.

After swapping pivot with jth index, we have array divided into two parts, pivot as boundary. All elements on left side of pivot are smaller (they may not be sorted) and all elements on right side of pivot are greater than pivot (again may not be sorted).

quick sort partitions

We, apply this same partition process to left and right arrays again, till base condition is hit. In this case, base condition would be if there is only one element in array to be partitioned.

Quick sort algorithm

quickSort([], start, end)
 1. If array has more than one elements i.e (start < end):
    1.1 Find correct place for pivot.
    pivot = partition(arr, low, high)
    1.2. Apply same function recursively to left of pivot index
         quickSort(arr, start, pivot -1 )
         and to the right of pivot index
         quickSort(arr, pivot + 1, end)

Quick sort implementation

#include <stdio.h>

void swap(int a[], int i, int j){
	int temp = a[i];
	a[i] = a[j];
	a[j] = temp;

int partition(int a[], int start, int end){
	int pivot = a[start];
	int i  = start+1;
	int j  = end;
	while(i < j){
	    while(a[i] < pivot) i++;
            while(a[j] > pivot) j--;
            if(i < j) {
                swap(a, i, j);
	swap(a, start, j);
	return j;

void quickSort(int a[], int start, int end){
    if(start < end){
        int p = partition(a, start, end);
	quickSort(a,start, p-1);
	quickSort(a, p+1, end);

int main(void) {
	int a[]= {4,3,2,5,6,8,1};
	int size = sizeof(a)/sizeof(a[0]);
	quickSort(a, 0, size-1);
	for(int i=0; i < size; i++){
		printf(" %d", a[i]);
	return 0;

There is another implementation which is based on Lomuto partition scheme, in this scheme, we make last element as pivot. The implementation is compact but complexity is bit higher than the original partition methods in terms of number of swaps.

void swap(int *a, int *b){
    int temp = *a;
    *a = *b;
    *b = temp;
int partition(int a[], int low, int high)
    // set pivot as highest element
    int x  = a[high];
    //Current low points to previous of low of this part of array. 
    int i = low - 1;
    for (int j = low; j <= high-1; j++)
    	/*Move in the array till current node data is 
        less than the pivot */
        if (a[j] <= x){
            //set the current low appropriately
            swap(&a[i], &a[j]);
    //Now swap the next node of current low with pivot
    swap(&a[i+1], &a[high]);
    printf("\n Pivot : %d\n", a[i+1]);
    for(int j=0; j<=high; j++){
    	printf("%d ", a[j]);
    //return current low as partitioning point.
    return i+1;
/* A recursive implementation of quicksort for linked list */
void quickSortUtil(int a[], int low, int high)
    if (low < high)
        int p = partition(a,low, high);
        quickSortUtil(a,low, p-1);
        quickSortUtil(a, p+1, high);
/* Driver program to run above code */
int main(){
    int a[] = {5,4,2,7,9,1,6,10,8};
    int size = sizeof(a)/sizeof(a[0]);
    quickSortUtil(a, 0, size-1);
    for(int i=0; i<size; i++){
    	printf("%d ", a[i]);
    return 0;

Complexity analysis of quick sort algorithm

If pivot splits original array into two equal parts (which is the intention), complexity of quick sort is O(n log n). However, worst case complexity of quick sort happens when input array is already sorted in increasing or decreasing order. In this case, array is partitioned into two subarrays, one with size 1 and other with size n-1. Similarly, subarray with n-1 elements, it again is divided into two subarrays of size 1 and n-2. In order to completely sort array it will split for n-1 times and each time it requires to traverse n element to find correct position of pivot. Hence overall complexity of quick sort comes out as O(n2).

There is a very interesting question, which tests your understanding of system basics. Question is what is space complexity of this algorithm? There is no apparent memory is used. However, recursive implementation internally puts stack frames on stack for partitioned indices and function call return address and so on. In worst case, there can be n stack frames, hence worst case complexity of quick sort will be O(n).

How can we reduce that? If the partition with fewest elements is (recursively) sorted first, it requires at most O(log n) space. Then the other partition is sorted using tail recursion or iteration, which doesn’t add to the call stack. This idea, was described by R. Sedgewick, and keeps the stack depth bounded by O(log n) and hence space complexity will be O(log n).

Quick sort with tail recursion

Quicksort(A, p, r)
 while (p < r)
  q = Partition(A, p, r)
  Quicksort(A, p, q)
  p = q+1

Selection of Pivot
If array is completely sorted, then worst case behavior of quick sort is O(n2), so there comes another problem. How can we select pivot so that two subarrays are almost equal size. There are many solutions proposed.
1. Taking median of array as pivot. So how to select median of an unsorted array. We look into this problem separately, but yes it guarantees two halves of same size.
2. Selecting pivot randomly. This requires heuristics algorithms to select pivot.

Please leave your comment in case you find something wrong or you have some improved version.

Arranging people according to country of origin : Count sort in play


Let’s consider a problem where we have nationals of different countries in a room and our task is to arrange these nationals in such a way that all the nationals of same country are clubbed together.
Simple application of count sort isn’t? Count nationals of each country and place them in order as discussed in previous post.


Now let’s add some complexity. What if we need all nationals of country we discovered early to be before the nationals of country we encountered later?
So input be
A = [I,I,I, U,U,S, S, E,E,E,M,M,M,I, U,M,E]
Output should be
A = [I,I,I,I, U,U,U,S,S,E,E,E,E, M,M,M]


So there is additional information we need to track that what was the order of the visiting country while traversing the country and rest all follows as per the count sort.
How do we keep track of order?
We can keep an array of countries and put countries in such a way that index of first encounter country is lower than the country later. We can do this while counting the nationals of each country by checking that if this was the first national of this country we encountered then we can go and add the country in country array.

Complete code

void arrange(int a[], int n, int K){
            n is number of people
            K is no of distinct countries
        char country[K];
        int  count[K];
        int i,j;
        int index =0;
        int next_index = 0;

        for(i=0;i<=K; i++){
                count[i] =0;

                if(count[a[i]] == 0){
                   If this is the first person for that country, add that country to our country array
                        country[index++] = a[i];

        for(i=0; i<index;i++){
                j = next_index;
                next_index += count[country[i]];
                for(; j<next_index; j++){
                        a[j] = country[i];




It is fairly easy to apply count sort on any arranging problem where we have a certain attribute we need to arrange input on.

Count sort : Sorting in linear time

Count sort : Sorting in linear time

Are there any sorting algorithm which has worst case complexity of O(n)? There are a few like count sort and decision tree algorithms. In this post we would discuss about count sort and couple of problems where this counting sort algorithm can be applied.

Counting sort was invented by Harold H. Seward
To apply counting sort, we need to keep in mind following assumptions:

  1. There should be duplicate values in the input
  2. There should be at most K different type of values in input.
  3. The input data ranges in 0 to K

Count sort goes for O(n2) if K is very close to n i.e. a very few elements are duplicate and rest all are unique. So above three conditions are necessary to have this algorithm work in linear time.

Count Sort -Approach
Let’s see how does it work? There are three steps involved in the algorithm:

  • Sampling the data, counting the frequencies of each element in the input set.
  • Accumulating frequencies to find out relative positions of each element.
  • Third step distributes each element to its appropriate place.
  • Now let’s take one example and go through the above three steps:
    Input is  an array A = [1,2,3,2,2,3,3,1,1,1,1,3,2,2]. Here we can see that K=3. Now let’s perform the first step of the method, i.e. count the frequency of each element. We keep a hash and keep track of each element’s count. Since values are upper bound by K, we need at most K sized hash. We initialize this hash as zero. A is input array and n is the size of the array.

    int char count [K];
    for(i=0; i<K;i++){

    Count looks likes count =[5,5,4]. The complexity of this step is O(n). The second step involves the accumulation of frequencies where we add frequencies of previous elements to the current element.
    F(j) = summation of f[i] for all i=0

    F(j) = summation of f[i] for all i<=j and i>=0
       Count[i] +=count[i-1];

    The second step runs for K times hence the complexity of O(K). The third step involves distribution. Let’s create an output array called as B of size n For every element in A we check the corresponding count in count array and place the element at that position. So, first 1 will be at position 4 (there are total 5 and array is an index from 0), the first two will be placed at 9 and the first three will be at 13. Subsequently, lower positions will be filled as we encounter them in the input array.

    for(i=0; i<n;i++){
           B[--count[a[i]]] = a[i];

    This step has complexity of O(n)

    void count_sort(int a[], int n, int K){
            int count [K];
            int i,j;
            int b[n];
            for(i=0; i<=K;i++){
                    count[i] +=count[i-1];
            for(i=0; i<n;i++){
                    b[count[a[i]]] = a[i];
    int main(){
            int a[]= {0,1,1,1,0,0,0,3,3,3,4,4};
            int n  =  sizeof(a)/sizeof(a[0]);
    Iter 0 :0  0  0  0  1  0  0  0  0  0  0  0  0  0  
    Iter 1 :0  0  0  0  1  0  0  0  0  2  0  0  0  0  
    Iter 2 :0  0  0  0  1  0  0  0  0  2  0  0  0  3  
    Iter 3 :0  0  0  0  1  0  0  0  2  2  0  0  0  3  
    Iter 4 :0  0  0  0  1  0  0  2  2  2  0  0  0  3  
    Iter 5 :0  0  0  0  1  0  0  2  2  2  0  0  3  3  
    Iter 6 :0  0  0  0  1  0  0  2  2  2  0  3  3  3  
    Iter 7 :0  0  0  1  1  0  0  2  2  2  0  3  3  3  
    Iter 8 :0  0  1  1  1  0  0  2  2  2  0  3  3  3  
    Iter 9 :0  1  1  1  1  0  0  2  2  2  0  3  3  3  
    Iter 10 :1  1  1  1  1  0  0  2  2  2  0  3  3  3  
    Iter 11 :1  1  1  1  1  0  0  2  2  2  3  3  3  3  
    Iter 12 :1  1  1  1  1  0  2  2  2  2  3  3  3  3  
    Iter 13 :1  1  1  1  1  2  2  2  2  2  3  3  3  3  
    Final O/P :1  1  1  1  1  2  2  2  2  2  3  3  3  3

    Total complexity of the algorithm being O(K)+ O(n) + O(K) +O(n) = O(K+n). Please refer to the master theorem, how this is derived. Now if we see, K+n remains in order of n till the time K<n, if K goes on to n^2, the complexity of algorithm goes for polynomial time instead of linear time.

    There is immediate application of this algorithm in following problem:
    Let’s there is an array which contains Black, White and Red balls, we need to arrange these balls in such a way that all black balls come first, white second and Red at last. Hint: assign black 0, white 1 and Red 2 and see. 🙂

    In the next post, we would discuss how extra space can be saved, how initial positions of elements can be maintained. We would go through an interesting problem to discuss the above optimization.

    Why this filling from the end of the span? Because this step makes count sort stable sort. Here we have seen a sorting algorithm which can give us o/p linear time given some particular conditions met on the i/p data.