升降排序
#include<stdio.h>
int main()
{
int a[5] = {1,78,55,23,8};
int i,j;
int temp = 0;
// printf("input 5 num\n");
//scanf("%d",&a[i]);
for (i = 1; i < 5; i++)
{
for(j = 0; j < 5-i; j++)
{
if(a[j] > a[j+1])
{
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
for(i = 0; i < 5; i++)
{
printf("%d\t",a[i]);
}
return 0;
}
这里是升排序,然后改变一下大小就是降。
冒泡排序
#include<stdio.h>
int main()
{
int i,j,temp;
int a[10];
for(i=0;i<10;i++) //for循环不写或&漏写都会段错误
scanf ("%d,",&a[i]);
for(j=0;j<=9;j++)
{
for (i=0;i<10-j-1;i++)
if (a[i]>a[i+1])
{
temp=a[i];
a[i]=a[i+1];
a[i+1]=temp;
}
}
for(i=0;i<10;i++)
printf("%d\t,",a[i]);
printf("\n");
return 0;
}