自己动手写算法.Sort.Bubble

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. }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值