I have implemented a quick sort algorithm but when I tested it I noticed that it fails when the input array has the largest element in the first element (this is the element I got the pivot from). Here’s my code:
void partition(int *a,int size){
if(size<=1){return;}
int pivot=a[0];
int left=0,right=0;
for(left=1,right=size-1;left<=right;){ //was size-1
if(a[left]>=pivot&&a[right]<=pivot) {
swap(left,right,a);
}
if(a[left]<pivot){left++;}
if(a[right]>pivot){right--;}
}
swap(0,right,a);
partition(a,right-1);
partition(&(a[right+1]),size-right-1);
}
Some samples that it fails on:
I/P 245 111 32 4
O/P 4 111 32 245 `
I/P 154 11 43 3 7
O/P 7 11 43 3 154
What are the possible mistakes I have made?
Well, the problem lies here :
Change it to
And it will work. I think you know the reason.
For the partition function to work correctly, you must supply 2 things :
1) The array to work on.
2) The number of elements it has, the size.
The problem lies in the first recursive call to partition the left subarray:
partition(a,right-1)The argument 2, the
size, is incorrectly specified to beright-1, when it is actuallyright.This can be worked out by using the fact that the number of elements in an array from an index
atob( both included,b>=a) areN= b-a+1.Here, we have
a=0,b=right-1, thus the number of elements in the left sub array, thesize,N=(right-1)-(0)+1=right.Thus the to work correctly, you must call it like
partition(a,right);.The left sub array ends at
right-1, but it hasright-1+1=rightelements.Happens all the time 🙂