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);
}
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问