一维数组的冒泡排序

 1 #include <stdio.h>
 2 int main(int argc, const char *argv[])
 3 {
 4     int i,j,t,count;
 5     int a[]={1,85,45,12,14,12,14,78,45,69};
 6     int n = sizeof(a)/sizeof(int);
 7     for(i=1;i<n;i++)//外部循环,循环的总轮数
 8     {
 9         count=0;//计数器
10         for(j=0;j<n-i;j++)//内部循环,每次比较的次数
11         {
12             if(a[j]>a[j+1])
13             {
14                 t=a[j];
15                 a[j]=a[j+1];
16                 a[j+1]=t; 
17                 count++;
18             }
19         }
20         if(count==0)
21             break;
22     }
23     for(i=0;i<n;i++)//遍历输出
24     {
25         printf("%d\t",a[i]);
26     }
27     return 0;
28 }

 

posted @ 2023-02-15 10:29  不思故乡  阅读(57)  评论(0)    收藏  举报