quicksort alogrithm

Discussion in 'C++' started by s11049151, Nov 4, 2009.

  1. s11049151

    s11049151 New Member

    Joined:
    Oct 19, 2009
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    Can anyone identify the error in my code please.......:undecided

    Code:
    #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 a moderator: Nov 4, 2009
  2. SaswatPadhi

    SaswatPadhi ~ Б0ЯИ Τ0 С0δЭ ~

    Joined:
    May 5, 2009
    Messages:
    1,342
    Likes Received:
    55
    Trophy Points:
    0
    Occupation:
    STUDENT !
    Location:
    Orissa, INDIA
    Home Page:
    http://www.crackingforfun.blogspot.com
    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 [NOPARSE]
    Code:
     ... 
    [/NOPARSE]

    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:
    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. :)
     
  3. s11049151

    s11049151 New Member

    Joined:
    Oct 19, 2009
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
  4. SaswatPadhi

    SaswatPadhi ~ Б0ЯИ Τ0 С0δЭ ~

    Joined:
    May 5, 2009
    Messages:
    1,342
    Likes Received:
    55
    Trophy Points:
    0
    Occupation:
    STUDENT !
    Location:
    Orissa, INDIA
    Home Page:
    http://www.crackingforfun.blogspot.com
    My pleasure. :)
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice