[英]Bubble-sort in C
我正在嘗試用C編寫Bubble排序排序算法以進行練習和修訂,但是,我遇到了一些問題:我試圖在20個隨機數數組的每次迭代后打印每次交換,但是該程序似乎出於某種原因擺脫比以前更大的物品。
這是代碼:
int i, j, temp;
int SortData[20]= {20, 43, 90, 17, 2, 4, 67, 54, 0, 44, 78, 89, 21, 45, 72,
88, 65, 100, 97, 25};
for(i=0; i<20; i++)
{
printf("|%d", SortData[i]);
}
printf("|");
printf("\n");
for (i=0; i<19; i++)
{
for(j=0;j<18;j++){
if(SortData[j]>SortData[j+1])
{
temp = SortData[j];
SortData[j]=SortData[j+1];
SortData[j+1]=temp;
printf("|%d", SortData[j]);
}
}
printf("\n");
}
printf("\n");
system("pause");
return 0;
運行此代碼后,將發生以下情況:
|20|43|90|17|2|4|67|54|0|44|78|89|21|45|72|88|65|100|97|25|
|17|2|4|67|54|0|44|78|89|21|45|72|88|65|97
|17|2|4|54|0|44|21|45|72|88|65
|17|2|4|0|44|21|45|72|65
|2|4|0|21|45|65
|0|21|45|65
|0|21|65
|0|21
|0
Process returned 10 (0xA) execution time : 3.072 s
Press any key to continue.
此外,我運行了一些測試來檢查數組排序或打印過程中是否存在錯誤,這是該測試的結果:
int i, j, temp;
int SortData[20]= {20, 43, 90, 17, 2, 4, 67, 54, 0, 44, 78, 89, 21, 45, 72,
88, 65, 100, 97, 25};
for(i=0; i<20; i++)
{
printf("|%d", SortData[i]);
}
printf("|");
printf("\n");
for (i=0; i<19; i++)
{
for(j=0;j<18;j++){
if(SortData[j]>SortData[j+1])
{
temp = SortData[j];
SortData[j]=SortData[j+1];
SortData[j+1]=temp;
}
}
}
for (i=0; i<20; i++){
printf("|%d", SortData[i]);//Error here as 25 isn't sorted
}
printf("|");
printf("\n");
system("pause");
return 0;
與上面的代碼相比,此代碼段的唯一變化是,打印語句從嵌套的for循環中出來,並使用單獨的for循環進行打印,這種工作方式是對數字進行排序,但由於某些原因25不是:
|20|43|90|17|2|4|67|54|0|44|78|89|21|45|72|88|65|100|97|25|
|0|2|4|17|20|21|43|44|45|54|65|67|72|78|88|89|90|97|100|25|
Press any key to continue . . .
因此,事實證明排序和打印存在問題。 我能否對如何打印交換的每個迭代並使其正確交換有一些提示?
更新:
因此,我在嵌套的for循環中將循環計數器增加了,現在它對數組進行排序並顯示每次迭代。 更改后的代碼如下所示:
int i, j, temp;
int SortData[20]= {20, 43, 90, 17, 2, 4, 67, 54, 0, 44, 78, 89, 21, 45, 72, 88, 65, 100, 97, 25};
for(i=0; i<20; i++)
{
printf("|%d", SortData[i]);
}
printf("|");
printf("\n");
for (i=0; i<20; i++)
{
for(j=0;j<19;j++){
if(SortData[j]>SortData[j+1])
{
temp = SortData[j];
SortData[j]=SortData[j+1];
SortData[j+1]=temp;
}
printf("|%d", SortData[j]);//Changed code
}
printf("\n");
}
//for (i=0; i<20; i++){
// printf("|%d", SortData[i]);//Error here as 25 isn't sorted
//}
printf("|");
printf("\n");
system("pause");
return 0;
現在它確實顯示了每個迭代並對其進行排序,但是由於某種原因,數字100從數組中消失了,因此它僅對19個項目進行排序,而不是20:
|20|43|90|17|2|4|67|54|0|44|78|89|21|45|72|88|65|100|97|25|
|20|43|17|2|4|67|54|0|44|78|89|21|45|72|88|65|90|97|25
|20|17|2|4|43|54|0|44|67|78|21|45|72|88|65|89|90|25|97
|17|2|4|20|43|0|44|54|67|21|45|72|78|65|88|89|25|90|97
|2|4|17|20|0|43|44|54|21|45|67|72|65|78|88|25|89|90|97
|2|4|17|0|20|43|44|21|45|54|67|65|72|78|25|88|89|90|97
|2|4|0|17|20|43|21|44|45|54|65|67|72|25|78|88|89|90|97
|2|0|4|17|20|21|43|44|45|54|65|67|25|72|78|88|89|90|97
|0|2|4|17|20|21|43|44|45|54|65|25|67|72|78|88|89|90|97
|0|2|4|17|20|21|43|44|45|54|25|65|67|72|78|88|89|90|97
|0|2|4|17|20|21|43|44|45|25|54|65|67|72|78|88|89|90|97
|0|2|4|17|20|21|43|44|25|45|54|65|67|72|78|88|89|90|97
|0|2|4|17|20|21|43|25|44|45|54|65|67|72|78|88|89|90|97
|0|2|4|17|20|21|25|43|44|45|54|65|67|72|78|88|89|90|97
|0|2|4|17|20|21|25|43|44|45|54|65|67|72|78|88|89|90|97
|0|2|4|17|20|21|25|43|44|45|54|65|67|72|78|88|89|90|97
|0|2|4|17|20|21|25|43|44|45|54|65|67|72|78|88|89|90|97
|0|2|4|17|20|21|25|43|44|45|54|65|67|72|78|88|89|90|97
|0|2|4|17|20|21|25|43|44|45|54|65|67|72|78|88|89|90|97
|0|2|4|17|20|21|25|43|44|45|54|65|67|72|78|88|89|90|97
|0|2|4|17|20|21|25|43|44|45|54|65|67|72|78|88|89|90|97
|
Press any key to continue . . .
為什么100消失了?
完美運行,您犯了一個小錯誤:
int i, j, temp;
int SortData[20]= {20, 43, 90, 17, 2, 4, 67, 54, 0, 44, 78, 89, 21, 45, 72, 88, 65, 100, 97, 25};
for(i=0; i<20; i++)
{
printf("|%d", SortData[i]);
}
printf("|");
printf("\n");
//before for(i=0; i<19; i++)
for (i=0; i<20; i++)// i < 20 or it will skip the last number
{
//before for(j=0; j<18; j++)
for(j=0;j<19;j++){
if(SortData[j]>SortData[j+1])
{
temp = SortData[j];
SortData[j]=SortData[j+1];
SortData[j+1]=temp;
}
}
}
for (i=0; i<20; i++){
printf("|%d", SortData[i]);
}
printf("|");
printf("\n");
return 0;
如果要打印氣泡排序的每個迭代,請使用以下代碼:
int i, j, temp, h;
int SortData[20]= {20, 43, 90, 17, 2, 4, 67, 54, 0, 44, 78, 89, 21, 45, 72, 88, 65, 100, 97, 25};
for(i=0; i<20; i++)
{
printf("|%d", SortData[i]);
}
printf("|");
printf("\n");
for (i=0; i<20; i++)
{
for(j=0;j<19;j++){
if(SortData[j]>SortData[j+1])
{
temp = SortData[j];
SortData[j]=SortData[j+1];
SortData[j+1]=temp;
for (h=0; h<20; h++)
{
printf("|%d", SortData[h]);
}
printf("|");
printf("\n");
}
}
}
return 0;
}
在您輸入的代碼段的第11行中,您僅使用前19條記錄進行排序。 將此代碼設置為20並求解。
我評論我修復的那一行。
提示:可能您是剛開始軟件開發,對於這種情況,學習如何使用調試會很有趣。
int i, j, temp;
int SortData[20]= {20, 43, 90, 17, 2, 4, 67, 54, 0, 44, 78, 89, 21, 45, 72,
88, 65, 100, 97, 25};
for(i=0; i<20; i++)
{
printf("|%d", SortData[i]);
}
printf("|");
printf("\n");
// before > for (i=0; i<19; i++)
for (i=0; i<20; i++)
{
// before > for(j=0;j<18;j++){
for(j=0;j<19;j++){
if(SortData[j]>SortData[j+1])
{
temp = SortData[j];
SortData[j]=SortData[j+1];
SortData[j+1]=temp;
}
}
}
for (i=0; i<20; i++){
printf("|%d", SortData[i]);//Error here as 25 isn't sorted
}
printf("|");
printf("\n");
system("pause");
return 0;
使用for循環時,您只是跳過最后一個數字。 為i <20而不是i <19運行for循環,因為它在排序時跳過了最后一個數字。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.