[爆卦]bubble sort c語言是什麼?優點缺點精華區懶人包

為什麼這篇bubble sort c語言鄉民發文收入到精華區:因為在bubble sort c語言這個討論話題中,有許多相關的文章在討論,這篇最有參考價值!作者airwaves (airwaves)看板C_and_CPP標題[問題] C語言 Bubble ...


各位大神好,
最近在學習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://www.ptt.cc/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
Gway: 你寫的的終止條件有可能不會滿足而造成無窮迴圈歐 10/08 17:04
jepk007: 不覺得第一段輸入的部分重複性很高嗎 10/08 17:42
Gway: 我眼殘 把startSort = startSort -> next;看成在括號內 請 10/08 18:43
Gway: 忽略我講的 囧 但你的終止條件確實定的比較不好 用flag主要 10/08 18:43
Gway: 是看還有沒有bubble(I.eswap的動作)你的寫法是無條件都做 10/08 18:43
alan23273850: 我沒仔細檢查,但印象中起標只是偵測已排序後提早 10/09 12:00
alan23273850: 終止,是加速的手段,跟正確性無關 10/09 12:00
fragmentwing: 你如果擔心有多做 首先改成隨機輸入1~10(反正就是1 10/15 15:23
fragmentwing: ~10隨機交換幾百次後丟進去排序) 10/15 15:23
fragmentwing: 立參數a1 a2紀錄該次排序次數 b1 b2紀錄總排序次數 10/15 15:23
fragmentwing: 每次大概做10次後看你高興要不要繼續就按個鍵啥的 10/15 15:23
fragmentwing: 相信很快就能看出來了 10/15 15:23

你可能也想看看

搜尋相關網站