# include<stdio.h> # include<stdlib.h> int main() { int a[6]={1,3,5,2,4,6}; int i,j,k,s=0; for(i=0;i<6;i++) { for(j=i;j<6;j++) { if(a[j]>a[i]) {s=a[i]; a[i]=a[j]; a[j]=s;} for(k=0;k<6;k++) printf("%2d",a[k]); printf("\n"); } printf("***************\n"); //for(k=0;k<6;k++) //printf("%2d",a[k]); //printf("\n"); } //for(i=0;i<6;i++) //printf("%2d",a[i]); //printf("\n"); // printf("%2d\n",fun(a)); } /*********************** 比较n(n-1)/2 T=O(N^2) 交换最好 0次 最差 n(n-1)/2 *************************/ [wz@localhost ~]$ gcc 1.c [wz@localhost ~]$ ./a.out 1 3 5 2 4 6 3 1 5 2 4 6 5 1 3 2 4 6 5 1 3 2 4 6 5 1 3 2 4 6 6 1 3 2 4 5 *************** 6 1 3 2 4 5 6 3 1 2 4 5 6 3 1 2 4 5 6 4 1 2 3 5 6 5 1 2 3 4 *************** 6 5 1 2 3 4 6 5 2 1 3 4 6 5 3 1 2 4 6 5 4 1 2 3 *************** 6 5 4 1 2 3 6 5 4 2 1 3 6 5 4 3 1 2 *************** 6 5 4 3 1 2 6 5 4 3 2 1 *************** 6 5 4 3 2 1 ***************
原文:http://sts609.blog.51cto.com/11227442/1749383