经典排序之冒泡排序代码

 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,每次冒泡找到最大(或最小)的数放到最后。

posted on 2012-03-25 12:25  NewPanderKing  阅读(426)  评论(0编辑  收藏  举报

导航