冒泡排序

 1 void bubbleSort(int array[], int n) {
 2     int lastElement;
 3     for (lastElement = n-1; lastElement > 0; --lastElement) {
 4         int i, flag = 0;
 5         for (i = 0; i < lastElement; ++i) {
 6             if (array[i] > array[i+1]) {
 7                 swap(&array[i], &array[i+1]);
 8                 flag = 1;
 9             }
10         }
11         //若flag=0,则没有发生交换,已经有序
12         if (flag == 0) {
13             break;
14         }
15     }
16 }
冒泡排序

 

posted @ 2020-04-18 10:12  小茗从不写博客  阅读(116)  评论(0)    收藏  举报