修改后的冒泡排序不显示通行证

modified bubble sort not showing the passes

当我输入值 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);

    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;
}

帮帮我,你们的建议和想法将非常感谢我。

你的代码没问题。为确保打印每一遍,只需在循环开始时打印一遍即可。此外,您不需要声明额外的变量来跟踪通过。

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;
}