Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   quicksort alogrithm (http://www.go4expert.com/forums/quicksort-alogrithm-t19982/)

s11049151 4Nov2009 18:34

quicksort alogrithm
 
Can anyone identify the error in my code please.......:undecided

Code: C++

#include <iostream.h>
#include <stdlib.h>
void quicksort ( int array[], int lo, int hi );
int main()
{


    int array[7, 9, 15, 14, 11, 13, 4, 3, 21, 33, 28];
    cout<<quicksort(array[]);//parse error before ]  what am i doing wrong? :(

    system("PAUSE");
    return 0;
}
void quicksort ( int array[], int lo, int hi )
{
// lo is the lower index,
// hi is the upper index
    int i = lo, j = hi, temp;
//step 1: x will be the pivot
    int x = array[(lo+hi)/2];
//step 2: partition
    do
    {
        while(array[i]<x)
            i++;
        while(array[j]>x)
            j--;
        if (i<=j)
        {
            temp = array[i]; //swap
            array[i] = array[j];
            array[j] = temp;
            i++;
            j-- ;
        }
    }
    while(i<=j);
//step 3: recursion
    if (lo<j) quicksort(array,lo,j);
    if (i<hi) quicksort(array,i,hi);
//cout<<array[i]<<" ";
}


SaswatPadhi 4Nov2009 18:55

Re: quicksort alogrithm
 
Before I talk abt the problem with your code, I would tell you abt the problem with your post.
Please *always* :
(1) Post properly indented code.
(2) Don't COLOR code, rather post them inside [CODE] ... [/CODE]

Now ... abt your code. Well, it has a *lot* of mistakes.

(1) Declaration of arrays
You are declaring arrays like : int array[7, 9, 15, 14, 11, 13, 4, 3, 21, 33, 28];, which is syntactically incorrect.
Declare arrays like : int array[] = {7, 9, 15, 14, 11, 13, 4, 3, 21, 33, 28};.

(2) Insufficient arguments into quicksort
You are calling the quicksort function with 1 argument (quicksort(array[]);) instead of 3 (void quicksort ( int array[], int lo, int hi );).
You should do it like : void quicksort ( array, 0, 10 );

(3) Printing a value with "void" data-type
You are trying to print the return value of quicksort function, which actually has void return type. :crazy:
You can do it this way :
Code: C++

quicksort(array,0,10);
for(int i=0; i < 11; ++i) cout << array[i] << " ";
putchar(10);

BTW, Your quicksort logic is all right, so don't worry abt that. :)

s11049151 4Nov2009 18:59

Re: quicksort alogrithm
 
Thank you.

SaswatPadhi 4Nov2009 19:12

Re: quicksort alogrithm
 
My pleasure. :)


All times are GMT +5.5. The time now is 18:28.