Need to use and measure the time it takes a randomized array to be sorted. I am getting a stack overflow error as shown here:
Unhandled exception at 0x00007FF64EEC2634 in COM301 Lab 3.exe: 0xC00000FD: Stack overflow (parameters: 0x0000000000000001, 0x0000004DB18B3FC8).
I think there is probably a few issues with my code, shown below. The quicksort is not exactly perfect but this was how I got it to work for smaller arrays. The error pops up in swap, but its not an issue with that I think. Here is the code:
#include <ctime>
#include <chrono>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#ifndef QuickSort
using namespace std;
static void swap(int arr[], int& x, int& y) {
int temp = arr[x];
arr[x] = arr[y];
arr[y] = temp;
}
void minMax(int arr[], const int& size, int& min, int& max) {
for (int i = 0; i < size; i++) {
if (arr[i] < min)
min = arr[i];
if (arr[i] > max)
max = arr[i];
}
}
//QuickSort:____________________________________________________________________________________________________________________________________________
//partitioning for quicksort
int partition(int arr[], int start, int end) {
int pivot = arr[start]; //array is randomly generated, so pivot selection is trivial
int i = start + 1;
int j = end;
while (i <= j) { //While I is less than J, increment closer to the pivot until a suitable switch is found.
while (i <= j && arr[i] < pivot)
i++;
while (i <= j && arr[j] > pivot)
j--;
if (i < j)
swap(arr, i, j); //suitable match found, switch i and j only if theyre not out of scope of their proper placement
else
break;
}
swap(arr, start, j); //put pivot in its correct position
return j;
}
void quickSort(int arr[], int start, int end) {
if (start + 1 < end) { //as long as the array length is more than 1, continue recursing
int p = partition(arr, start, end);
//right side recursion
quickSort(arr, p + 1, end);
//left side recursion
quickSort(arr, start, p);
}
}
//MergeSort:_________________________________________________________________________________________________________
int main() {
//initialization of arrays, and array qualities
const int size = 250;
int max = 0;
int min = 999999999;
int arr[size];
int arrC1[size], arrC2[size], arrC3[size], arrC4[size];
//Random generation seeded by the clock, as well as initialization of start/stop time variable
srand(static_cast<unsigned>(time(0)));
clock_t startTime, endTime;
clock_t timeElapsed;
// Creation and printing of unsorted array
for (int i = 0; i < size; i++) {
arr[i] = rand();
}
cout << "Randomly generated array(unsorted): \n";
for(int i = 0; i < size; i++)
cout << arr[i] << endl;
//copying of array to be used by sorting algorithms
for (int i = 0; i < size; i++) {
arrC1[i] = arr[i];
}
for (int i = 0; i < size; i++) {
arrC2[i] = arr[i];
}
for (int i = 0; i < size; i++) {
arrC3[i] = arr[i];
}
for (int i = 0; i < size; i++) {
arrC4[i] = arr[i];
}
//Run and print Quick sort
startTime = clock();
quickSort(arrC1, 0, size - 1);
endTime = clock();
timeElapsed = (endTime - startTime) * 1000;
cout << "Start Time: " << startTime;
cout << "\n End Time: " << endTime;
cout << "\n \n \n Quick Sorted Array: \n";
for (int i = 0; i < size; i++)
cout << arrC1[i] << endl;
cout << "Time Elapsed: " << timeElapsed;
}
I am a student, looking to get into the software engineering industry, so please be gentle with me. I would like to learn how to fix this in simple ways that are easy to understand if possible (I have some experience with programming but not much at this level). Additionally, I have issues with the clock() function so if anyone could show me an up to date fix that would be great. I need it in milliseconds/nanoseconds because seconds is way too big a number to measure.
Note: The code is very half-baked rn, I am just trying to fix the quick sort atm. I need to eventually implement merge sort, selection sort, etc. No help needed for that I just thought I'd mention it lol.
I've tried making swap static(didn't think it'd help but vss recommended it). I have also messed around with trying to make the integers used in the functions pointers to reduce memory usage but it messed with the operations done on them later. I've also messed with making the left side recursion end at p - 1 since I know that it is how quick sort is supposed to work (but it breaks what I have). I messed with the if (start + 1 < end) of the quickSort function but IDK how to properly optimize it. I've also tried setting the pivot differently but anything fancy I tried broke the program, even at the smaller array level. Really though I'm sure its something dumb that someone who knows quick sort could show me. If you can I'd really appreciate the help, thanks!