#include <cstdlib>
#include <iostream>
#include <vector>
#include <stack>
using namespace std;
#define RANDOM_LOW 0
#define RANDOM_HIGH 999
#define PRINT_VECTORS
struct sPair
{
int left;
int right;
};
int RandomInt ( int low, int high );
void GenerateNumbers ( const int & x, vector<int>& v );
void PrintTable ( const int & x, vector<int>& v );
void Partition ( vector<int>& v, const int& L, const int& R, sPair& less, sPair& more );
void QuickSort( const int & size, vector<int>& v )
{
stack<sPair> S; sPair x; sPair y; int L = 0, R = (size-1);
while ( true )
{
x.left = x.right = y.left = y.right = 0;
Partition(v, L, R, x, y);
if ( (x.right - x.left) > (y.right - y.left) )
{
S.push(x); L = y.left;
R = y.right;
}
else
{
S.push(y); L = x.left;
R = x.right;
}
while ( R <= L )
{
if (S.empty()) return;
x = S.top();
S.pop();
#ifdef COMPREHENSIVE_OUTPUT
cout << "\nX.left: " << x.left << ", X.right: " << x.right << endl;
#endif
L = x.left;
R = x.right;
}
}
}
void Partition ( vector<int>& v, const int& L, const int& R, sPair& less, sPair& more )
{
int i = L, j = R;
while ( true )
{
while ( v[j] >= v[i] && (j > 0) )
j--;
#ifdef COMPREHENSIVE_OUTPUT
cout << "A: j = " << j << ", i = " << i << endl;
cout << "A: v[j] = " << v[j] << ", v[i] = " << v[i] << endl;
#endif
if ( j > i && (v[j] != v[i]))
{
int temp = v[j];
v[j] = v[i];
v[i] = temp;
#ifdef COMPREHENSIVE_OUTPUT
cout << "SWAP A: j = " << j << ", i = " << i << endl;
cout << "SWAP A: v[j] = " << v[j] << ", v[i] = " << v[i] << endl;
#endif
}
else {
less.left = L;
less.right = i-1;
more.left = i+1;
more.right = R;
return;
}
while ( v[i] <= v[j] && (i < R))
i++;
#ifdef COMPREHENSIVE_OUTPUT
cout << "B: j = " << j << ", i = " << i << endl;
cout << "B: v[j] = " << v[j] << ", v[i] = " << v[i] << endl;
#endif
if ( j > i && (v[j] != v[i]))
{
int temp = v[j];
v[j] = v[i];
v[i] = temp;
#ifdef COMPREHENSIVE_OUTPUT
cout << "SWAP B: j = " << j << ", i = " << i << endl;
cout << "SWAP B: v[j] = " << v[j] << ", v[i] = " << v[i] << endl;
#endif
}
else {
less.left = L;
less.right = j-1;
more.left = j+1;
more.right = R;
return;
}
}
}
int main( void )
{
int x; vector<int> table; time_t start, end;
srand(time(NULL));
while ( true )
{
cout << "\nHow many random numbers (between " << RANDOM_LOW << " and " << RANDOM_HIGH << ") shall we sort?\n";
cin >> x;
if ( x <= 1 )
break;
time(&start);
GenerateNumbers( x, table );
time(&end);
cout << "\n\n" << x << " random integers generated in " << difftime(end,start) << " seconds. Sorting...\n\n";
#ifdef PRINT_VECTORS
cout << "\nUnsorted: ";
PrintTable( x, table );
#endif
time(&start);
QuickSort(x, table);
time(&end);
#ifdef PRINT_VECTORS
cout << "\nSorted: ";
PrintTable( x, table );
#endif
cout << "\n\nTime: " << difftime(end,start) << endl << endl;
}
cin.get();
return EXIT_SUCCESS;
}
int RandomInt( int low, int high )
{
int num = rand() % (high+1);
num = (num < low ? low : (num > high ? high : num ) );
return num;
}
void GenerateNumbers( const int & x, vector<int>& v )
{
if ( v.size() > 0 )
v.clear();
if ( x == 9 )
{
v.insert( v.begin(), 79 );
v.insert( v.begin(), 28 );
v.insert( v.begin(), 57 );
v.insert( v.begin(), 96 );
v.insert( v.begin(), 35 );
v.insert( v.begin(), 84 );
v.insert( v.begin(), 13 );
v.insert( v.begin(), 62 );
v.insert( v.begin(), 41 );
}
else if ( x == 11 ) {
int temp = RandomInt( RANDOM_LOW, RANDOM_HIGH );
v.insert( v.begin(), temp);
v.insert( v.begin(), 79 );
v.insert( v.begin(), 28 );
v.insert( v.begin(), 57 );
v.insert( v.begin(), 96 );
v.insert( v.begin(), 35 );
v.insert( v.begin(), temp);
v.insert( v.begin(), 84 );
v.insert( v.begin(), 13 );
v.insert( v.begin(), 62 );
v.insert( v.begin(), 41 );
}
else
for ( int i = 0; i < x; i++ )
v.insert( v.end(), RandomInt( RANDOM_LOW, RANDOM_HIGH ) );
}
void PrintTable( const int & x, vector<int>& v )
{
cout << endl;
cout << "[ ";
for ( int i = 0; i < x; i++ )
cout << v[i] << " ";
cout << "]\n";
}