Quick-sort doesn't work with middle pivot element
- by Bobby
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);
}
}