C语言 QUICKSORT排序 升序变降序
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
C语言 QUICKSORT排序升序变降序
悬赏分:0 |解决时间:2009-11-21 15:26 |提问者:jason51122
#include
#define NUMEL 7
int main()
{
int nums[NUMEL] = {67,32,45,73,98,101,99};
int i;
void quicksort(int [], int, int);
quicksort(nums, 0, NUMEL-1);
printf("The sorted list, in ascending order, is:\n");
for (i = 0; i < NUMEL; i++)
printf("%d ",nums[i]);
printf("\n");
return 0;
}
void quicksort(int num[], int lower, int upper)
{
int pivot;
int partition(int [], int, int);
pivot = partition(num, lower, upper);
if (lower < pivot)
quicksort(num, lower, pivot - 1);
if (upper > pivot)
quicksort(num, pivot + 1, upper);
return;
}
int partition(int num[], int left, int right)
{
int pivot, temp;
pivot = num[left]; /* "capture" the pivot value, which frees up one slot */ while (left < right)
{
/* scan from right to left */
while(num[right] >= pivot && left < right) /* skip over larger or equal values */
right--;
if (right != left)
{
num[left] = num[right]; /* move the higher value into the available slot */
left++;
}
/* scan from left to right */
while (num[left] <= pivot && left < right) /* skip over smaller or equal values */
left++;
if (right != left)
{
num[right] = num[left]; /* move lower value into the available slot */
right-- ;
}
}
num[left] = pivot; /* move pivot into correct position */
return(left); /* return the pivot index */
}
哪位高手能把这个升序变为降序急在线等
问题补充:
请问哪部分的符号改啊
if (lower < pivot)
quicksort(num, lower, pivot - 1);
if (upper > pivot)
quicksort(num, pivot + 1, upper);
这部分怎么办
最佳答案
修改partition()函数中的两处即可:
int partition(int num[], int left, int right)
{
int pivot, temp;
pivot = num[left];
while (left < right)
{
while(num[right] <= pivot && left < right) /*注意这里*/ right--;
if (right != left)
{
num[left] = num[right];
left++;
}
while (num[left] >= pivot && left < right) /*注意这里*/
left++;
if (right != left)
{
num[right] = num[left];
right-- ;
}
}
num[left] = pivot;
return(left);
}