冒泡排序中数据交换的次数

#include<stdio.h>  
int main()  
{  
    int a[100];  
    int t;  
    int n;  
    int i,j,temp;  
    int count;  
    scanf("%d",&t);  
    while(t--)  
    {  
        count = 0;  
        scanf("%d",&n);  
        for(i = 0; i < n; i++)  
        {  
            scanf("%d",&a[i]);  
        }  
        for(i = 0; i < n - 1; i++)  
        {  
            for(j = 0; j < n - 1 - i; j++)  
            {  
                if(a[j] > a[j+1])  
                {  
                    temp = a[j];  
                    a[j] = a[j+1];  
                    a[j+1] = temp;  
                    count++;  
                }  
            }  
        }  
        printf("%d\n",count);  
    }  
    return 0;  
}  

相关文章:

  • 2022-12-23
  • 2021-06-23
  • 2021-04-07
  • 2021-08-27
  • 2021-08-15
  • 2022-02-18
猜你喜欢
  • 2022-12-23
  • 2022-02-23
  • 2021-10-02
  • 2021-08-06
  • 2021-10-27
  • 2021-12-29
相关资源
相似解决方案