74 for (int64_t i = 0; i < arr.size(); i++) {
75 std::cout << arr[i] <<
" ";
77 std::cout << std::endl;
90 int64_t randomPivotIndex = start + rand() % (end - start + 1);
91 return randomPivotIndex;
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];
107 int64_t pInd = start;
109 for (int64_t i = start; i < end; i++) {
110 if (arr[i] <= pivot) {
111 std::swap(arr[i], arr[pInd]);
118 return std::make_tuple(pInd, arr);
129template <
size_t size>
130std::array<int64_t, size>
quickSortRP(std::array<int64_t, size> arr,
131 int64_t start, int64_t end) {
136 std::swap(arr[end], arr[randomIndex]);
138 int64_t pivotIndex = 0;
140 std::tie(pivotIndex, arr) = partition(arr, start, end);
143 std::array<int64_t, arr.size()> rightSortingLeft =
145 std::array<int64_t, arr.size()> full_sorted =
146 quickSortRP(rightSortingLeft, pivotIndex + 1, end);
159template <
size_t size>
161 srand(time(
nullptr));
162 std::array<int64_t, size> unsortedArray{};
166 int64_t randomNum = from + rand() % (to - from + 1);
168 unsortedArray[i] = randomNum;
172 return unsortedArray;
188 template <
typename T>
191 std::cout <<
"[TESTS] : ---> " << msg << std::endl;
200 log(
"Running Tests...");
206 log(
"Test Cases over!");
207 std::cout << std::endl;
215 const int64_t inputSize = 1;
216 log(
"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
218 log(
"This is test case 1 for Random Pivot Quick Sort Algorithm : ");
220 log(
" EDGE CASE : Only contains one element");
221 std::array<int64_t, inputSize> unsorted_arr{2};
224 int64_t end = unsorted_arr.size() - 1;
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,
230 log(
"Algorithm finished!");
232 log(
"Checking assert expression...");
233 assert(std::is_sorted(sorted_arr.begin(), sorted_arr.end()));
234 log(
"Assertion check passed!");
236 log(
"[PASS] : TEST CASE 1 PASS!");
237 log(
"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
246 const int64_t inputSize = 500;
247 log(
"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
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>(
257 int64_t end = unsorted_arr.size() - 1;
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,
263 log(
"Algorithm finished!");
265 log(
"Checking assert expression...");
266 assert(std::is_sorted(sorted_arr.begin(), sorted_arr.end()));
267 log(
"Assertion check passed!");
269 log(
"[PASS] : TEST CASE 2 PASS!");
270 log(
"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
279 const int64_t inputSize = 1000;
280 log(
"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
282 log(
"This is test case 3 for Random Pivot Quick Sort Algorithm : ");
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>(
290 int64_t end = unsorted_arr.size() - 1;
292 log(
"Running algorithm...");
293 std::array<int64_t, unsorted_arr.size()> sorted_arr =
294 sorting::random_pivot_quick_sort::quickSortRP(unsorted_arr, start,
296 log(
"Algorithm finished!");
298 log(
"Checking assert expression...");
299 assert(std::is_sorted(sorted_arr.begin(), sorted_arr.end()));
300 log(
"Assertion check passed!");
302 log(
"[PASS] : TEST CASE 3 PASS!");
303 log(
"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
323int main(
int argc,
char *argv[]) {
326 const int64_t inputSize = 10;
327 std::array<int64_t, inputSize> unsorted_array =
328 sorting::random_pivot_quick_sort::generateUnsortedArray<inputSize>(
330 std::cout <<
"Unsorted array is : " << std::endl;
331 sorting::random_pivot_quick_sort::showArray(unsorted_array);
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);
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.
Functions for the Random Pivot Quick Sort implementation.
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.