C_and_CPP 板


LINE

开发平台(Platform): Win10 编译器: GCC 额外使用到的函数库(Library Used): 无 问题(Question):试着优化Quicksort,选mid为pivot和选end结果不同 ,选end结果正确,mid却无法sort,请各位帮我看看程式哪里有错 P.S. 注解处为选end为pivot 喂入的资料(Input):9 4 1 6 7 3 8 2 5 预期的正确结果(Expected Output):1 2 3 4 5 6 7 8 9 错误结果(Wrong Output): 未显示 程式码(Code): #include <iostream> using namespace std; const int n = 9; void swap(int &a, int &b) { int t = a; a = b; b = t; } int Partition(int *list, int front, int end) { int pivot = (front + end) / 2; int i = front - 1; int j = end + 1; while (i < j) { do i++; while (list[i] <= list[pivot]); do j--; while (list[j] >= list[pivot]); swap(list[i], list[j]); } swap(list[pivot], list[i]); return i; } /*int Partition(int *list, int front, int end) { int i = front - 1; for (int j = front; j < end; j++) { if (list[j] < list[end]) { swap(list[++i], list[j]); } } swap(list[++i], list[end]); return i; }*/ void Quicksort(int *list, int front, int end) { if (front < end) { int pivot = Partition(list, front, end); Quicksort(list, front, pivot - 1); Quicksort(list, pivot + 1, end); } } void Print(int *list) { for (int i = 0; i < n; i++) cout << list[i] << " "; cout << endl; } int main() { int a[] = {9, 4, 1, 6, 7, 3, 8, 2, 5}; cout << "Unsorted: \n"; Print(a); cout << "Sorted: \n"; Quicksort(a, 0, n - 1); Print(a); return 0; } --



※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 140.113.136.218 (台湾)
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/C_and_CPP/M.1618729063.A.8D4.html
1F:推 LPH66: 你知道 Partition 是怎麽运作的吗? 04/18 15:42
2F:→ LPH66: 我是指, 每一行做了什麽事使得最後得到什麽结果这些细节 04/18 15:42
3F:推 ucrxzero: 不是就是 枢点右边都比他大 左边都比他小 04/18 15:47
4F:→ ucrxzero: 然後递回下去 若找到越中间的枢点越好 04/18 15:48
5F:→ superme7: tace过code了用pivot = end实作过,但是不知道pivot = m 04/18 15:55
6F:→ superme7: 为甚麽跑不出来 04/18 15:56
7F:推 ucrxzero: 你是不是在写作业 04/18 15:57
8F:→ superme7: 不是,这是我上网学的 04/18 16:01
9F:推 ucrxzero: 我帮你看一夏 04/18 16:01
10F:→ superme7: 感谢U大 04/18 16:06
11F:推 ucrxzero: 找到了改一下条件 04/18 16:22
12F:→ ucrxzero: while (i<j &&list[i] <= list[pivot]); 04/18 16:22
13F:→ ucrxzero: while (i<j &&list[j] >= list[pivot]); 04/18 16:22
14F:→ ucrxzero: 但还是不对 04/18 16:25
15F:→ ucrxzero: 等等= = 04/18 16:25
16F:推 ucrxzero: www.algolist.net/Algorithms/Sorting/Quicksort 04/18 17:22
17F:推 ko27tye: 你要这样做的话 partition function内的 最外层swap不要 04/18 19:38
18F:→ ko27tye: 做 然後把i和j传出去给quickSort当pivot 04/18 19:38
19F:推 ucrxzero: 对 ko27跟我查到的是一样的方式 04/18 19:42
20F:→ superme7: 感谢各位大大 我知道原因了 04/18 22:06
21F:推 ro9956882: pivot在end可行的话 多一行swap(list[mid],list[end]) 04/24 02:42







like.gif 您可能会有兴趣的文章
icon.png[问题/行为] 猫晚上进房间会不会有憋尿问题
icon.pngRe: [闲聊] 选了错误的女孩成为魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一张
icon.png[心得] EMS高领长版毛衣.墨小楼MC1002
icon.png[分享] 丹龙隔热纸GE55+33+22
icon.png[问题] 清洗洗衣机
icon.png[寻物] 窗台下的空间
icon.png[闲聊] 双极の女神1 木魔爵
icon.png[售车] 新竹 1997 march 1297cc 白色 四门
icon.png[讨论] 能从照片感受到摄影者心情吗
icon.png[狂贺] 贺贺贺贺 贺!岛村卯月!总选举NO.1
icon.png[难过] 羡慕白皮肤的女生
icon.png阅读文章
icon.png[黑特]
icon.png[问题] SBK S1安装於安全帽位置
icon.png[分享] 旧woo100绝版开箱!!
icon.pngRe: [无言] 关於小包卫生纸
icon.png[开箱] E5-2683V3 RX480Strix 快睿C1 简单测试
icon.png[心得] 苍の海贼龙 地狱 执行者16PT
icon.png[售车] 1999年Virage iO 1.8EXi
icon.png[心得] 挑战33 LV10 狮子座pt solo
icon.png[闲聊] 手把手教你不被桶之新手主购教学
icon.png[分享] Civic Type R 量产版官方照无预警流出
icon.png[售车] Golf 4 2.0 银色 自排
icon.png[出售] Graco提篮汽座(有底座)2000元诚可议
icon.png[问题] 请问补牙材质掉了还能再补吗?(台中半年内
icon.png[问题] 44th 单曲 生写竟然都给重复的啊啊!
icon.png[心得] 华南红卡/icash 核卡
icon.png[问题] 拔牙矫正这样正常吗
icon.png[赠送] 老莫高业 初业 102年版
icon.png[情报] 三大行动支付 本季掀战火
icon.png[宝宝] 博客来Amos水蜡笔5/1特价五折
icon.pngRe: [心得] 新鲜人一些面试分享
icon.png[心得] 苍の海贼龙 地狱 麒麟25PT
icon.pngRe: [闲聊] (君の名は。雷慎入) 君名二创漫画翻译
icon.pngRe: [闲聊] OGN中场影片:失踪人口局 (英文字幕)
icon.png[问题] 台湾大哥大4G讯号差
icon.png[出售] [全国]全新千寻侘草LED灯, 水草

请输入看板名称,例如:BabyMother站内搜寻

TOP