张旻昕
2018-12-25 12:39C语言分治法练习快速排序中递归的逻辑运算问题
具体问题
#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);
}
- 点赞
- 回答
- 收藏
- 复制链接分享
0条回答
为你推荐
- 找出一个整形数组中的元素的最大值
- c++
- 2个回答
- 这个算法问题,请大身看下怎么解决,如何输出公因数
- 算法
- each
- 2个回答
- 分治法求最大非连续子序列
- 分治算法
- 算法
- 1个回答
- C++语言编程 急急!!! 用分治策略
- 算法
- 语言
- c++
- 2个回答
- C++语言编程 急急!!!用分治策略
- 递归
- 算法
- c++
- 2个回答
换一换