C 修改的气泡排序不显示过程
当我输入值1,2,3,4,5时,由于C 修改的气泡排序不显示过程,c,sorting,bubble-sort,C,Sorting,Bubble Sort,当我输入值1,2,3,4,5时,由于 我使用的计数条件。 但我希望我的代码显示至少一个通过,如果它也是按排序顺序排列的 如果您发现列表在任何中间点排序,请停止该过程 这是我的密码: #include<stdio.h> int main() { int s,i,j,temp,a[20],count=0,x,n=0; printf("Enter the number of elements :\n"); scanf("%d",&s);
- 我使用的计数条件。 但我希望我的代码显示至少一个通过,如果它也是按排序顺序排列的
- 如果您发现列表在任何中间点排序,请停止该过程
#include<stdio.h>
int main()
{
int s,i,j,temp,a[20],count=0,x,n=0;
printf("Enter the number of elements :\n");
scanf("%d",&s);
for(i=0;i<s;i++)
{
printf("Enter element %d\n",i+1);
scanf("%d",&a[i]);
}
printf("Unsorted list is :\n");
for(i=0;i<s;i++)
{
printf("%d ",a[i]);
}
for(i=0;i<(s-1);i++)
{
count=0;
n++;
for(j=0;j<(s-i)-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
count++;
}
}
if(count<=0)
{
break;
}
else
{
printf("\nAfter Pass %d elements are :",n);
for(x=0;x<s;x++)
{
printf("%d ",a[x]);
}
}
}
printf("\nSorted list is :\n");
for(i=0;i<s;i++)
printf("%d ",a[i]);
return 0;
}
#包括
int main()
{
int s,i,j,temp,a[20],计数=0,x,n=0;
printf(“输入元素的数量:\n”);
scanf(“%d”和“&s”);
对于(i=0;i您的代码很好。要确保打印每个过程,只需在循环开始时打印过程。此外,您不需要声明其他变量来跟踪过程
for (i = 0; i < (s - 1); i++)
{
printf("\nAfter Pass %3d elements are: ", i);
for (j = 0; j < s; j++)
printf("%d ", a[j]);
count = 0;
for (j = 0; j < (s - i) - 1; j++)
{
if (a[j] > a[j + 1])
{
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
count++;
}
}
if (count == 0)
break;
}
for(i=0;i<(s-1);i++)
{
printf(“\n通过后%3d元素为:”,i);
对于(j=0;ja[j+1])
{
温度=a[j];
a[j]=a[j+1];
a[j+1]=温度;
计数++;
}
}
如果(计数=0)
打破
}
so?添加一个额外的标志…丢失else条件(但是将通行打印代码保留在其中),然后在通行打印循环后移动if及其中断。