具体问题
- 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);
}