TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
random_pivot_quick_sort.cpp
Go to the documentation of this file.
1
47#include <algorithm>
48#include <array>
49#include <cassert>
50#include <ctime>
51#include <iostream>
52#include <tuple>
53
58namespace sorting {
72template <size_t T>
73void showArray(std::array<int64_t, T> arr) {
74 for (int64_t i = 0; i < arr.size(); i++) {
75 std::cout << arr[i] << " ";
76 }
77 std::cout << std::endl;
78}
79
88int64_t getRandomIndex(int64_t start, int64_t end) {
89 srand(time(nullptr)); // Initialize random number generator.
90 int64_t randomPivotIndex = start + rand() % (end - start + 1);
91 return randomPivotIndex;
92}
93
102template <size_t size>
103std::tuple<int64_t, std::array<int64_t, size>> partition(
104 std::array<int64_t, size> arr, int64_t start, int64_t end) {
105 int64_t pivot = arr[end]; // Randomly selected element will be here from
106 // caller function (quickSortRP()).
107 int64_t pInd = start;
108
109 for (int64_t i = start; i < end; i++) {
110 if (arr[i] <= pivot) {
111 std::swap(arr[i], arr[pInd]); // swapping the elements from current
112 // index to pInd.
113 pInd++;
114 }
115 }
116 std::swap(arr[pInd],
117 arr[end]); // swapping the pivot element to its sorted position
118 return std::make_tuple(pInd, arr);
119}
120
129template <size_t size>
130std::array<int64_t, size> quickSortRP(std::array<int64_t, size> arr,
131 int64_t start, int64_t end) {
132 if (start < end) {
133 int64_t randomIndex = getRandomIndex(start, end);
134
135 // switching the pivot with right most bound.
136 std::swap(arr[end], arr[randomIndex]);
137
138 int64_t pivotIndex = 0;
139 // getting pivot index and pivot sorted array.
140 std::tie(pivotIndex, arr) = partition(arr, start, end);
141
142 // Recursively calling
143 std::array<int64_t, arr.size()> rightSortingLeft =
144 quickSortRP(arr, start, pivotIndex - 1);
145 std::array<int64_t, arr.size()> full_sorted =
146 quickSortRP(rightSortingLeft, pivotIndex + 1, end);
147 arr = full_sorted;
148 }
149 return arr;
150}
151
159template <size_t size>
160std::array<int64_t, size> generateUnsortedArray(int64_t from, int64_t to) {
161 srand(time(nullptr));
162 std::array<int64_t, size> unsortedArray{};
163 assert(from < to);
164 int64_t i = 0;
165 while (i < size) {
166 int64_t randomNum = from + rand() % (to - from + 1);
167 if (randomNum) {
168 unsortedArray[i] = randomNum;
169 i++;
170 }
171 }
172 return unsortedArray;
173}
174
175} // namespace random_pivot_quick_sort
176} // namespace sorting
177
181class TestCases {
182 private:
188 template <typename T>
189 void log(T msg) {
190 // It's just to avoid writing cout and endl
191 std::cout << "[TESTS] : ---> " << msg << std::endl;
192 }
193
194 public:
199 void runTests() {
200 log("Running Tests...");
201
202 testCase_1();
203 testCase_2();
204 testCase_3();
205
206 log("Test Cases over!");
207 std::cout << std::endl;
208 }
209
214 void testCase_1() {
215 const int64_t inputSize = 1;
216 log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
217 "~");
218 log("This is test case 1 for Random Pivot Quick Sort Algorithm : ");
219 log("Description:");
220 log(" EDGE CASE : Only contains one element");
221 std::array<int64_t, inputSize> unsorted_arr{2};
222
223 int64_t start = 0;
224 int64_t end = unsorted_arr.size() - 1; // length - 1
225
226 log("Running algorithm of data of length 50 ...");
227 std::array<int64_t, unsorted_arr.size()> sorted_arr =
228 sorting::random_pivot_quick_sort::quickSortRP(unsorted_arr, start,
229 end);
230 log("Algorithm finished!");
231
232 log("Checking assert expression...");
233 assert(std::is_sorted(sorted_arr.begin(), sorted_arr.end()));
234 log("Assertion check passed!");
235
236 log("[PASS] : TEST CASE 1 PASS!");
237 log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
238 "~");
239 }
240
245 void testCase_2() {
246 const int64_t inputSize = 500;
247 log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
248 "~");
249 log("Description:");
250 log(" BIG INPUT : Contains 500 elements and repeated elements");
251 log("This is test case 2 for Random Pivot Quick Sort Algorithm : ");
252 std::array<int64_t, inputSize> unsorted_arr =
253 sorting::random_pivot_quick_sort::generateUnsortedArray<inputSize>(
254 1, 10000);
255
256 int64_t start = 0;
257 int64_t end = unsorted_arr.size() - 1; // length - 1
258
259 log("Running algorithm of data of length 500 ...");
260 std::array<int64_t, unsorted_arr.size()> sorted_arr =
261 sorting::random_pivot_quick_sort::quickSortRP(unsorted_arr, start,
262 end);
263 log("Algorithm finished!");
264
265 log("Checking assert expression...");
266 assert(std::is_sorted(sorted_arr.begin(), sorted_arr.end()));
267 log("Assertion check passed!");
268
269 log("[PASS] : TEST CASE 2 PASS!");
270 log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
271 "~");
272 }
273
278 void testCase_3() {
279 const int64_t inputSize = 1000;
280 log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
281 "~");
282 log("This is test case 3 for Random Pivot Quick Sort Algorithm : ");
283 log("Description:");
284 log(" LARGE INPUT : Contains 1000 elements and repeated elements");
285 std::array<int64_t, inputSize> unsorted_arr =
286 sorting::random_pivot_quick_sort::generateUnsortedArray<inputSize>(
287 1, 10000);
288
289 int64_t start = 0;
290 int64_t end = unsorted_arr.size() - 1; // length - 1
291
292 log("Running algorithm...");
293 std::array<int64_t, unsorted_arr.size()> sorted_arr =
294 sorting::random_pivot_quick_sort::quickSortRP(unsorted_arr, start,
295 end);
296 log("Algorithm finished!");
297
298 log("Checking assert expression...");
299 assert(std::is_sorted(sorted_arr.begin(), sorted_arr.end()));
300 log("Assertion check passed!");
301
302 log("[PASS] : TEST CASE 3 PASS!");
303 log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
304 "~");
305 }
306};
307
312static void test() {
313 TestCases tc = TestCases();
314 tc.runTests();
315}
316
323int main(int argc, char *argv[]) {
324 test(); // Executes various test cases.
325
326 const int64_t inputSize = 10;
327 std::array<int64_t, inputSize> unsorted_array =
328 sorting::random_pivot_quick_sort::generateUnsortedArray<inputSize>(
329 50, 1000);
330 std::cout << "Unsorted array is : " << std::endl;
331 sorting::random_pivot_quick_sort::showArray(unsorted_array);
332
333 std::array<int64_t, inputSize> sorted_array =
334 sorting::random_pivot_quick_sort::quickSortRP(
335 unsorted_array, 0, unsorted_array.size() - 1);
336 std::cout << "Sorted array is : " << std::endl;
337 sorting::random_pivot_quick_sort::showArray(sorted_array);
338 return 0;
339}
class encapsulating the necessary test cases
void log(T msg)
A function to print64_t given message on console.
void testCase_2()
A test case which contains main list of 100 elements and sublist of 20.
void testCase_1()
A test case contains edge case, printing inorder successor of last node.
void testCase_3()
A test case which contains main list of 50 elements and sublist of 20.
void runTests()
Executes test cases.
int main()
Main function.
Functions for the Random Pivot Quick Sort implementation.
for working with vectors
std::array< int64_t, size > generateUnsortedArray(int64_t from, int64_t to)
A function utility to generate unsorted array of given size and range.
std::array< int64_t, size > quickSortRP(std::array< int64_t, size > arr, int64_t start, int64_t end)
Random pivot quick sort function. This function is the starting point of the algorithm.
static void test()
Self-test implementations.
int64_t getRandomIndex(int64_t start, int64_t end)
Takes the start and end indices of an array and returns a random int64_teger between the range of tho...
void showArray(std::array< int64_t, T > arr)
Utility function to print the array.