1. 程式人生 > >20 改進的氣泡排序--雙向氣泡排序

20 改進的氣泡排序--雙向氣泡排序

說明:請看看之前介紹過的氣泡排序法:

for(i = 0; i < MAX-1 && flag == 1; i++) {

         flag = 0;

         for(j = 0; j < MAX-i-1; j++) {

                 if(number[j+1] < number[j]) {

                         SWAP(number[j+1], number[j]);

                         flag = 1;

                }

          }

}

事實上這個氣泡排序法已經不是單純的氣泡排序了,它使用了旗標與右端左移兩個方法來改進排序的效能,而Shaker排序法使用到後面這個觀念進一步改良氣泡排序法。

解法:在上面的氣泡排序法中,交換的動作並不會一直進行至陣列的最後一個,而是會進行至MAX-i-1 ,所以排序的過程中,陣列右方排序好的元素會一直增加,使得左邊排序的次數逐漸減少如, 我們的例子所示:

排序前:95 27 90 49 80 58 6 9 18 50

27 90 49 80 58 6 9 18

50 [95] 95浮出

27 49 80 58 6 9

18 50

[90 95] 90浮出

27 49 58 6 9

18

50 [80

90 95] 80浮出

27 49 6 9 18

50

[58 80

90

95] ......

27 6 9 18 49

[50 58 80

90

95] ......

6 9 18 27 [49 50 58 80

90

95] ......

6 9 18 [27 49 50 58 80

90

95]

方括號括住的部份表示已排序完畢,Shaker排序使用了這個概念,如果讓左邊的元素也具有這 樣的性質,讓左右兩邊的元素都能先排序完成,如此未排序的元素會集中在中間,由於左右兩 邊同時排序,中間未排序的部份將會很快的減少。

方法就在於氣泡排序的雙向進行, 先讓氣泡排序由左向右進行, 再來讓氣泡排序由右往左進,行 如此完成一次排序的動作,而您必須使用 left與right兩個旗標來記錄左右兩端已排序的元素位置。

一個排序的例子如下所示:

排序前:45 19 77 81 13 28 18 19 77 11

往右排序:19 45 77 13 28 18 19 77 11 [81]

向左排序:[11]19 45 77 13 28 18 19 77 [81]

往右排序:[11]19 45 13 28 18 19 [77 77 81]

向左排序:[11 13] 19 45 18 28 19 [77 77 81]

往右排序:[11 13] 19 18 28 19 [45 77 77 81]

 向左排序:[11 13 18] 19 19 28 [45 77 77 81]

往右排序:[11 13 18] 19 19 [28 45 77 77 81]

 向左排序:[11 13 18 19 19] [28 45 77 77 81]

如上所示,括號中表示左右兩邊已排序完成的部份,當left > right時,則排序完成。

*************************程式***********************

#include <iostream>
#include <ctime>
using namespace std;

#define MAX 100
#define N 10
void bubble(int* a,const int& len);
inline void swap(int& x,int& y);

int main(int argc,char* argv[])
{
      srand((unsigned)time(0));   
      int a[MAX],n[N];  
      for(int i=0;i<MAX;i++)
            a[i]=i;
      for(int i=0;i<N;i++)  //產生一個無序陣列
      {
            int k=rand()%(MAX-i); 
            n[i]=a[k];
            if((MAX-i)<MAX){
               swap(a[k],a[MAX-i]);
            }
             cout <<n[i] <<" ";
      } 
      cout <<endl;
      bubble(n,N);    
      return 0;
}
 
void bubble(int* a,const int& len)  //雙向氣泡排序
{
    int left=0,right=len-1,count=0;
    while(left<right)
    {
        for(int i=0;i<len-1-i;i++)
        {
            int top=i,end=len-1-i;
            count++;  
            //比較大的數往後移
            if(a[top]>a[top+1]) swap(a[top],a[top+1]);
            //比較小的數往前移
            if(a[end]<a[end-1]) swap(a[end],a[end-1]);   
        }
        left++;
        right--;           
    }
   

 for(int i=0;i<=len-1;i++)
        cout <<a[i] <<" ";


    cout <<endl <<"迴圈了:" <<count <<"次" <<endl;
}
 
inline void swap(int& x,int& y) //交換x,y
{
     int temp=y;
     y=x;
     x=temp;
}

************************END**********************