简体   繁体   中英

Quick sort programmed in C

I am reading ANSI C by K&R. I came across the qsort program. I want a little help. Suppose I have 9 elements with index 0->8. Please read the comments to see if I am understanding it correct or not. Thanks a lot for you efforts

void qsort(int v[] , int left, int right)
{
int i, j, last;
void swap(int v[], int i, int j);   

if(left >= right)               /*if the array has only one element return it*/
      return;
swap(v,left, (left+right)/2); /* now, left=(left+right)/2= 0+8/2= 4 we have 4 as left*/
last= left;   /* putting left = last of the first partition group i.e. last=4*/

for(i=left+1; i<=right,i++)  /* now looping from 4+1=5 to 8 with increment of 1*/
     if(v[i] < v[left])       /*if value at 5th is less than value at 4th */
          swap(v, ++last, i);  

I have problem in this last swap step. As my values suggest swap ++4 ie to i ie 4+1= 5 (swapping 5 position with 5?). How can I understand this? There must be a swapping between 4 and 5, not 5 and 5 is it?

code continues

swap(v,left, last);
qsort(v,left,last-1);
qsort(v,last+1,right);
}

Firstly, you have a small misconception about the swap function. Let's say the prototype of the function is -

swap(int array[], int i, int j)

The swap function swaps the numbers at location array[i] and array[j]. So, the swap function swaps the elements in the array. So, the line -

swap(v, left, (left + right) / 2);

Means that, the middle element in the array is swapped with the leftmost element. Clearly, the quicksort is taking the middle element as the pivot. This swap has no effect on the local variables or parameters. According to your data input example, the value of 'left' = 0, and the value of right = '8', even after the swapping. This is where you got confused. The elements of array are swapped, not the values of variables. So, now, the line -

last = left;

makes, 'last' point to the location of the pivot ('left'), so here the value of 'last' = 0 not 4. So, the loop,

for(i = left + 1; i <= right; i++) 

Runs from i = 1 to 8. BTW, you forgot the semicolon..! Then, the line,

if(v[i] < v[left])

checks if the current element ('v[i]') is less than the pivot ('v[left]') or not. Then, accordingly swaps the lesser elements as in the line,

 swap(v, ++last, i);

from the location (last + 1) till where ever it increments to. So, the elements to the left of 'last' are less than pivot and the elements to the right are greater. I think you are missing another line, where we bring the pivot back to the middle which was at the location 'v[left]' during the execution of the algorithm. Then, the recursive calls play their roles. If you are looking for help with quicksort, this is a good place to start !

I hope my answer has helped you, if it did, let me know..!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM