C_and_CPP 板


LINE

各位大神好, 最近在学习C语言。 想要用linked-list写bubble sort 这是我参考网路上的做法,最後有成功排序的程式码 #include <stdio.h> #include <stdlib.h> struct node { int data; struct node *next; }; void printLinkedList(struct node *start); void swap(struct node *node1, struct node *node2); void bubbleSort(struct node *start); // ================================================ int main() { int arry[10]; struct node *prev, *first = NULL, *current; printf("Please type in the 01st number:"); scanf("%d", &arry[0]); printf("Please type in the 02nd number:"); scanf("%d", &arry[1]); printf("Please type in the 03rd number:"); scanf("%d", &arry[2]); printf("Please type in the 04th number:"); scanf("%d", &arry[3]); printf("Please type in the 05th number:"); scanf("%d", &arry[4]); printf("Please type in the 06th number:"); scanf("%d", &arry[5]); printf("Please type in the 07th number:"); scanf("%d", &arry[6]); printf("Please type in the 08th number:"); scanf("%d", &arry[7]); printf("Please type in the 09th number:"); scanf("%d", &arry[8]); printf("Please type in the 10th number:"); scanf("%d", &arry[9]); int i; for (i = 0; i < 10; i++) { current = (struct node*)malloc(sizeof(struct node)); current -> data = arry[i]; if (i == 0) { first = current; } else { prev -> next = current; } current -> next = NULL; prev = current; } printf("\n"); printf("List before sorting:"); printLinkedList(first); printf("\n"); bubbleSort(first); printf("List after sorting:"); printLinkedList(first); printf("\n\n"); return 0; } // ================================================ void printLinkedList(struct node *start) { struct node *tmp = start; while(tmp != NULL) { printf("%d,", tmp -> data); tmp = tmp -> next; } } // ------------------------------------------------ void swap(struct node *node1, struct node *node2) { int tmp = node2 -> data; node2 -> data = node1 -> data; node1 -> data = tmp; } // ------------------------------------------------ void bubbleSort(struct node *start) { struct node *startSort; struct node *endSort = NULL; int swapped; if (start == NULL) { return; } do { swapped = 0; startSort = start; while (startSort -> next != endSort) { if (startSort -> data >= startSort -> next -> data) { swap(startSort, startSort -> next); swapped = 1; } startSort = startSort -> next; } endSort = startSort; } while (swapped); } 以下是我的问题, 我原本的做法在bubbleSort子程式没有加入swapped这个参数(如下), 但是网路上找到的做法都有这种旗标。 想请问两种是否只差在我会多执行几次多余的while (startSort -> next != endSort)? 还是说我的做法会有什麽bug呢?? 谢谢大家~ void bubbleSort(struct node *start) { struct node *startSort; struct node *endSort = NULL; int swapped; if (start == NULL) { return; } while(1) { startSort = start; while (startSort -> next != endSort) { if (startSort -> data >= startSort -> next -> data) { swap(startSort, startSort -> next); } startSort = startSort -> next; } endSort = startSort; if (endSort == start -> next) { break; } } } --



※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 114.44.82.152 (台湾)
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/C_and_CPP/M.1570504758.A.3A4.html ※ 编辑: airwaves (114.44.82.152 台湾), 10/08/2019 11:22:21 ※ 编辑: airwaves (114.44.82.152 台湾), 10/08/2019 11:27:33
1F:推 Gway: 你写的的终止条件有可能不会满足而造成无穷回圈欧 10/08 17:04
2F:→ jepk007: 不觉得第一段输入的部分重复性很高吗 10/08 17:42
3F:→ Gway: 我眼残 把startSort = startSort -> next;看成在括号内 请 10/08 18:43
4F:→ Gway: 忽略我讲的 囧 但你的终止条件确实定的比较不好 用flag主要 10/08 18:43
5F:→ Gway: 是看还有没有bubble(I.eswap的动作)你的写法是无条件都做 10/08 18:43
6F:推 alan23273850: 我没仔细检查,但印象中起标只是侦测已排序後提早 10/09 12:00
7F:→ alan23273850: 终止,是加速的手段,跟正确性无关 10/09 12:00
8F:推 fragmentwing: 你如果担心有多做 首先改成随机输入1~10(反正就是1 10/15 15:23
9F:→ fragmentwing: ~10随机交换几百次後丢进去排序) 10/15 15:23
10F:→ fragmentwing: 立参数a1 a2纪录该次排序次数 b1 b2纪录总排序次数 10/15 15:23
11F:→ fragmentwing: 每次大概做10次後看你高兴要不要继续就按个键啥的 10/15 15:23
12F:→ fragmentwing: 相信很快就能看出来了 10/15 15:23







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