# Quick Sort Implementation C++14

Following are the implementations of QuickSort:

• C++14

`/* C++ implementation of QuickSort */`

`#include <bits/stdc++.h>`

`using` `namespace` `std;`

`// A utility function to swap two elements`

`void` `swap(` `int` `* a, ` `int` `* b)`

`{`

` ` `int` `t = *a;`

` ` `*a = *b;`

` ` `*b = t;`

`}`

`/* 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 */`

`int` `partition (` `int` `arr[], ` `int` `low, ` `int` `high)`

`{`

` ` `int` `pivot = arr[high]; ` `// pivot`

` ` `int` `i = (low - 1); ` `// Index of smaller element and indicates the right position of pivot found so far`

` ` `for` `(` `int` `j = low; j <= high - 1; j++)`

` ` `{`

` ` `// If current element is smaller than the pivot`

` ` `if` `(arr[j] < pivot)`

` ` `{`

` ` `i++; ` `// increment index of smaller element`

` ` `swap(&arr[i], &arr[j]);`

` ` `}`

` ` `}`

` ` `swap(&arr[i + 1], &arr[high]);`

` ` `return` `(i + 1);`

`}`

`/* The main function that implements QuickSort`

`arr[] --> Array to be sorted,`

`low --> Starting index,`

`high --> Ending index */`

`void` `quickSort(` `int` `arr[], ` `int` `low, ` `int` `high)`

`{`

` ` `if` `(low < high)`

` ` `{`

` ` `/* pi is partitioning index, arr[p] is now`

` ` `at right place */`

` ` `int` `pi = partition(arr, low, high);`

` ` `// Separately sort elements before`

` ` `// partition and after partition`

` ` `quickSort(arr, low, pi - 1);`

` ` `quickSort(arr, pi + 1, high);`

` ` `}`

`}`

`/* Function to print an array */`

`void` `printArray(` `int` `arr[], ` `int` `size)`

`{`

` ` `int` `i;`

` ` `for` `(i = 0; i < size; i++)`

` ` `cout << arr[i] << ` `" "` `;`

` ` `cout << endl;`

`}`

`// Driver Code`

`int` `main()`

`{`

` ` `int` `arr[] = {10, 7, 8, 9, 1, 5};`

` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr);`

` ` `quickSort(arr, 0, n - 1);`

` ` `cout << ` `"Sorted array: \n"` `;`

` ` `printArray(arr, n);`

` ` `return` `0;`

`}`

Output

Sorted array: 1 5 7 8 9 10

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)

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 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.