/*代楼主干三分之一(冒泡法)*/
#include<stdio.h>
#define N 10 //排序量N个
void bubbleSorting(int[],int);main( )
{
int a[N],j;
printf("请输入%d个任意整数:\n",N);
for(j=0;j<N;j++)scanf("%d",&a[j]);
printf("排序前:");
for(j=0;j<N-1;j++)printf("%d,",a[j]);
printf("%d\n",a[j]);
bubbleSorting(a,N); printf("\n排序后:");
for(j=0;j<N-1;j++)printf("%d,",a[j]);
printf("%d\n",a[j]);
printf("\nPress any key to continue...\n");
scanf("%*c");
}
void bubbleSorting(int ary[],int n)
{
int i,j,tmp,
exFlag;
for(i=1;i<=n-1;i++)//从第1轮到第n-1轮
{
exFlag=0;
for(j=1;j<=n-i;j++)//第i轮中第j次比较与交换
if(ary[j-1]>ary[j])//若“左邻”大于“右舍”
{
exFlag=1;
tmp=ary[j-1];
ary[j-1]=ary[j];
ary[j]=tmp;
}
if(exFlag==0)break;//说明实际上已经排好序
printf("\n第%d轮后:",i);
for(j=0;j<n;j++)printf("%d ",ary[j]);
}
printf("\n");
}
[此贴子已经被作者于2006-6-8 4:41:49编辑过]