quicksort error

I need some help with my quicksort.
I keep getting error that vector subscript is out of range
Any help would be great
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
void quicksort(vector<int> &v,int low, int high)
{
	int middle = ( low + high ) / 2;
	if( v[ middle ] < v[ low ] )
		swap( v[ low ], v[ middle ] );
	if( v[ high ] < v[ low ] )
		swap( v[ low ], v[ high ] );
	if( v[ high ] < v[ middle ] )
		swap( v[ middle ], v[ high ] );

	int pivot = v[middle];
	swap( v[ middle ] , v[ high-1 ]);

		
		int i,j;
		for(i=low, j=high-1; ;)
		{
			while(v[i] < pivot){i++;}
			while(pivot < v[j]){j--;}
			if(i<j)
				swap( v[ i ] , v[ j ] );
			else 
				break;
		}
	
	
	swap( v[ i ], v[ high - 1 ] );
	
	quicksort(v,low,i-1 );
	quicksort(v,i + 1,high);
		

}
	
	

void quicksort(vector<int> &v)
{
	quicksort(v,0,v.size()-1);
}
Hello there, if you are using visual studio then try setting a breakpoint and check what happens

*cheers*
I'm very new to visual studio, how can I setup a breakpoint?
Ah definitely solved my problem. I didnt tell the function what to do when we reached 2 or less nums in the vector
Good job :), there should be a gray area in the VS editor if you click it you will get a red ball and that is the breakpoint, here is a picture of how it looks like when it's enabled:

http://blogs.msdn.com/blogfiles/endpoint/WindowsLiveWriter/Int.NETOutputCachingwithWCFWebHttpServic_DD70/BreakpointInVisualStudio_4.jpg


EDIT: and don't forget running your program in debug mode if you do so you will trigger it and you will then be able to see variable data and so on.


Last edited on
Topic archived. No new replies allowed.