Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   quick sort implementation causing mysterious error (http://www.go4expert.com/forums/quick-sort-implementation-causing-t17915/)

k0der 9Jun2009 11:08

quick sort implementation causing mysterious error
 
the code for the quicksort that i implemented is as below :
Code:

 
//******************** this routine implements quick sort taking first element as the pivot *******************
//  last modified on 12-05-2009 1:08 PM

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<errno.h>

main()
{
    int elements[]={24,5,50,10,1,55,95,80},j;
    void quick(int *array,int left,int right);
    int left,right,pivot;
    left=0;
    right=7;

    for(j=left;j<=right;j++)        //print the array befor sorting:W

        printf("%d\t",elements[j]);

    quick(elements,left,right);
    printf("\nthe sorted array is :\n");
    for(left=0;left<=right;left++)
        printf("%d\t",elements[left]);
    printf("\n");

}
void quick(int el[],int left,int right)
{
      int pivot=0,tmp,i=left-1,j=right;  // j increased and i dec for looping convenience
    int num=right-left+1;            //num of elements

  // swapping the last element and pivot ;is first element
    tmp=el[pivot];
    el[pivot]=el[right];
    el[right]=tmp;

  //now comparing the elements from the pivot
    pivot=right;                        //pivot has become the index for the rightmost element
    printf("\n pivot : %d\n",el[pivot]);

    for( ; ; )
        {
        while(el[++i] > el[pivot])
        {
            printf("\n el[i]=%d \n",el[i]);   
                break;
        }
   
        while(el[--j] < el[pivot])
        {
            printf("\n el[j]=%d \n",el[j]);
            break;
        }
           
        if(fflush(NULL) != 0  )
            printf("\n %s \n",strerror(errno));
        if(i>j)
            break;
        else
        {
            tmp=el[i];
            el[i]=el[j];
            el[j]=tmp;
           
            printf("\n----after swap-----\n");      // looking at list here for debugging purpose 

            for(tmp=left;tmp<=right;tmp++)
                printf("%d\t",el[tmp]);
        }
    }   
   
            tmp=el[pivot];        //for i>j case   
            el[pivot]=el[i];
            el[i]=tmp;
   
           
    printf("\n ---Aafter sStep-----\n");     
    for(j=left;j<=right;j++)
        printf("%d\t",el[j]);

        sleep(5);    // for debugging purpose

    if(left<i)
        quick(el,left,i-1);

    if(right>i)
            quick(el,i+1,right);
}


the output is not sorted.and error is still there but where i am not able to find.
thanks for help.

SaswatPadhi 9Jun2009 11:24

Re: quick sort implementation causing mysterious error
 
Please edit your post and post the code inside code-blocks. (so as to preserve the indentation).

xpi0t0s 9Jun2009 13:37

Re: quick sort implementation causing mysterious error
 
There's a good interactive quicksort at http://pages.stern.nyu.edu/~panos/ja...ort/index.html
With this you'll be able to get your head round how it works, then you should be able to find what's wrong with yours.

k0der 14Jun2009 14:00

Re: quick sort implementation causing mysterious error
 
i am not able to view the site due to some plugins problem as i dont have a net connection and i am using this internet through a cyber caffe..could u please post the code here..
thanks a lot for the help. :)


All times are GMT +5.5. The time now is 12:10.