|
| 1 | +## Description |
| 2 | + |
| 3 | +In QuickSort we first partition the array in place such that all elements to the left of the pivot element are smaller, while all elements to the right of the pivot are greater than the pivot. Then we recursively call the same procedure for left and right subarrays. Using a randomly generated pivot we can further improve the time complexity of QuickSort. |
| 4 | + |
| 5 | +# Code |
| 6 | + |
| 7 | +```cpp |
| 8 | +#include <cstdlib> |
| 9 | +#include <time.h> |
| 10 | +#include <iostream> |
| 11 | +using namespace std; |
| 12 | + |
| 13 | +int partition(int arr[], int low, int high) |
| 14 | +{ |
| 15 | + // pivot |
| 16 | + int pivot = arr[high]; |
| 17 | + |
| 18 | + // Index of smaller element |
| 19 | + int i = (low - 1); |
| 20 | + |
| 21 | + for (int j = low; j <= high - 1; j++) |
| 22 | + { |
| 23 | + // If current element is smaller |
| 24 | + // than or equal to pivot |
| 25 | + if (arr[j] <= pivot) { |
| 26 | + |
| 27 | + // increment index of |
| 28 | + // smaller element |
| 29 | + i++; |
| 30 | + swap(arr[i], arr[j]); |
| 31 | + } |
| 32 | + } |
| 33 | + swap(arr[i + 1], arr[high]); |
| 34 | + return (i + 1); |
| 35 | +} |
| 36 | + |
| 37 | +// Generates Random Pivot, swaps pivot with |
| 38 | +// end element and calls the partition function |
| 39 | +int partition_r(int arr[], int low, int high) |
| 40 | +{ |
| 41 | + // Generate a random number in between |
| 42 | + // low .. high |
| 43 | + srand(time(NULL)); |
| 44 | + int random = low + rand() % (high - low); |
| 45 | + |
| 46 | + // Swap A[random] with A[high] |
| 47 | + swap(arr[random], arr[high]); |
| 48 | + |
| 49 | + return partition(arr, low, high); |
| 50 | +} |
| 51 | + |
| 52 | +/* The main function that implements |
| 53 | +QuickSort |
| 54 | +arr[] --> Array to be sorted, |
| 55 | +low --> Starting index, |
| 56 | +high --> Ending index */ |
| 57 | +void quickSort(int arr[], int low, int high) |
| 58 | +{ |
| 59 | + if (low < high) { |
| 60 | + |
| 61 | + /* pi is partitioning index, |
| 62 | + arr[p] is now |
| 63 | + at right place */ |
| 64 | + int pi = partition_r(arr, low, high); |
| 65 | + |
| 66 | + // Separately sort elements before |
| 67 | + // partition and after partition |
| 68 | + quickSort(arr, low, pi - 1); |
| 69 | + quickSort(arr, pi + 1, high); |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +/* Function to print an array */ |
| 74 | +void printArray(int arr[], int size) |
| 75 | +{ |
| 76 | + int i; |
| 77 | + for (i = 0; i < size; i++) |
| 78 | + cout<<arr[i]<<" "; |
| 79 | +} |
| 80 | + |
| 81 | +// Driver Code |
| 82 | +int main() |
| 83 | +{ |
| 84 | + int arr[] = { 10, 7, 8, 9, 1, 5 }; |
| 85 | + int n = sizeof(arr) / sizeof(arr[0]); |
| 86 | + |
| 87 | + quickSort(arr, 0, n - 1); |
| 88 | + printf("Sorted array: \n"); |
| 89 | + printArray(arr, n); |
| 90 | + |
| 91 | + return 0; |
| 92 | +} |
| 93 | +``` |
| 94 | +
|
| 95 | +## Run Code |
| 96 | +https://ide.geeksforgeeks.org/bd2d6f18-3d4a-4ca0-a0a8-492c16d40c7e |
| 97 | +
|
| 98 | +## Complexities |
| 99 | +### Time complexity : |
| 100 | +Best Case : O(nlogn) |
| 101 | +Worst Case : O(n^2) |
| 102 | +### Space complexity : |
| 103 | +O(N) |
0 commit comments