TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
quick_sort.cpp
Go to the documentation of this file.
1
27#include <algorithm>
28#include <cassert>
29#include <cstdint>
30#include <ctime>
31#include <iostream>
32#include <vector>
33
38namespace sorting {
44namespace quick_sort {
58template <typename T>
59int partition(std::vector<T> *arr, const int &low, const int &high) {
60 T pivot = (*arr)[high]; // taking the last element as pivot
61 int i = (low - 1); // Index of smaller element
62
63 for (int j = low; j < high; j++) {
64 // If current element is smaller than or
65 // equal to pivot
66 if ((*arr)[j] <= pivot) {
67 i++; // increment index of smaller element
68 std::swap((*arr)[i], (*arr)[j]);
69 }
70 }
71
72 std::swap((*arr)[i + 1], (*arr)[high]);
73 return (i + 1);
74}
75
86template <typename T>
87void quick_sort(std::vector<T> *arr, const int &low, const int &high) {
88 if (low < high) {
89 int p = partition(arr, low, high);
90
91 quick_sort(arr, low, p - 1);
92 quick_sort(arr, p + 1, high);
93 }
94}
95
106template <typename T>
107std::vector<T> quick_sort(std::vector<T> arr, const int &low, const int &high) {
108 if (low < high) {
109 int p = partition(&arr, low, high);
110
111 quick_sort(&arr, low, p - 1);
112 quick_sort(&arr, p + 1, high);
113 }
114 return arr;
115}
116
123template <typename T>
124void show(const std::vector<T> &arr, const int &size) {
125 for (int i = 0; i < size; i++) std::cout << arr[i] << " ";
126 std::cout << "\n";
127}
128
129} // namespace quick_sort
130} // namespace sorting
131
136static void tests() {
137 // 1st test (normal numbers)
138 std::vector<uint64_t> arr = {5, 3, 8, 12, 14, 16, 28, 96, 2, 5977};
139 std::vector<uint64_t> arr_sorted = sorting::quick_sort::quick_sort(
140 arr, 0, int(std::end(arr) - std::begin(arr)) - 1);
141
142 assert(std::is_sorted(std::begin(arr_sorted), std::end(arr_sorted)));
143 std::cout << "\n1st test: passed!\n";
144
145 // 2nd test (normal and negative numbers)
146 std::vector<int64_t> arr2 = {9, 15, 28, 96, 500, -4, -58,
147 -977, -238, -800, -21, -53, -55};
148 std::vector<int64_t> arr_sorted2 = sorting::quick_sort::quick_sort(
149 arr2, 0, std::end(arr2) - std::begin(arr2));
150
151 assert(std::is_sorted(std::begin(arr_sorted2), std::end(arr_sorted2)));
152 std::cout << "2nd test: passed!\n";
153
154 // 3rd test (decimal and normal numbers)
155 std::vector<double> arr3 = {29, 36, 1100, 0, 77, 1,
156 6.7, 8.97, 1.74, 950.10, -329.65};
157 std::vector<double> arr_sorted3 = sorting::quick_sort::quick_sort(
158 arr3, 0, int(std::end(arr3) - std::begin(arr3)) - 1);
159
160 assert(std::is_sorted(std::begin(arr_sorted3), std::end(arr_sorted3)));
161 std::cout << "3rd test: passed!\n";
162
163 // 4th test (random decimal and negative numbers)
164 size_t size = std::rand() % 750 + 100;
165
166 std::vector<float> arr4(size);
167 for (uint64_t i = 0; i < size; i++) {
168 arr4[i] = static_cast<float>(std::rand()) /
169 static_cast<float>(RAND_MAX / 999.99 - 0.99) -
170 250;
171 }
172
173 std::vector<float> arr4_sorted = sorting::quick_sort::quick_sort(
174 arr4, 0, int(std::end(arr4) - std::begin(arr4)) - 1);
175 assert(std::is_sorted(std::begin(arr4_sorted), std::end(arr4_sorted)));
176
177 std::cout << "4th test: passed!\n";
178
179 // Printing all sorted arrays
180 std::cout << "\n\tPrinting all sorted arrays:\t\n";
181
182 std::cout << "1st array:\n";
183 sorting::quick_sort::show(arr_sorted, std::end(arr) - std::begin(arr));
184 std::cout << std::endl;
185 std::cout << "2nd array:\n";
186 sorting::quick_sort::show(arr_sorted2, std::end(arr2) - std::begin(arr2));
187 std::cout << std::endl;
188 std::cout << "3rd array:\n";
189 sorting::quick_sort::show(arr_sorted3,
190 int(std::end(arr3) - std::begin(arr3)) - 1);
191 std::cout << std::endl;
192 std::cout << "Start: 4th array:\n\n";
193 sorting::quick_sort::show(
194 arr4_sorted, int(std::end(arr4_sorted) - std::begin(arr4_sorted)) - 1);
195 std::cout << "\nEnd: 4th array.\n";
196}
197
202int main() {
203 int choice = 0;
204
205 std::cout << "\tAvailable modes\t\n\n";
206 std::cout << "1. Self-tests mode\n2. Interactive mode";
207
208 std::cout << "\nChoose a mode: ";
209 std::cin >> choice;
210 std::cout << "\n";
211
212 while ((choice != 1) && (choice != 2)) {
213 std::cout << "Invalid option. Choose between the valid modes: ";
214 std::cin >> choice;
215 }
216
217 if (choice == 1) {
218 std::srand(std::time(nullptr));
219 tests(); // run self-test implementations
220 } else if (choice == 2) {
221 int size = 0;
222 std::cout << "\nEnter the number of elements: ";
223
224 std::cin >> size;
225 std::vector<float> arr(size);
226
227 std::cout
228 << "\nEnter the unsorted elements (can be negative/decimal): ";
229
230 for (int i = 0; i < size; ++i) {
231 std::cout << "\n";
232 std::cin >> arr[i];
233 }
234 sorting::quick_sort::quick_sort(&arr, 0, size - 1);
235 std::cout << "\nSorted array: \n";
236 sorting::quick_sort::show(arr, size);
237 }
238 return 0;
239}
Functions for the Quick sort implementation in C++.
for working with vectors
static void tests()
Self-test implementations.
int main()
Main function.