I will make it more reader friendly after I get the algorithm completed. Does anything with the quick sort algorithm stand out as for why it is not working 100%? This is not homework; I am just practicing problems out of books and I am caught on this one.
Hints, more so than answers, would be more appreciated. Please don't post your own quick sort algorithm, as copying someone else's code does not help me learn. I am just looking for that last bit to help me make the leap across; I don't want you to build another bridge somewhere else, and say "use mine"; it won't help. If my algorithm is a lost cause just say so, and I will go back to the drawing board.
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
#include <cmath>
#include <ctime>
#include <assert.h>
using std::vector;
using std::cin;
using std::cout;
using std::endl;
using std::vector;
using std::ostream_iterator;
using std::istream_iterator;
void print_part( vector<float>::iterator l,
vector<float>::iterator r ) {
copy( l,r+1,ostream_iterator<float>( cout," " ) );
cout << endl;
}
void print( std::vector<float> &data ) {
copy( data.begin( ), data.end( ),
ostream_iterator<float>( cout," " ) );
cout << endl;
}
void swap( vector<float>::iterator left,
vector<float>::iterator right ) {
cout << "Swaping " << (*left) << " with " << (*right) << endl;
float tmp = (*right);
(*right) = (*left);
(*left) = tmp;
}
int get_pivot_index( vector<float> &data,
vector<float>::iterator left,
vector<float>::iterator right ) {
int pivot_index = ((right-left)/2);
int base = std::distance( data.begin( ),left );
return ( pivot_index+=base );
}
void my_qsort( vector<float> &data,
vector<float>::iterator left,
vector<float>::iterator right,
int side
) {
((side==0) ? cout<<"":(side==1) ? cout<<"Left: " : cout<<"Right: " );
print_part( left,right );
if( (right-left)>0) {
vector<float>::iterator lit=left;
vector<float>::iterator rit=right;
int pivot_index = get_pivot_index( data,left,right );
int pivot=data[pivot_index];
cout << "pivot = " << pivot << endl;
while( lit <= right && rit > left && lit <= rit ) {
while( lit <= right && (*lit) < pivot ) {
if( lit > rit ) { break; }
else { lit++; }
}
while( rit > left && (*rit) >= pivot ) {
if( lit > rit ) { break; }
else { rit--; }
}
if( lit < rit ) { swap( lit,rit ); }
}
my_qsort( data,left,rit,1 );
my_qsort( data,lit+1,right,2 );
}
char c;
cin >> c;
}
int main( int argc, char *argv[ ] ) {
vector<float>
data(
( istream_iterator<float>( cin ) ),
( istream_iterator<float>( ) )
);
cout << "-----------------------------------------------" << endl;
my_qsort( data,data.begin( ),data.end( )-1,0 );
cout << "-----------------------------------------------" << endl;
print( data );
return ( 0 );
}