|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
这个是代码是用快速排序的方式把数组a里面的整形从小到大排列,但是代码好像实现不了,我是不是又哪里理解错了
- #include <stdio.h>
- #include <stdlib.h>
- #define num 9
- void mostly(int low, int high, int a[]);
- int integrate(int low, int high, int a[]);
- void mostly(int low, int high, int a[])
- {
- if (low < high)
- {
- int total = integrate(low, high, a);
- mostly(low, total - 1, a);
- mostly(total + 1, high, a);
- }
- }
- int integrate(int low, int high, int a[])
- {
- int p = a[low];
- while (low < high)
- {
- if (p < a[high] && low < high)
- {
- high--;
- }
- a[low] = a[high];
- if (p > a[low] && low < high)
- {
- low++;
- }
- a[high] = a[low];
- }
- a[low] = p;
- return p;
- }
- int main(void)
- {
- int a[num] = {5, 2, 3, 7, 4, 2, 9, 7, 8};
- int low = 0, high = num - 1;
- mostly(low, high, a);
- printf(",%d", a[0]);
- for (int k = 1; k < num; k++)
- {
- printf(",%d", a[k]);
- }
- system("pause");
- return 0;
- }
复制代码
本帖最后由 xiaosi4081 于 2022-11-18 19:43 编辑
写错了刚刚
感觉应该是上面的问题
上面假设遇到两个值的时候怎么办呢?
还帮你小改了一下
pwq
代码:
- #include <stdio.h>
- #include <stdlib.h>
- #define num 9
- void mostly(int low, int high, int a[]);
- int integrate(int low, int high, int a[]);
- void mostly(int low, int high, int a[])
- {
- if (low < high-1)
- {
- int total = integrate(low, high, a);
- mostly(low, total - 1, a);
- mostly(total + 1, high, a);
- }
- }
- int integrate(int low, int high, int a[])
- {
- int p = a[low];
- while (low < high)
- {
- while (p <= a[high] && low < high)
- {
- high--;
- }
- if(low < high)
- {
- a[low] = a[high];
- }
-
- while (p > a[low] && low < high)
- {
- low++;
- }
-
- if(low<high){
- a[high] = a[low];
- }
- }
- a[low] = p;
- return p;
- }
- int main(void)
- {
- int a[num] = {5, 2, 3, 7, 4, 2, 9, 7, 8};
- int low = 0, high = num - 1;
-
- mostly(low, high, a);
- printf(",%d", a[0]);
- for (int k = 1; k < num; k++)
- {
- printf(",%d", a[k]);
- }
- system("pause");
- return 0;
- }
复制代码
给个最佳答案球球了
|
|