![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Rules & FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here. |
|
|
LinkBack | Thread Tools | Display Modes |
|
|||
|
qsort
this is the code in K & R ritchie
void qsort(int v[], int left, int right){ int i, last; void swap(int v[], int i, int j); if(left >= right) return; swap(v, left, (left + right)/2); last = left; for(i = left + 1; i <= right; i++) if(v[i] < v[left]) swap(v, ++last,i); swap(v, left, last); qsort(v , left, last - 1); qsort(v, last - 1, right); } the highlighted portion does not make sense to me at the first go, b'cos it is trying to swap the same element. i find (i & last ) have the same value when swap is called in the for loop for the first time, plz explain.... thanks in advance |
| Forum Sponsor | ||
|
|
|
|||
|
PLEASE put code in code tags! They are wonderful, magical things that make it possible to read your code without one's eyes spontaneously bleeding.
Code:
void qsort(int v[], int left, int right){
int i, last;
void swap(int v[], int i, int j);
if(left >= right)
return;
swap(v, left, (left + right)/2);
last = left;
for(i = left + 1; i <= right; i++)
if(v[i] < v[left])
swap(v, ++last,i); swap(v, left, last);
qsort(v , left, last - 1);
qsort(v, last - 1, right);
}
Quote:
Have you tested it -- does it work? If so, my guess would be that a little redundancy was allowed for the sake of making it smaller. |