Loading...
Searching...
No Matches
Implementation of the Random Pivot Quick Sort algorithm. More...
#include <algorithm>
#include <array>
#include <cassert>
#include <ctime>
#include <iostream>
#include <tuple>
Go to the source code of this file.
template<size_t T> void sorting::random_pivot_quick_sort::showArray (std::array< int64_t, T > arr) Utility function to print the array.Implementation of the Random Pivot Quick Sort algorithm.
### Logic * The logic is pretty simple, the only change is in thepartitioning algorithm, which is selecting the pivot element.
### Partition Logic * Partitions are done such as numbers lower than the "pivot"element is arranged on the left side of the "pivot", and number larger than the "pivot" element are arranged on the right part of the array.
### Algorithm * Select the pivot element randomly using getRandomIndex() functionfrom this namespace.
Initialize the pInd (partition index) from the start of the array.
Definition in file random_pivot_quick_sort.cpp.
◆ generateUnsortedArray()template<size_t size>
std::array< int64_t, size > sorting::random_pivot_quick_sort::generateUnsortedArray ( int64_t from, int64_t to )A function utility to generate unsorted array of given size and range.
Definition at line 160 of file random_pivot_quick_sort.cpp.
160 {
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}
◆ getRandomIndex() int64_t sorting::random_pivot_quick_sort::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 those two for selecting pivot element.
Definition at line 88 of file random_pivot_quick_sort.cpp.
88 {
89 srand(time(nullptr));
90 int64_t randomPivotIndex = start + rand() % (end - start + 1);
91 return randomPivotIndex;
92}
◆ main()Main function.
Definition at line 321 of file random_pivot_quick_sort.cpp.
321 {
323
324 const int64_t inputSize = 10;
325 std::array<int64_t, inputSize> unsorted_array =
327 50, 1000);
328 std::cout << "Unsorted array is : " << std::endl;
330
331 std::array<int64_t, inputSize> sorted_array =
333 unsorted_array, 0, unsorted_array.size() - 1);
334 std::cout << "Sorted array is : " << std::endl;
336 return 0;
337}
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.
void showArray(std::array< int64_t, T > arr)
Utility function to print the array.
◆ partition()template<size_t size>
std::tuple< int64_t, std::array< int64_t, size > > sorting::random_pivot_quick_sort::partition ( std::array< int64_t, size > arr, int64_t start, int64_t end )A partition function which handles the partition logic of quick sort.
Definition at line 103 of file random_pivot_quick_sort.cpp.
104 {
105 int64_t pivot = arr[end];
106
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]);
112
113 pInd++;
114 }
115 }
116 std::swap(arr[pInd],
117 arr[end]);
118 return std::make_tuple(pInd, arr);
119}
◆ quickSortRP()template<size_t size>
std::array< int64_t, size > sorting::random_pivot_quick_sort::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.
Definition at line 130 of file random_pivot_quick_sort.cpp.
131 {
132 if (start < end) {
134
135
136 std::swap(arr[end], arr[randomIndex]);
137
138 int64_t pivotIndex = 0;
139
140std::tie(pivotIndex, arr) =
partition(arr, start, end);
141
142
143 std::array<int64_t, arr.size()> rightSortingLeft =
145 std::array<int64_t, arr.size()> full_sorted =
146 quickSortRP(rightSortingLeft, pivotIndex + 1, end);
147 arr = full_sorted;
148 }
149 return arr;
150}
std::tuple< int64_t, std::array< int64_t, size > > partition(std::array< int64_t, size > arr, int64_t start, int64_t end)
A partition function which handles the partition logic of quick sort.
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...
◆ showArray()template<size_t T>
void sorting::random_pivot_quick_sort::showArray ( std::array< int64_t, T > arr )Utility function to print the array.
Definition at line 73 of file random_pivot_quick_sort.cpp.
73 {
74 for (int64_t i = 0; i < arr.size(); i++) {
75 std::cout << arr[i] << " ";
76 }
77 std::cout << std::endl;
78}
◆ test()Self-test implementations.
Definition at line 312 of file random_pivot_quick_sort.cpp.
312 {
315}
class encapsulating the necessary test cases
void runTests()
Executes test cases.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4