正在加载图片...
8.3冒泡排序void bubble (int a[], int n)( int i,j;程序解析for(i= 1; i< n; i++ )void bubble (int a[], int n);for (j = 0; j < n-i;j++)int main(void)if (a[] > a[i+1])I int n, a[8]; int i;t=a[]; a[]=a[j+1]; a[i+1]=t;printf("Entern (n<=8): ");1scanf("%d", &n);1printf("Entera[%d]:",n);for (i=0; i<n;i++)scanf("%d",&a[]);bubble(a, n);printf("Aftersorted, a[%d] =", n);for (i=0; i<n; i++)printf("%3d",a[i]);Enter n (n<=8): 8return 0;Enter a[8] : 7 3 66 3 -5 22 -77 21After sorted, a[8]=-77-5233722668.3 冒泡排序- 程序解析 void bubble (int a[ ], int n); int main(void) { int n, a[8]; int i; printf("Enter n (n<=8): "); scanf("%d", &n); printf("Enter a[%d] : ",n); for (i=0; i<n;i++) scanf("%d",&a[i]); bubble(a, n); printf("After sorted, a[%d] = ", n); for (i=0; i<n; i++) printf("%3d",a[i]); return 0; } void bubble (int a[ ], int n) { int i, j; for( i = 1; i < n; i++ ) for (j = 0; j < n-i; j++ ) if (a[j] > a[j+1]){ t=a[j]; a[j]=a[j+1]; a[j+1]=t; } } Enter n (n<=8): 8 Enter a[8] : 7 3 66 3 -5 22 -77 2 After sorted, a[8] = -77 -5 2 3 3 7 22 66
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有