Quick-sort doesn't work with middle pivot element

Posted by Bobby on Stack Overflow See other posts from Stack Overflow or by Bobby
Published on 2012-09-23T21:35:40Z Indexed on 2012/09/23 21:37 UTC
Read the original article Hit count: 173

Filed under:
|
|

I am trying to sort an array of elements using quick-sort algorithm.But I am not sure where am I going wrong.I choose the middle element as pivot every time and then I am checking the conditions.Here is my code below.

void quicksort(int *temp,int p,int r)
{
        if(r>p+1)
        {
                int mid=(p+r)/2;
                int piv=temp[mid];
                int left=p+1;
                int right=r;
                while(left < right)
                {
                        if(temp[left]<=piv)
                        left++;
                        else
                        swap(&temp[left],&temp[--right]);
                }
                swap(&temp[--left],&temp[p]);
                quicksort(temp,p,left);
                quicksort(temp,right,r);
        }
}

© Stack Overflow or respective owner

Related posts about c

    Related posts about sorting