Reputation: 13
void qkSort (int *arr,int size)
{
if (size>1)
{
int p = partition(arr,size);
qkSort(arr,p);
qkSort(arr+p+1,size-(p-1));
}
return;
}
int partition(int *arr,int size)
{
int i,j,pivot,temp;
pivot = size-1;
for(i=0,j=-1;i<size-1;++i)
{
if (arr[i]<arr[pivot])
{
++j;
if(i!=j)
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
temp = arr[pivot];
arr[pivot] = arr[j+1];
arr[j+1] = temp;
pivot = j+1;
return pivot;
}
The program hangs up. Monitoring the variables by gdb shows that the second recursive call doesnt get its argument passed correctly. Please help! Thank you in advance!
Upvotes: 0
Views: 825
Reputation: 49
Your calculation of the size of 2nd array is wrong.
qkSort(arr+p+1,size-(p-1));
This will work.
qkSort(arr+p+1,(size-p)-1);
Upvotes: 0
Reputation: 2483
Please refer to: http://p2p.wrox.com/visual-c/66347-quick-sort-c-code.html
This example should help you get started.
Upvotes: 2