张旻昕 2018-12-25 12:39 采纳率: 0%
浏览 437

C语言分治法练习快速排序中递归的逻辑运算问题

具体问题

  • Ouicksort函数中的条件语句if中的逻辑表达式我本来用的是‘==’,生成时没报错,但运行时出现问题:图片说明
  • 将其改为‘>=’时却没有问题了:图片说明
  • 但是我没有想到会有形参low>high的情况 ## 代码
#include<stdio.h>

int Split(int low, int high);
void Quicksort(int a[], int low, int high);

int main(void)
{
    int a[100], low, high, n;     // Use a[0] as an empty element to store the value of "middle" temperarily.
    printf("Enter the lengh of the arry you want to enter:");
    scanf("%d", &n);
    printf("Enter the arry:");
    for (int i = 1; i <= n; i++)
        scanf("%d", &a[i]);
    Quicksort(a, 1, n);
    printf("The sorted arry:");
    for (int i = 1; i <= n; i++)
        printf("%d ", a[i]);
    return 0;
}

int Split(int a[], int low, int high)
{
    a[0] = a[low];
    for ( ; ; ) {
        for ( ; a[high] >= a[0] && high > low; high--);
        if (high == low)
                break;
        a[low++] = a[high];
        for ( ; a[low] <= a[0] && high > low; low++);
        if (high == low)
                break;
        a[high--] = a[low];         
        }
    a[high] = a[0];
    return high;
}

void Quicksort(int a[], int low, int high)
{
    int middle;
    if (low >= high) ////////////////////////////////////"The repair of problem 1: replace '==' by '>='
        return;
    middle = Split(a, low, high);
    Quicksort(a, low, middle - 1);
    Quicksort(a, middle + 1, high);
}
  • 写回答

1条回答

  • CSDN-Ada助手 CSDN-AI 官方账号 2022-09-09 15:26
    关注
    不知道你这个问题是否已经解决, 如果还没有解决的话:

    如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 以帮助更多的人 ^-^
    评论

报告相同问题?

悬赏问题

  • ¥15 Excel发现不可读取的内容
  • ¥15 UE5#if WITH_EDITOR导致打包的功能不可用
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?
  • ¥15 电磁场的matlab仿真
  • ¥15 mars2d在vue3中的引入问题
  • ¥50 h5唤醒支付宝并跳转至向小荷包转账界面
  • ¥15 算法题:数的划分,用记忆化DFS做WA求调
  • ¥15 chatglm-6b应用到django项目中,模型加载失败
  • ¥15 CreateBitmapFromWicBitmap内存释放问题。