鱼C论坛

 找回密码
 立即注册
查看: 3014|回复: 10

[已解决]关于快速排序的问题

[复制链接]
发表于 2022-11-18 19:22:45 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
这个是代码是用快速排序的方式把数组a里面的整形从小到大排列,但是代码好像实现不了,我是不是又哪里理解错了

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define num 9
  4. void mostly(int low, int high, int a[]);
  5. int integrate(int low, int high, int a[]);
  6. void mostly(int low, int high, int a[])
  7. {
  8.     if (low < high)
  9.     {
  10.         int total = integrate(low, high, a);
  11.         mostly(low, total - 1, a);
  12.         mostly(total + 1, high, a);
  13.     }
  14. }
  15. int integrate(int low, int high, int a[])
  16. {
  17.     int p = a[low];
  18.     while (low < high)
  19.     {
  20.         if (p < a[high] && low < high)
  21.         {
  22.             high--;
  23.         }
  24.         a[low] = a[high];
  25.         if (p > a[low] && low < high)
  26.         {
  27.             low++;
  28.         }
  29.         a[high] = a[low];
  30.     }
  31.     a[low] = p;
  32.     return p;
  33. }
  34. int main(void)
  35. {
  36.     int a[num] = {5, 2, 3, 7, 4, 2, 9, 7, 8};
  37.     int low = 0, high = num - 1;

  38.     mostly(low, high, a);
  39.     printf(",%d", a[0]);
  40.     for (int k = 1; k < num; k++)
  41.     {
  42.         printf(",%d", a[k]);
  43.     }
  44.     system("pause");
  45.     return 0;
  46. }
复制代码
最佳答案
2022-11-18 19:34:08
本帖最后由 xiaosi4081 于 2022-11-18 19:43 编辑

写错了刚刚

感觉应该是上面的问题

上面假设遇到两个值的时候怎么办呢?

还帮你小改了一下

pwq

代码:

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define num 9
  4. void mostly(int low, int high, int a[]);
  5. int integrate(int low, int high, int a[]);
  6. void mostly(int low, int high, int a[])
  7. {
  8.         if (low < high-1)
  9.         {       
  10.                 int total = integrate(low, high, a);
  11.                 mostly(low, total - 1, a);
  12.                 mostly(total + 1, high, a);
  13.         }
  14. }
  15. int integrate(int low, int high, int a[])
  16. {
  17.         int p = a[low];
  18.         while (low < high)
  19.         {
  20.                 while (p <= a[high] && low < high)
  21.                 {
  22.                         high--;
  23.                 }
  24.                 if(low < high)
  25.                 {
  26.                         a[low] = a[high];
  27.                 }
  28.                
  29.                 while (p > a[low] && low < high)
  30.                 {
  31.                         low++;
  32.                 }
  33.                
  34.                 if(low<high){
  35.                         a[high] = a[low];
  36.                 }
  37.         }
  38.         a[low] = p;
  39.         return p;
  40. }
  41. int main(void)
  42. {
  43.         int a[num] = {5, 2, 3, 7, 4, 2, 9, 7, 8};
  44.         int low = 0, high = num - 1;
  45.        
  46.         mostly(low, high, a);
  47.         printf(",%d", a[0]);
  48.         for (int k = 1; k < num; k++)
  49.         {
  50.                 printf(",%d", a[k]);
  51.         }
  52.         system("pause");
  53.         return 0;
  54. }
复制代码


给个最佳答案球球了
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-11-18 19:31:06 | 显示全部楼层

回帖奖励 +4 鱼币

不是,建议还是写个一体化递归哈
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-11-18 19:34:08 | 显示全部楼层    本楼为最佳答案   

回帖奖励 +4 鱼币

本帖最后由 xiaosi4081 于 2022-11-18 19:43 编辑

写错了刚刚

感觉应该是上面的问题

上面假设遇到两个值的时候怎么办呢?

还帮你小改了一下

pwq

代码:

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define num 9
  4. void mostly(int low, int high, int a[]);
  5. int integrate(int low, int high, int a[]);
  6. void mostly(int low, int high, int a[])
  7. {
  8.         if (low < high-1)
  9.         {       
  10.                 int total = integrate(low, high, a);
  11.                 mostly(low, total - 1, a);
  12.                 mostly(total + 1, high, a);
  13.         }
  14. }
  15. int integrate(int low, int high, int a[])
  16. {
  17.         int p = a[low];
  18.         while (low < high)
  19.         {
  20.                 while (p <= a[high] && low < high)
  21.                 {
  22.                         high--;
  23.                 }
  24.                 if(low < high)
  25.                 {
  26.                         a[low] = a[high];
  27.                 }
  28.                
  29.                 while (p > a[low] && low < high)
  30.                 {
  31.                         low++;
  32.                 }
  33.                
  34.                 if(low<high){
  35.                         a[high] = a[low];
  36.                 }
  37.         }
  38.         a[low] = p;
  39.         return p;
  40. }
  41. int main(void)
  42. {
  43.         int a[num] = {5, 2, 3, 7, 4, 2, 9, 7, 8};
  44.         int low = 0, high = num - 1;
  45.        
  46.         mostly(low, high, a);
  47.         printf(",%d", a[0]);
  48.         for (int k = 1; k < num; k++)
  49.         {
  50.                 printf(",%d", a[k]);
  51.         }
  52.         system("pause");
  53.         return 0;
  54. }
复制代码


给个最佳答案球球了

评分

参与人数 1荣誉 +5 鱼币 +5 贡献 +3 收起 理由
1613551 + 5 + 5 + 3

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-11-18 19:38:30 | 显示全部楼层

回帖奖励 +4 鱼币

我觉得就写传统的快速排序吧,不要去写一些奇怪易出错的排序

评分

参与人数 1鱼币 +5 收起 理由
1613551 + 5

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-11-18 19:41:39 | 显示全部楼层

回帖奖励 +4 鱼币

本帖最后由 zhangjinxuan 于 2022-11-18 19:47 编辑

正如二楼说的,用一体化递归

一般我写快排通常就是记代码和思路,之后就一直按这样的代码去写,不然容易出错
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define num 9
  4. void mostly(int low, int high, int a[])
  5. {
  6.     if (low >= high) return;
  7.     int i = low, j = high, t = a[low];
  8.     while (i <= j) {
  9.                 while (i <= j && a[i] < t) ++i;
  10.                 while (i <= j && a[j] > t) --j;
  11.                 if (i <= j) {
  12.                         int t = a[i];
  13.                         a[i] = a[j];
  14.                         a[j] = t;
  15.                         ++i, --j;
  16.                 }
  17.         }
  18.         mostly(low, j, a);
  19.         mostly(i, high, a);
  20. }
  21. int main(void)
  22. {
  23.     int a[num] = {5, 2, 3, 7, 4, 2, 9, 7, 8};
  24.     int low = 0, high = num - 1;

  25.     mostly(low, high, a);
  26.     printf(",%d", a[0]);
  27.     for (int k = 1; k < num; k++)
  28.     {
  29.         printf(",%d", a[k]);
  30.     }
  31.     puts("");
  32.     system("pause");
  33.     return 0;
  34. }
复制代码

评分

参与人数 1荣誉 +5 鱼币 +5 贡献 +3 收起 理由
1613551 + 5 + 5 + 3

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-11-18 19:50:17 | 显示全部楼层

回帖奖励 +4 鱼币

xiaosi4081 发表于 2022-11-18 19:34
写错了刚刚

感觉应该是上面的问题

没必要写两个函数吧

你不是说你要用一体化递归吗

我学快排就是把代码记下来,思路能理解就行,之后一直找这样去写即可

评分

参与人数 1荣誉 +5 鱼币 +5 贡献 +3 收起 理由
1613551 + 5 + 5 + 3

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-11-18 20:46:05 | 显示全部楼层

回帖奖励 +4 鱼币

zhangjinxuan 发表于 2022-11-18 19:50
没必要写两个函数吧

你不是说你要用一体化递归吗

没事,他写这样子他看的懂
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-11-18 20:46:52 | 显示全部楼层
再领个币,顺便提醒个最佳
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-11-18 21:01:17 | 显示全部楼层
xiaosi4081 发表于 2022-11-18 20:46
再领个币,顺便提醒个最佳

我还在看代码,我大概知道我的问题了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-11-18 21:05:30 | 显示全部楼层
zhangjinxuan 发表于 2022-11-18 19:50
没必要写两个函数吧

你不是说你要用一体化递归吗

懂了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-11-18 23:56:59 | 显示全部楼层
被 xiaosi4081改的可以排序了哎
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-23 04:00

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表