Actually, you can easily find some source codes on the web. The problem (stack-overflow) appears on Windows (no problem on other platforms) when two vectors (size around 25000) are sorted after a GUI application is launched. My version of quick-sorting two vectors in increasing order of the first vector:
#include <iostream>
#include <stack>
#include <vector>
#include <stdlib.h>
using namespace std;
template< class T1, class T2 >
inline void swap( vector< T1 > & vec1, vector< T2 > & vec2, int a, int b )
{
T1 tmp1 = vec1[ a ];
T2 tmp2 = vec2[ a ];
vec1[ a ] = vec1[ b ]; vec2[ a ] = vec2[ b ];
vec1[ b ] = tmp1; vec2[ b ] = tmp2;
}
template< class T1, class T2 >
void quicksort( vector< T1 > & vec1, vector< T2 > & vec2 )
{
stack< int > help_stack;
help_stack.push( 0 );
help_stack.push( vec1.size() - 1 );
while( !help_stack.empty() )
{
int right = help_stack.top(); help_stack.pop();
int left = help_stack.top(); help_stack.pop();
if ( left >= right )
{
continue;
}
int next_index = left + ( int ) ( ( right - left + 1 ) * ( ( double ) rand() / ( double ) RAND_MAX ) );
swap( vec1, vec2, next_index, right );
T1 p = vec1[ right ];
int a = left;
int b = right - 1;
while( a <= b )
{
while (a <= b && vec1[a ] <= p )
{
a++;
}
while (a <= b && vec1 >= p )
{
b--;
}
if ( a < b )
{
swap( vec1, vec2, a, b );
}
}
swap( vec1, vec2, a, right );
help_stack.push( a + 1 );
help_stack.push( right );
help_stack.push( left );
help_stack.push( a - 1 );
}
}
int main()
{
unsigned i, size = 50;
double value1, value2;
vector< double > vec1;
vector< double > vec2;
for ( i = 0; i < size; ++i )
{
value1 = size * ( ( double ) rand() / ( double ) RAND_MAX );
vec1.push_back( value1 );
value2 = size * ( ( double ) rand() / ( double ) RAND_MAX );
vec2.push_back( value2 );
}
cout << "\n arrays before sorting \n" << endl;
for ( i = 0; i < size; ++i )
{
cout << " i = " << i << " " << vec1[ i ] << " " << vec2[ i ] << endl;
}
quicksort( vec1, vec2 );
cout << "\n arrays after sorting \n" << endl;
for ( i = 0; i < size; ++i )
{
cout << " i = " << i << " " << vec1[ i ] << " " << vec2[ i ] << endl;
}
return 0;
}