c语言:编写冒泡排序,排序一个整形数组(从小到大)
程序员文章站
2023-11-02 18:19:28
程序:不妨按从小到大排序
#include
int main ()
{
int a[10];
int i = 0;
int j =...
程序:不妨按从小到大排序
#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