HiveBrain v1.2.0
Get Started
← Back to all entries
snippetcppMinor

My own quicksort algorithm

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
algorithmownquicksort

Problem

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
#include
#include
#include
#include
#include
#include

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::iterator l,
vector::iterator r ) {
copy( l,r+1,ostream_iterator( cout," " ) );
cout &data ) {
copy( data.begin( ), data.end( ),
ostream_iterator( cout," " ) );
cout ::iterator left,
vector::iterator right ) {
cout &data,
vector::iterator left,
vector::iterator right ) {
int pivot_index = ((right-left)/2);
int base = std::distance( data.begin( ),left );
return ( pivot_index+=base );

}

void my_qsort( vector &data,
vector::iterator left,
vector::iterator right,
int side
) {
((side==0) ? cout0) {
vector::iterator lit=left;
vector::iterator rit=right;
int pivot_index = get_pivot_index( data,left,right );
int pivot=data[pivot_index];
cout left && lit rit ) { break; }
else { lit++; }
}
while( rit > left && (*rit) >= pivot ) {
if( lit > rit ) { break; }
else { rit--; }

Solution

-
The last three libraries should be removed as they're not being used. There's no need to keep a library around if you're long longer using it or have never used even it.

You may also consider organizing them in some way, such as alphabetically.

-
You don't really need these:

using std::vector;
using std::cin;
using std::cout;
using std::endl;
using std::vector;
using std::ostream_iterator;
using std::istream_iterator;


Just use std:: where needed, otherwise it'll just get longer if you add more. Also, you have listed using std::vector twice, but it should just be there once.

-
print() isn't modifying any data members (it just prints something):

print( std::vector &data )


so data should be passed by const& to avoid an unnecessary copy:

print(std::vector const& data)


-
There's no need to provide your own swap() function; just use std::swap:

std::swap(lit, rit);


-
This is not a very common nor maintainable style in my_qsort():

if( lit > rit ) { break; }
else { lit++; }


Put each bracketed statement on its own line, in case you ever need to add to them:

if (lit > rit) {
    break;
}
else {
    lit++;
}


-
Why is there a "pause" in my_qsort()?

char c;
cin >> c;


Just let the function works without such interruptions. But if you must keep it anyway, then at least make the user aware that it's a pause, and tell them to input a character in order to continue.

-
Using parentheses for a simple return is not really needed:

return ( 0 );


Just keep it simple and omit them:

return 0;


Moreover, you don't really need this. Reaching the end of main() already implies success, so the compiler will insert the return itself.

Code Snippets

using std::vector;
using std::cin;
using std::cout;
using std::endl;
using std::vector;
using std::ostream_iterator;
using std::istream_iterator;
print( std::vector<float> &data )
print(std::vector<float> const& data)
std::swap(lit, rit);
if( lit > rit ) { break; }
else { lit++; }

Context

StackExchange Code Review Q#3799, answer score: 8

Revisions (0)

No revisions yet.