自己動手寫演算法.Sort.Bubble

來源:互聯網
上載者:User

Bubble Sort Algorithm

 

 

 

    This is an sorting algorithm which I learned when I began to study the C languange. I never wrote it down even though it's such a simple algorithm.

 

  • Implementation

C++:

Cs

  1. /*                                                                                  
  2. 1. pick the last one as the seed
  3. 2. if smaller than the previous one, swap it
  4. 3. else the previous becomes as the seed
  5. 4. this loop ends until to the 'top'
  6. we can make sure that the smallest one is on the top each time
  7. */
  8. void SortBubble::sort(int arr[], const int nSize)
  9. {
  10.     ALog::print(_T("# Bubble Sort: /n#"));
  11.     ALog::print(arr, nSize) << LINE_SPLIT;
  12.     int top = 0;
  13.     while( top < nSize-1 )
  14.     {       
  15.         ALog::print(_T("# ")) << top << std::endl;
  16.         for(int cur=nSize-1; cur>top; --cur)
  17.         {
  18.             if( arr[cur] < arr[cur-1] )
  19.             {
  20.                 std::swap(arr[cur], arr[cur-1]);
  21.             }
  22.             ALog::print(arr, nSize);
  23.         }
  24.         ++top;
  25.     }
  26.     ALog::print(LINE_SPLIT);
  27. }
  • Test data

The intermediate results:

 

  1. # Bubble Sort: 
  2. # 2 9 3 7 8 6 4 5 0 1
  3. =========================
  4. # 0
  5.  2 9 3 7 8 6 4 5 0 1
  6.  2 9 3 7 8 6 4 0 5 1
  7.  2 9 3 7 8 6 0 4 5 1
  8.  2 9 3 7 8 0 6 4 5 1
  9.  2 9 3 7 0 8 6 4 5 1
  10.  2 9 3 0 7 8 6 4 5 1
  11.  2 9 0 3 7 8 6 4 5 1
  12.  2 0 9 3 7 8 6 4 5 1
  13.  0 2 9 3 7 8 6 4 5 1
  14. # 1
  15.  0 2 9 3 7 8 6 4 1 5
  16.  0 2 9 3 7 8 6 1 4 5
  17.  0 2 9 3 7 8 1 6 4 5
  18.  0 2 9 3 7 1 8 6 4 5
  19.  0 2 9 3 1 7 8 6 4 5
  20.  0 2 9 1 3 7 8 6 4 5
  21.  0 2 1 9 3 7 8 6 4 5
  22.  0 1 2 9 3 7 8 6 4 5
  23. # 2
  24.  0 1 2 9 3 7 8 6 4 5
  25.  0 1 2 9 3 7 8 4 6 5
  26.  0 1 2 9 3 7 4 8 6 5
  27.  0 1 2 9 3 4 7 8 6 5
  28.  0 1 2 9 3 4 7 8 6 5
  29.  0 1 2 3 9 4 7 8 6 5
  30.  0 1 2 3 9 4 7 8 6 5
  31. # 3
  32.  0 1 2 3 9 4 7 8 5 6
  33.  0 1 2 3 9 4 7 5 8 6
  34.  0 1 2 3 9 4 5 7 8 6
  35.  0 1 2 3 9 4 5 7 8 6
  36.  0 1 2 3 4 9 5 7 8 6
  37.  0 1 2 3 4 9 5 7 8 6
  38. # 4
  39.  0 1 2 3 4 9 5 7 6 8
  40.  0 1 2 3 4 9 5 6 7 8
  41.  0 1 2 3 4 9 5 6 7 8
  42.  0 1 2 3 4 5 9 6 7 8
  43.  0 1 2 3 4 5 9 6 7 8
  44. # 5
  45.  0 1 2 3 4 5 9 6 7 8
  46.  0 1 2 3 4 5 9 6 7 8
  47.  0 1 2 3 4 5 6 9 7 8
  48.  0 1 2 3 4 5 6 9 7 8
  49. # 6
  50.  0 1 2 3 4 5 6 9 7 8
  51.  0 1 2 3 4 5 6 7 9 8
  52.  0 1 2 3 4 5 6 7 9 8
  53. # 7
  54.  0 1 2 3 4 5 6 7 8 9
  55.  0 1 2 3 4 5 6 7 8 9
  56. # 8
  57.  0 1 2 3 4 5 6 7 8 9
  58. =========================
  • Optimization

We can end the loops if there are no swaping operations.

  1. void SortBubble::sort2(int arr[], const int nSize)
  2. {
  3.     ALog::print(_T("# Bubble Sort: /n#"));
  4.     ALog::print(arr, nSize) << LINE_SPLIT;
  5.     int top = 0;
  6.     bool bNoSort = false;
  7.     while( !bNoSort && top < nSize-1 )
  8.     {       
  9.         bNoSort = true;
  10.         ALog::print(_T("# ")) << top << std::endl;
  11.         for(int cur=nSize-1; cur>top; --cur)
  12.         {
  13.             if( arr[cur] < arr[cur-1] )
  14.             {
  15.                 std::swap(arr[cur], arr[cur-1]);
  16.                 bNoSort = false;
  17.             }
  18.             ALog::print(arr, nSize);
  19.         }
  20.         ++top;
  21.     }
  22.     ALog::print(LINE_SPLIT);
  23. }

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.