|
1 | 1 | /**
|
2 | 2 | * @file
|
3 |
| - * @brief Quick sort algorithm |
4 |
| - * |
5 |
| - * Implementation Details - |
6 |
| - * Quick Sort is a divide and conquer algorithm. It picks and element as |
7 |
| - * pivot and partition the given array around the picked pivot. There |
8 |
| - * are many different versions of quickSort that pick pivot in different |
9 |
| - * ways. |
| 3 | + * @brief [Quick sort implementation](https://en.wikipedia.org/wiki/Quicksort) in C++ |
| 4 | + * @details |
| 5 | + * Quick Sort is a [divide and conquer algorithm](https://en.wikipedia.org/wiki/Category:Divide-and-conquer_algorithms). |
| 6 | + * It picks an element as pivot and partition the given array around the picked pivot. There |
| 7 | + * are many different versions of quickSort that pick pivot in different ways. |
10 | 8 | *
|
11 | 9 | * 1. Always pick the first element as pivot
|
12 | 10 | * 2. Always pick the last element as pivot (implemented below)
|
|
19 | 17 | * than x) before x, and put all greater elements (greater than x) after
|
20 | 18 | * x. All this should be done in linear time
|
21 | 19 | *
|
| 20 | + * @author [David Leal](https://github.com/Panquesito7) |
| 21 | + * @author Unknown author |
22 | 22 | */
|
23 | 23 |
|
24 |
| -#include <cstdlib> |
25 |
| -#include <iostream> |
| 24 | +#include <algorithm> /// for std::is_sorted |
| 25 | +#include <iostream> /// for IO operations |
| 26 | +#include <vector> /// for std::vector |
| 27 | +#include <cassert> /// for std::assert |
| 28 | +#include <ctime> /// for std::time |
26 | 29 |
|
| 30 | +/** |
| 31 | + * @brief Sorting algorithms |
| 32 | + * @namespace sorting |
| 33 | + */ |
27 | 34 | namespace sorting {
|
28 | 35 | /**
|
29 |
| - * This function takes last element as pivot, places |
30 |
| - * the pivot element at its correct position in sorted |
31 |
| - * array, and places all smaller (smaller than pivot) |
32 |
| - * to left of pivot and all greater elements to right |
33 |
| - * of pivot |
34 |
| - * |
| 36 | + * @namespace quick_sort |
| 37 | + * @brief Functions for the [Quick sort implementation](https://en.wikipedia.org/wiki/Quicksort) in C++ |
35 | 38 | */
|
36 |
| - |
37 |
| -int partition(int arr[], int low, int high) { |
38 |
| - int pivot = arr[high]; // taking the last element as pivot |
| 39 | +namespace quick_sort { |
| 40 | +/** |
| 41 | + * @brief Sorts the array taking the last element as pivot |
| 42 | + * @details |
| 43 | + * This function takes last element as pivot, places |
| 44 | + * the pivot element at its correct position in sorted |
| 45 | + * array, and places all smaller (smaller than pivot) |
| 46 | + * to left of pivot and all greater elements to right of pivot |
| 47 | + * @tparam T array type |
| 48 | + * @param arr the array with contents given by the user |
| 49 | + * @param low first point of the array (starting index) |
| 50 | + * @param high last point of the array (ending index) |
| 51 | + * @returns index of the smaller element |
| 52 | + */ |
| 53 | +template <typename T> |
| 54 | +int partition(std::vector<T> *arr, const int &low, const int &high) { |
| 55 | + T pivot = (*arr)[high]; // taking the last element as pivot |
39 | 56 | int i = (low - 1); // Index of smaller element
|
40 | 57 |
|
41 | 58 | for (int j = low; j < high; j++) {
|
42 | 59 | // If current element is smaller than or
|
43 | 60 | // equal to pivot
|
44 |
| - if (arr[j] <= pivot) { |
| 61 | + if ((*arr)[j] <= pivot) { |
45 | 62 | i++; // increment index of smaller element
|
46 |
| - int temp = arr[i]; |
47 |
| - arr[i] = arr[j]; |
48 |
| - arr[j] = temp; |
| 63 | + std::swap((*arr)[i], (*arr)[j]); |
49 | 64 | }
|
50 | 65 | }
|
51 |
| - int temp = arr[i + 1]; |
52 |
| - arr[i + 1] = arr[high]; |
53 |
| - arr[high] = temp; |
| 66 | + |
| 67 | + std::swap((*arr)[i + 1], (*arr)[high]); |
54 | 68 | return (i + 1);
|
55 | 69 | }
|
56 | 70 |
|
57 | 71 | /**
|
58 |
| - * The main function that implements QuickSort |
59 |
| - * arr[] --> Array to be sorted, |
60 |
| - * low --> Starting index, |
61 |
| - * high --> Ending index |
| 72 | + * @brief the main function that implements Quick Sort. |
| 73 | + * |
| 74 | + * Void function used in T (array type) function, which then |
| 75 | + * can be used as self-tests or other functionalities. |
| 76 | + * @tparam T array type |
| 77 | + * @param arr array to be sorted |
| 78 | + * @param low starting index |
| 79 | + * @param high ending index |
62 | 80 | */
|
63 |
| -void quickSort(int arr[], int low, int high) { |
| 81 | +template <typename T> |
| 82 | +void quick_sort(std::vector<T> *arr, const int &low, const int &high) { |
64 | 83 | if (low < high) {
|
65 | 84 | int p = partition(arr, low, high);
|
66 |
| - quickSort(arr, low, p - 1); |
67 |
| - quickSort(arr, p + 1, high); |
| 85 | + |
| 86 | + quick_sort(arr, low, p - 1); |
| 87 | + quick_sort(arr, p + 1, high); |
68 | 88 | }
|
69 | 89 | }
|
70 | 90 |
|
71 |
| -} // namespace sorting |
| 91 | +/** |
| 92 | + * @brief the main function that implements Quick Sort. |
| 93 | + * |
| 94 | + * T (array type) function which calls the void function. Can |
| 95 | + * be used for self-tests and other functionalities. |
| 96 | + * @tparam T array type |
| 97 | + * @param arr array to be sorted |
| 98 | + * @param low starting index |
| 99 | + * @param high ending index |
| 100 | + */ |
| 101 | +template <typename T> |
| 102 | +std::vector<T> quick_sort(std::vector<T> arr, const int &low, const int &high) { |
| 103 | + if (low < high) { |
| 104 | + int p = partition(&arr, low, high); |
72 | 105 |
|
73 |
| -using sorting::quickSort; |
| 106 | + quick_sort(&arr, low, p - 1); |
| 107 | + quick_sort(&arr, p + 1, high); |
| 108 | + } |
| 109 | + return arr; |
| 110 | +} |
74 | 111 |
|
75 |
| -// prints the array after sorting |
76 |
| -void show(int arr[], int size) { |
| 112 | +/** |
| 113 | + * @brief Utility function to print the array contents |
| 114 | + * @param arr the array to be printed |
| 115 | + * @param size size of the given array |
| 116 | + * @returns void |
| 117 | + */ |
| 118 | +template <typename T> |
| 119 | +void show(const std::vector<T> &arr, const int &size) { |
77 | 120 | for (int i = 0; i < size; i++) std::cout << arr[i] << " ";
|
78 | 121 | std::cout << "\n";
|
79 | 122 | }
|
80 | 123 |
|
81 |
| -/** Driver program to test above functions */ |
| 124 | +} // namespace quick_sort |
| 125 | +} // namespace sorting |
| 126 | + |
| 127 | +/** |
| 128 | + * @brief Self-test implementations |
| 129 | + * @returns void |
| 130 | + */ |
| 131 | +static void tests() { |
| 132 | + // 1st test (normal numbers) |
| 133 | + std::vector<uint64_t> arr = {5, 3, 8, 12, 14, 16, 28, 96, 2, 5977}; |
| 134 | + std::vector<uint64_t> arr_sorted = sorting::quick_sort::quick_sort(arr, 0, int(std::end(arr)-std::begin(arr)) - 1); |
| 135 | + |
| 136 | + assert(std::is_sorted(std::begin(arr_sorted), std::end(arr_sorted))); |
| 137 | + std::cout << "\n1st test: passed!\n"; |
| 138 | + |
| 139 | + // 2nd test (normal and negative numbers) |
| 140 | + std::vector<int64_t> arr2 = {9, 15, 28, 96, 500, -4, -58, -977, -238, -800, -21, -53, -55}; |
| 141 | + std::vector<int64_t> arr_sorted2 = sorting::quick_sort::quick_sort(arr2, 0, std::end(arr2)-std::begin(arr2)); |
| 142 | + |
| 143 | + assert(std::is_sorted(std::begin(arr_sorted2), std::end(arr_sorted2))); |
| 144 | + std::cout << "2nd test: passed!\n"; |
| 145 | + |
| 146 | + // 3rd test (decimal and normal numbers) |
| 147 | + std::vector<double> arr3 = {29, 36, 1100, 0, 77, 1, 6.7, 8.97, 1.74, 950.10, -329.65}; |
| 148 | + std::vector<double> arr_sorted3 = sorting::quick_sort::quick_sort(arr3, 0, int(std::end(arr3)-std::begin(arr3)) - 1); |
| 149 | + |
| 150 | + assert(std::is_sorted(std::begin(arr_sorted3), std::end(arr_sorted3))); |
| 151 | + std::cout << "3rd test: passed!\n"; |
| 152 | + |
| 153 | + // 4th test (random decimal and negative numbers) |
| 154 | + size_t size = std::rand() % 750 + 100; |
| 155 | + |
| 156 | + std::vector<float> arr4(size); |
| 157 | + for (uint64_t i = 0; i < size; i++) { |
| 158 | + arr4[i] = static_cast <float> (std::rand()) / static_cast <float> (RAND_MAX / 999.99 - 0.99) - 250; |
| 159 | + } |
| 160 | + |
| 161 | + std::vector<float> arr4_sorted = sorting::quick_sort::quick_sort(arr4, 0, int(std::end(arr4) - std::begin(arr4)) - 1); |
| 162 | + assert(std::is_sorted(std::begin(arr4_sorted), std::end(arr4_sorted))); |
| 163 | + |
| 164 | + std::cout << "4th test: passed!\n"; |
| 165 | + |
| 166 | + // Printing all sorted arrays |
| 167 | + std::cout << "\n\tPrinting all sorted arrays:\t\n"; |
| 168 | + |
| 169 | + std::cout << "1st array:\n"; |
| 170 | + sorting::quick_sort::show(arr_sorted, std::end(arr)-std::begin(arr)); std::cout << std::endl; |
| 171 | + std::cout << "2nd array:\n"; |
| 172 | + sorting::quick_sort::show(arr_sorted2, std::end(arr2)-std::begin(arr2)); std::cout << std::endl; |
| 173 | + std::cout << "3rd array:\n"; |
| 174 | + sorting::quick_sort::show(arr_sorted3, int(std::end(arr3)-std::begin(arr3)) - 1); std::cout << std::endl; |
| 175 | + std::cout << "Start: 4th array:\n\n"; |
| 176 | + sorting::quick_sort::show(arr4_sorted, int(std::end(arr4_sorted) - std::begin(arr4_sorted)) - 1); |
| 177 | + std::cout << "\nEnd: 4th array.\n"; |
| 178 | +} |
| 179 | + |
| 180 | +/** |
| 181 | + * @brief Main function |
| 182 | + * @returns 0 on exit |
| 183 | + */ |
82 | 184 | int main() {
|
83 |
| - int size; |
84 |
| - std::cout << "\nEnter the number of elements : "; |
| 185 | + int choice = 0; |
85 | 186 |
|
86 |
| - std::cin >> size; |
| 187 | + std::cout << "\tAvailable modes\t\n\n"; |
| 188 | + std::cout << "1. Self-tests mode\n2. Interactive mode"; |
87 | 189 |
|
88 |
| - int *arr = new int[size]; |
| 190 | + std::cout << "\nChoose a mode: "; |
| 191 | + std::cin >> choice; std::cout << "\n"; |
89 | 192 |
|
90 |
| - std::cout << "\nEnter the unsorted elements : "; |
| 193 | + while ((choice != 1) && (choice != 2)) { |
| 194 | + std::cout << "Invalid option. Choose between the valid modes: "; |
| 195 | + std::cin >> choice; |
| 196 | + } |
91 | 197 |
|
92 |
| - for (int i = 0; i < size; ++i) { |
93 |
| - std::cout << "\n"; |
94 |
| - std::cin >> arr[i]; |
| 198 | + if (choice == 1) { |
| 199 | + std::srand(std::time(nullptr)); |
| 200 | + tests(); // run self-test implementations |
| 201 | + } |
| 202 | + else if (choice == 2) { |
| 203 | + int size = 0; |
| 204 | + std::cout << "\nEnter the number of elements: "; |
| 205 | + |
| 206 | + std::cin >> size; |
| 207 | + std::vector<float> arr(size); |
| 208 | + |
| 209 | + std::cout << "\nEnter the unsorted elements (can be negative/decimal): "; |
| 210 | + |
| 211 | + for (int i = 0; i < size; ++i) { |
| 212 | + std::cout << "\n"; |
| 213 | + std::cin >> arr[i]; |
| 214 | + } |
| 215 | + sorting::quick_sort::quick_sort(&arr, 0, size - 1); |
| 216 | + std::cout << "\nSorted array: \n"; |
| 217 | + sorting::quick_sort::show(arr, size); |
95 | 218 | }
|
96 |
| - quickSort(arr, 0, size); |
97 |
| - std::cout << "Sorted array\n"; |
98 |
| - show(arr, size); |
99 |
| - delete[] arr; |
100 | 219 | return 0;
|
101 | 220 | }
|
0 commit comments