实现{2,5,6,3,7,8,0,9,12,1}冒泡,从大到小。
void bubblesort(int k[],int n){ /*冒泡*/
int i,j,tmp ,flag = 1;
for(i=1;i<=n-1 && flag == 1;i++){ /*执行n-1趟*/
flag = 0;
for(j=1;j<=n-i;j++){
if(k[j]
tmp = k[j+1];
k[j+1] = k[j];
k[j] = tmp;
flag = 1;
}
}
}
}
void bubble()
{
int i,a[11] = {-111,2,5,6,3,7,8,0,9,12,1}; /*初始化序列,a[0]任意*/
printf("The orginal data array is\n") ;
for(i=1;i<=10;i++) /*显示原序列*/
printf("%d ",a[i]);
bubblesort(a,10); /*执行*/
printf("\nThe result of bubble sorting for the array is\n");
for(i=1;i<=10;i++)
printf("%d ",a[i]); /*输出*/
}
阅读(1379) | 评论(0) | 转发(0) |