quicksort alogrithm

s11049151's Avatar
Light Poster
Can anyone identify the error in my code please.......

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]<<" ";
}

Last edited by SaswatPadhi; 4Nov2009 at 18:42.. Reason: Code-Block + Indentation
0
SaswatPadhi's Avatar, Join Date: May 2009
~ Б0ЯИ Τ0 С0δЭ ~
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.
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, shabbir likes this
0
s11049151's Avatar
Light Poster
Thank you.
0
SaswatPadhi's Avatar, Join Date: May 2009
~ Б0ЯИ Τ0 С0δЭ ~
My pleasure.