# Quick sort Algorithm

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

### Selection of pivot

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

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

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

At the same time, start from end of the 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

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

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

From end of the array, move towards left till you find an element that is less than the 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.

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

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

Since `i > j`, then 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 a boundary. All elements on the left side of the pivot are smaller (they may not be sorted) and all elements on the right side of the pivot are greater than pivot (again may not be sorted).

We, apply this same partition process to left and right arrays again, till the base condition is hit. In this case, the base condition would be if there is only one element in the 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

```package AlgorithmsAndMe;

public class QuickSort {

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

private int partition(int [] a, int start, int end){
int pivot = a[start];
int i  = start;
int j  = end;

while(i < j){
while(i <= end && a[i] <= pivot) i++;
while(j >= start && a[j] > pivot) j--;

if(i < j) {
swap(a, i, j);
}
}

swap(a, start, j);
return j;
}

public 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);
}
}
}

```

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

```#include<stdlib.h>
#include<stdio.h>

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
i++;
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 the original array into two equal parts (which is the intention), the complexity of quicksort is O(nlogn). However, worst-case complexity of quick sort happens when the 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 the array, it will split for n-1 times, and each time it requires to traverse n element to find the 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 the space complexity of this algorithm? There is no apparent memory is used. However, recursive implementation internally puts stack frames on the stack for partitioned indices and function call return addresses and so on. In the worst case, there can be n stack frames, hence worst-case complexity of quicksort 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 an array is completely sorted, then the worst-case behavior of quicksort 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 the array as a pivot. So how to select the median of an unsorted array. We look into this problem separately, but yes it guarantees two halves of the 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.