I'm working on a project for class where I have to implement different sorting algorithms. One of them is a randomized quicksort, but my random number is always set to -800 million even after setting srand and setting a scope of numbers to pick from, causing a stack overflow error.
I've tried everything I can really think of, which isn't much. I tried making a random number generator in another cpp file and it works fine, it just doesn't work in this context for some reason. I'm just not understanding how it's not properly generating a random number.
void randomizedQuickSort(int arr3[], int start, int end)
{
int temp, pivot;
int s = start, e = end;
srand(time(0)); //<------------------------ ERROR THROWN
// Partitioning
while (s <= e) {
pivot = 0 + (rand() % 5);
while (arr3[s] < pivot)
s++;
while (arr3[e] > pivot)
e--;
if (s <= e) {
temp = arr3[s];
arr3[s] = arr3[e];
arr3[e] = temp;
s++;
e--;
}
// Recursion
if (start < e)
randomizedQuickSort(arr3, start, e);
if (s < end)
randomizedQuickSort(arr3, s, end);
}
}
I expect the number to be between 0 and 4, but it generates -858,993,460 every time. Here's a screenshot of the error and variable values:
https://i.gyazo.com/47a0becfd5d26ae0a15c08af285c4357.png
Aucun commentaire:
Enregistrer un commentaire