You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C语言实现QuickSort快速排序升序正常降序结果错误问题求解

问题修复方案

核心错误点

你编写的quickDescending函数中,分区完成后的递归步骤错误调用了升序排序函数quickAscending,仅第一轮分区将最大值放到了数组首位,后续所有子分区都按照升序逻辑处理,最终得到错误的降序结果。

修复方法

将quickDescending函数末尾的两行递归调用代码,从调用quickAscending改为调用quickDescending即可,修改后的函数代码如下:

void quickDescending(int array[size], int start, int end){
    int temp;
    if(start < end){
        int pointer = start;
        int pivot = array[end];
        int count;
        for(count = start; count < end; count++)
        {
            if(array[count] > pivot) 
            {
                temp = array[count];
                array[count] = array[pointer];
                array[pointer] = temp;
                pointer++;
            }
        }
        temp = array[end];
        array[end] = array[pointer];
        array[pointer] = temp;
        // 原代码错误调用了quickAscending,改为调用自身
        quickDescending(array, start, pointer-1);
        quickDescending(array, pointer+1, end);
    }
}

修正后运行结果

  • 升序排序输出:0 3 5 10 15 20 27
  • 降序排序输出:27 20 15 10 5 3 0

内容的提问来源于stack exchange,提问作者Kaizuke

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.02 12:06:05