C中的气泡排序
我试图用C语言编写冒泡排序算法以供练习和修改,但是,我遇到了一些问题:我试图在20个随机数的数组中每次迭代后打印每个交换,然而,出于某种原因,该程序似乎删除了比之前的项大的项 代码如下:C中的气泡排序,c,bubble-sort,C,Bubble Sort,我试图用C语言编写冒泡排序算法以供练习和修改,但是,我遇到了一些问题:我试图在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"
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;
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;
}
此外,我还运行了一些测试,以检查数组排序或打印过程中是否存在错误,以下是该测试的结果:
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循环中将循环计数器增加了,现在它对数组进行排序并显示每个迭代。以下是更改后的代码的外观:
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会消失?这很好,您犯了一个小错误:
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;
inti,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};
对于(i=0;i这非常有效,您犯了一个小错误:
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;
inti,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};
对于(i=0;i在您输入的代码片段的第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;
inti,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};
对于(i=0;i for)(i=0;i在您输入的代码片段的第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;
inti,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为(i=0;i使用for循环时,您只是跳过了最后一个数字。运行for循环时,i您只是跳过了最后一个数字。运行for循环时,请为我运行for循环。如果你可以发布文本,请不要发布文本图片。$抱歉,将来将弃权。没有任何东西可以阻止你回避问题。你的code捕获最后一个排序数据[19]项目?变量i
和j
从未设置为19,即使i+1
或j+1
在您的第一个屏幕截图上有什么问题?每次扫描您的数字时,排列的数量必须减少。如果您可以发布文本,请不要发布文本图片。$抱歉,将在fu中弃权ture没有什么可以阻止你回答问题。你的代码如何捕获最后的排序数据[19]项目?变量i
和j
从未设置为19,即使i+1
或j+1
在第一个屏幕截图上有什么错误?每次扫描数字时,排列的数量必须减少。哦,我明白了,但是打印每次交换/迭代如何编辑答案以包括打印在每次迭代中。如果我的答案让你满意,请标记我知道了,但是打印每次交换/迭代如何编辑答案以包括每次迭代中的打印。如果我的答案让你满意,请标记它。你是否复制/粘贴了帖子的同一代码而不做任何更改?我强烈建议你修复你正在发布的代码中的问题sting或者最好只留下评论!谢谢评论,我是StackOverflow的新手。我修正了我的答案。欢迎来到StackOverflow!我强烈建议你阅读,以便下次做出更好的贡献;)您是否复制/粘贴了相同的帖子代码而未做任何更改?我强烈建议您修复您发布的代码中的问题,或者最好只留下评论!感谢评论,我是StackOverflow的新手。我修正了我的答案。欢迎来到StackOverflow!我强烈建议您阅读,以便下次做出更好的贡献;)您好,欢迎使用SO!不幸的是,这不是一个答案,而是一条评论,请删除它并将其作为评论发布!当您使用for循环时,您只是跳过了最后一个数字。为iHello运行for循环并欢迎使用SO!不幸的是,这不是答案,而是一条评论,请删除它并将其作为评论发布!您只是跳过了最后一个数字最后一个数字,当您使用for循环时。为i运行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");
// 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;