F
flipflop
Sorry if this is a very faq - I haven't seen the answer here though.
The quicksort code below sorts an array from large to small. I simply
want it to reverse the sort order: small to large, but can't see how.
I've walked through it on paper but am just not seeing it.
qsort(double *item,int array_size)
{
qsort_recurse(item,0,array_size-1);
}
qsort_recurse(double *item,int left,int right)
{
int i,j;
double x,tmp;
i=left;
j=right;
x=item[(left+right)/2];
do {
while (item>x && i<right) i++;
while (x>item[j] && j>left) j--;
if (i<=j) {
tmp=item;
item=item[j];
item[j]=tmp;
i++;
j--;
}
} while (i<=j);
if (left<j) qsort_recurse(item,left,j);
if (i<right) qsort_recurse(item,i,right);
}
The quicksort code below sorts an array from large to small. I simply
want it to reverse the sort order: small to large, but can't see how.
I've walked through it on paper but am just not seeing it.
qsort(double *item,int array_size)
{
qsort_recurse(item,0,array_size-1);
}
qsort_recurse(double *item,int left,int right)
{
int i,j;
double x,tmp;
i=left;
j=right;
x=item[(left+right)/2];
do {
while (item>x && i<right) i++;
while (x>item[j] && j>left) j--;
if (i<=j) {
tmp=item;
item=item[j];
item[j]=tmp;
i++;
j--;
}
} while (i<=j);
if (left<j) qsort_recurse(item,left,j);
if (i<right) qsort_recurse(item,i,right);
}