I'm a Comp Sci I student in college, and I have an assignment to make a quick sort that can sort vectors up to size 32000 with no problem. So, my program has been having a lot of trouble with big numbers, and every once in a while, I get the error that says I've gone out of the scope of my vector. Here's my code so far:
#include <iostream>
#include <vector>
#include <cstdlib>
#include <time.h>
using namespace std;
// Perform Quick Sort
void VectorPivot_once (vector <int> &list , int bot, int top)
{
int pivot1 = list[bot];
int hi = top;
int low = bot+1;
int pivotindex;
while (hi > low) {
if (list[hi] < pivot1 && list[low] > pivot1) {
int tmp = list[hi];
list[hi] = list[low];
list[low] = tmp;
}
else if (list[hi] >= pivot1)
hi--;
else if (list[low] < pivot1)
low++;
}
if (list[low] < pivot1) { // end case
list[bot] = list[low];
list[low] = pivot1;
pivotindex = low;
}
else
pivotindex = bot;
if(bot < hi)
{
VectorPivot_once(list, bot, pivotindex-1);
}
if(low < top)
{
VectorPivot_once(list, pivotindex+1, top);
}
return;
}
void VectorPivot (vector <int> &list)
{
VectorPivot_once(list, 0, (int)list.size()-1);
return;
}
int main()
{
int m = 200;
vector <int> stuff2 (m);
int y = 0;
while (y != m)
{
stuff2[y] = rand();
y++;
}
clock_t start, end;
start = clock();
VectorPivot(stuff2);
end = clock();
cout << "Vector Sort Vector " << m << " " << (end - start)/CLOCKS_PER_SEC << " seconds" << endl;
return 0;
}
Does anyone know why this has such inconsistent bugging?
Any help would be greatly apprecaited.