C语言 冒泡排序法的代码
收起
知与谁同
2018-07-16 16:28:53
1938
0
4
条回答
写回答
取消
提交回答
-
冒泡法排序的原理是相邻的两个数进行比较,现在以“输入10个数,对它们按小到大的顺序排序”这道题,来展示冒泡法排序。
#include <stdio.h>
int main(){
int a[10];
int i,j,t;
printf (" input 10 numbers: \n");
for (i=0;i<10;i++)
scanf("%d",&a[i] );
printf ("\n");
for(j=0;j<9;j++)
for(i=0;i<9-j;i++)
if (a[i]>a[i+1])
{t=a[i];a[i]=a[i+1];a[i+1]=t;}
printf("the sorted numbers: \n");
for(i=0;i<10;i++)
printf( "%d",a[i]);
printf("\n");
return 0;
}
2019-07-17 22:50:06
-
一个简单的xml文档
<?xml version="1.0" encoding="utf8" standalone="yes"?>
<cars>
<car name = "奔驰">
<carPrice>100000</carPrice>
<carColor>red</carColor>
</car>
</cars>
2019-07-17 22:50:06
-
main()
{
int i,j,temp;
int a[10];
for(i=0;i<10;i++)
scanf ("%d,",&a[i]);
for(j=0;j<=9;j++)
{ for (i=0;i<10-j;i++)
if (a[i]>a[i+1])
{ temp=a[i];
a[i]=a[i+1];
a[i+1]=temp;}
}
for(i=1;i<11;i++)
printf("%5d,",a[i] );
printf("\n");
}
2019-07-17 22:50:06
-
#include <stdio.h>
void bubble_sort(int a[], int n)
{int i, j, temp;
for (j = 0; j < n - 1; j++)
for (i = 0; i < n - 1 - j; i++)
if(a[i] > a[i + 1])
{temp=a[i]; a[i]=a[i+1]; a[i+1]=temp;}
}
int main()
{int number[10] = {95, 45, 15, 78, 84, 51, 24, 12, 38, 97};
int i,SIZE=10;
bubble_sort(number, SIZE);
for (i = 0; i < SIZE; i++)
printf("%d", number[i]);
printf("\n");
}
2019-07-17 22:50:06