(转)排序算法

Private Sub BubbleSortNumbers(varray As Variant)
  
   Dim cnt1 As Long
   Dim cnt2 As Long
   Dim tmp As Long
   Dim counter As Long
  
   Label12.Caption = "Working..."
  
   For cnt1 = UBound(varray) To LBound(varray) Step -1

      For cnt2 = LBound(varray) + 1 To cnt1
     
      If varray(cnt2 - 1) > varray(cnt2) Then
         tmp = varray(cnt2 - 1)
         varray(cnt2 - 1) = varray(cnt2)
         varray(cnt2) = tmp
     
        '-----------------------------
        'Required for the speed Test;
        'comment out for real use
         counter = counter + 1

         DoEvents
         If SkipFlag Then Exit For
        '----------------------------
     
      End If
     
      Next cnt2

   Next cnt1

   Label12.Caption = "Elements swapped : " & counter

End Sub


Private Sub QuickSortNumbers(varray As Variant, inLow As Long, inHigh As Long)

  'varray()  The varray to sort
  'inLow     First element of varray to start sort
  'inHigh    Last element of varray to start sort

  '----------------------------------------------------
  'update the call count label; comment out for real use

   QSCallCnt = QSCallCnt + 1
  '----------------------------------------------------

   Dim pivot As Long
  
   Dim tmpSwap As Long
   Dim tmpLow As Long
   Dim tmpHigh As Long
  
   tmpLow = inLow
   tmpHigh = inHigh
   pivot = varray((inLow + inHigh) / 2)

   While (tmpLow <= tmpHigh)
  
      While (varray(tmpLow) < pivot And tmpLow < inHigh)
         tmpLow = tmpLow + 1
      Wend
  
      While (pivot < varray(tmpHigh) And tmpHigh > inLow)
         tmpHigh = tmpHigh - 1
      Wend

      If (tmpLow <= tmpHigh) Then
         tmpSwap = varray(tmpLow)
         varray(tmpLow) = varray(tmpHigh)
         varray(tmpHigh) = tmpSwap
         tmpLow = tmpLow + 1
         tmpHigh = tmpHigh - 1

        '----------------------------------------------------
        'update the swap count label ; comment out for real use

         QSSwaps = QSSwaps + 1
        '----------------------------------------------------
      End If

   Wend
  
   If (inLow < tmpHigh) Then QuickSortNumbers varray, inLow, tmpHigh
   If (tmpLow < inHigh) Then QuickSortNumbers varray, tmpLow, inHigh
  
   Label15.Caption = "Sub was called : " & QSCallCnt & " times"
   Label16.Caption = "Elements Swapped : " & QSSwaps

End Sub


Private Sub SelectionSortNumbers(varray As Variant)

   Dim cnt1 As Long
   Dim cnt2 As Long
   Dim nMin As Long
   Dim tmp As Long
   Dim counter As Long
  
   Label13.Caption = "Working..."

   For cnt1 = LBound(varray) To UBound(varray) - 1

      nMin = cnt1

      For cnt2 = (cnt1 + 1) To UBound(varray)

         If varray(cnt2) < varray(nMin) Then
            nMin = cnt2
           '----------------------------------------------------
           'comment out for real use
           'update the iterations label

            counter = counter + 1
           '----------------------------------------------------
         End If

          '----------------------------------------------------
          'Required to enable abort of speed Test;
          'comment out for real use

           DoEvents
           If SkipFlag Then Exit For
          '----------------------------------------------------

      Next cnt2

      tmp = varray(nMin)
      varray(nMin) = varray(cnt1)
      varray(cnt1) = tmp

   Next cnt1

   Label13.Caption = "Elements swapped : " & counter

End Sub


Private Sub ShellSortNumbers(varray As Variant)

   Dim cnt As Long
   Dim tmp As Long
   Dim nHold As Long
   Dim nHValue As Long
   Dim counter As Long
  
   Label14.Caption = "Working..."
  
   nHValue = LBound(varray)
  
   Do
      nHValue = 3 * nHValue + 1
   Loop Until nHValue > UBound(varray)
  
   Do
      nHValue = nHValue / 3

      For cnt = nHValue + LBound(varray) To UBound(varray)

         tmp = varray(cnt)
         nHold = cnt

         Do While varray(nHold - nHValue) > tmp
            varray(nHold) = varray(nHold - nHValue)
            nHold = nHold - nHValue

          '----------------------------------------------------
           'Required for the speed Test; comment out for real use
           'update the iterations label

            counter = counter + 1
            DoEvents
           '----------------------------------------------------

            If nHold < nHValue Then Exit Do

         Loop

         varray(nHold) = tmp

      Next cnt

   Loop Until nHValue = LBound(varray)

   Label14.Caption = "Elements swapped : " & counter

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值