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.

add these lines at the top of VectorPivot_once. You will find that bot is 0 and top is -1.

if( bot > m || top > m || bot > top)
    {
        cout << "bot: " << bot << " top: " << top << "\n";
        cin.get();
        return;  
    }

Hey, thanks very much for your reply. So, I'm figuring that the problem lies in this general area:

if(bot < hi)
{
VectorPivot_once(list, bot, pivotindex-1); 
}

Because since bot can equal zero in this case, and most of the time the pivotindex is equal to bot, the top can decrease to negative 1. So, I'm thinking I need to alter the if conditions for both of the iterations.

Could someone just tell me if I'm going down the right path? I'm still relatively new to most of these concepts. Much appreciated.

The these was over my head, but the this was PERFECT.

Thank you so much. My program works wonderfully now.

This question has already been answered. Start a new discussion instead.