1 #include <stdio.h>
2 int main()
3 {
4 int i,j,k;
5 int tem,length;
6 int a[10]={2,24,3,19,45,12,1,66,34,7};
7 // length = a.length();
8 length = 10;
9 printf("Before ordered:\n");
10 for(i = 0; i < length; i++)
11 printf("%d ",*(a+i));
12 printf("\n\n");
13 for(i = 1; i < length; i++)
14 {
15 for(j = 0; j < length-i; j++)
16 {
17 if(a[j]>a[j+1])
18 {
19 tem = a[j];
20 a[j] = a[j+1];
21 a[j+1] = tem;
22 }
23
24 }
25 printf("The %d time bubble order:\n",i);
26 for(k = 0; k < length; k++)
27 printf("%d ",*(a+k));
28 printf("\n\n");
29 }
30 printf("After ordered:\n");
31 for(i = 0; i < length; i++)
32 printf("%d ",*(a+i));
33 printf("\n");
34 return 0;
35 }
冒泡排序方法的复杂度为O(n)^2,每次冒泡找到最大(或最小)的数放到最后。
本文转自NewPanderKing51CTO博客,原文链接:http://www.cnblogs.com/newpanderking/archive/2012/03/25/2416473.html ,如需转载请自行联系原作者