c語言:編寫冒泡排序,排序一個整形數組(從小到大)
程序:不妨按從小到大排序
#include <stdio.h>
int main ()
{
int a[10];
int i = 0;
int j = 0;
int t = 0;
printf ("input 10 numbers:");
for ( i = 0; i < 10; i++)
{
scanf ("%d",&a[i]);
}
for (i = 0; i < 9; i++)
for ( j = 0; j < 9 - i; j++)
if (a[j] > a[j+1])
{
t = a[j];
a[j] = a[j+1];
a[j+1] = t;
}
printf ("the sorted numbers:\n");
//"the sorted numbers"表示排序的數字
for (i =0; i < 10; i++)
printf ("%d\t", a[i]);
printf ("\n");
return 0;
}
輸出結果:
input 10 numbers:11 2 3 5 34 6 78 9 12 62
the sorted numbers:
2 3 5 6 9 11 12 34 62 78