标题:
c语言 起泡法排序
[打印本页]
作者:
liuda
时间:
2015-1-22 02:36
标题:
c语言 起泡法排序
#include<stdio.h>//起泡法排序
void main()
{
int a[10];
int i,j,t;//i是数组的序号,j是中间变量,t也是变量的序号
printf("please input 10 numbers,use space to separate each other\n");
for(i=0;i<=9;i++)
{
scanf("%d",&a[i]);
}
//从头换到尾,大就换,不大就不换,然后继续比
for(i=0;i<=9;i++)
{
for(t=0;t<=9;t++)
{
if(a[t]>a[t+1])//这里用到了嵌套,但是两套之间没关系,因为只换一套
{
j=a[t];
a[t]=a[t+1];
a[t+1]=j;
}
}//变量不够用,再添
}
for(i=0;i<=9;i++)
{
printf("%5d,",a[i]);
}
}
//片面的算法是不够的,很容易出错,很容易不可行
复制代码
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1