较小的数字如同气泡慢慢浮到上面,每一趟排序除了确定了一个最小值之外,还把其他的元素的位置也按照大小顺序做了一定的替换,这样在接下来的排序中就会减少交换的次数,显然该算法比初级的排序算法有改进。

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| #include <stdio.h>
#define MAXSIZE 100
typedef struct { int r[MAXSIZE+1]; int length; }SortList;
void BubbleSort02(SortList *L) { int i,j; int temp; for(i=1;i<L->length;i++) { for(j=L->length;j>i;j--) { if(L->r[j-1]>L->r[j]) { temp=L->r[j-1]; L->r[j-1]=L->r[j]; L->r[j]=temp; } } } }
|