找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2468|回复: 0
收起左侧

VB6 排序算法

[复制链接]
ID:94349 发表于 2015-11-2 14:27 | 显示全部楼层 |阅读模式
  1. Attribute VB_Name = "Module1"
  2. Option Explicit
  3. Global Const ZERO = 0
  4. Global Const ASCENDING_ORDER = 0
  5. Global Const DESCENDING_ORDER = 1

  6. Global gIterations
  7. '
  8. Sub BubbleSort(MyArray(), ByVal nOrder As Integer)
  9. Dim Index
  10. Dim TEMP
  11. Dim NextElement

  12.     NextElement = ZERO
  13.     Do While (NextElement < UBound(MyArray))
  14.         Index = UBound(MyArray)
  15.         Do While (Index > NextElement)
  16.             If nOrder = ASCENDING_ORDER Then
  17.                 If MyArray(Index) < MyArray(Index - 1) Then
  18.                     TEMP = MyArray(Index)
  19.                     MyArray(Index) = MyArray(Index - 1)
  20.                     MyArray(Index - 1) = TEMP
  21.                 End If
  22.             ElseIf nOrder = DESCENDING_ORDER Then
  23.                 If MyArray(Index) >= MyArray(Index - 1) Then
  24.                     TEMP = MyArray(Index)
  25.                     MyArray(Index) = MyArray(Index - 1)
  26.                     MyArray(Index - 1) = TEMP
  27.                 End If
  28.             End If
  29.             Index = Index - 1
  30.             gIterations = gIterations + 1
  31.         Loop
  32.         NextElement = NextElement + 1
  33.         gIterations = gIterations + 1
  34.     Loop

  35. End Sub

  36. Sub Bucket(MyArray(), ByVal nOrder As Integer)
  37. Dim Index
  38. Dim NextElement
  39. Dim TheBucket

  40.     NextElement = LBound(MyArray) + 1
  41.     While (NextElement <= UBound(MyArray))
  42.         TheBucket = MyArray(NextElement)
  43.         Index = NextElement
  44.         Do
  45.             If Index > LBound(MyArray) Then
  46.                 If nOrder = ASCENDING_ORDER Then
  47.                     If TheBucket < MyArray(Index - 1) Then
  48.                         MyArray(Index) = MyArray(Index - 1)
  49.                         Index = Index - 1
  50.                     Else
  51.                         Exit Do
  52.                     End If
  53.                 ElseIf nOrder = DESCENDING_ORDER Then
  54.                     If TheBucket >= MyArray(Index - 1) Then
  55.                         MyArray(Index) = MyArray(Index - 1)
  56.                         Index = Index - 1
  57.                     Else
  58.                         Exit Do
  59.                     End If
  60.                 End If
  61.             Else
  62.                 Exit Do
  63.             End If
  64.             gIterations = gIterations + 1
  65.         Loop
  66.         MyArray(Index) = TheBucket
  67.         NextElement = NextElement + 1
  68.         gIterations = gIterations + 1
  69.     Wend

  70. End Sub

  71. Sub Heap(MyArray())
  72. Dim Index
  73. Dim Size
  74. Dim TEMP

  75.     Size = UBound(MyArray)
  76.      
  77.     Index = 1
  78.     While (Index <= Size)
  79.         Call HeapSiftup(MyArray(), Index)
  80.         Index = Index + 1
  81.         gIterations = gIterations + 1
  82.     Wend

  83.     Index = Size
  84.     While (Index > 0)
  85.         TEMP = MyArray(0)
  86.         MyArray(0) = MyArray(Index)
  87.         MyArray(Index) = TEMP
  88.         Call HeapSiftdown(MyArray(), Index - 1)
  89.         Index = Index - 1
  90.         gIterations = gIterations + 1
  91.     Wend

  92. End Sub
  93.   

  94. Sub HeapSiftdown(MyArray(), M)
  95. Dim Index
  96. Dim Parent
  97. Dim TEMP

  98.     Index = 0
  99.     Parent = 2 * Index

  100.     Do While (Parent <= M)
  101.          
  102.         If (Parent < M And MyArray(Parent) < MyArray(Parent + 1)) Then
  103.             Parent = Parent + 1
  104.         End If

  105.         If MyArray(Index) >= MyArray(Parent) Then
  106.             Exit Do
  107.         End If

  108.         TEMP = MyArray(Index)
  109.         MyArray(Index) = MyArray(Parent)
  110.         MyArray(Parent) = TEMP
  111.          
  112.         Index = Parent
  113.         Parent = 2 * Index

  114.         gIterations = gIterations + 1
  115.     Loop
  116. End Sub

  117. Sub HeapSiftup(MyArray(), M)
  118. Dim Index
  119. Dim Parent
  120. Dim TEMP

  121.     Index = M
  122.     Do While (Index > 0)
  123.         Parent = Int(Index / 2)

  124.         If MyArray(Parent) >= MyArray(Index) Then
  125.             Exit Do
  126.         End If
  127.          
  128.         TEMP = MyArray(Index)
  129.         MyArray(Index) = MyArray(Parent)
  130.         MyArray(Parent) = TEMP

  131.         Index = Parent
  132.         gIterations = gIterations + 1
  133.     Loop
  134.      
  135. End Sub

  136. Sub Insertion(MyArray(), ByVal nOrder As Integer)
  137. Dim Index
  138. Dim TEMP
  139. Dim NextElement
  140.      
  141.     NextElement = LBound(MyArray) + 1
  142.     While (NextElement <= UBound(MyArray))
  143.         Index = NextElement
  144.         Do
  145.             If Index > LBound(MyArray) Then
  146.                 If nOrder = ASCENDING_ORDER Then
  147.                     If MyArray(Index) < MyArray(Index - 1) Then
  148.                         TEMP = MyArray(Index)
  149.                         MyArray(Index) = MyArray(Index - 1)
  150.                         MyArray(Index - 1) = TEMP
  151.                         Index = Index - 1
  152.                     Else
  153.                         Exit Do
  154.                     End If
  155.                 ElseIf nOrder = DESCENDING_ORDER Then
  156.                     If MyArray(Index) >= MyArray(Index - 1) Then
  157.                         TEMP = MyArray(Index)
  158.                         MyArray(Index) = MyArray(Index - 1)
  159.                         MyArray(Index - 1) = TEMP
  160.                         Index = Index - 1
  161.                     Else
  162.                         Exit Do
  163.                     End If
  164.                 End If
  165.             Else
  166.                 Exit Do
  167.             End If
  168.             gIterations = gIterations + 1
  169.         Loop
  170.         NextElement = NextElement + 1
  171.         gIterations = gIterations + 1
  172.     Wend

  173. End Sub

  174. Sub QuickSort(MyArray(), L, R)
  175. Dim I, J, X, Y

  176.     I = L
  177.     J = R
  178.     X = MyArray((L + R) / 2)
  179.          
  180.     While (I <= J)
  181.         While (MyArray(I) < X And I < R)
  182.             I = I + 1
  183.         Wend
  184.         While (X < MyArray(J) And J > L)
  185.             J = J - 1
  186.         Wend
  187.         If (I <= J) Then
  188.             Y = MyArray(I)
  189.             MyArray(I) = MyArray(J)
  190.             MyArray(J) = Y
  191.             I = I + 1
  192.             J = J - 1
  193.         End If
  194.         gIterations = gIterations + 1
  195.     Wend

  196.     If (L < J) Then Call QuickSort(MyArray(), L, J)
  197.     If (I < R) Then Call QuickSort(MyArray(), I, R)

  198. End Sub

  199. Sub Selection(MyArray(), ByVal nOrder As Integer)
  200. Dim Index
  201. Dim Min
  202. Dim NextElement
  203. Dim TEMP

  204.     NextElement = 0
  205.     While (NextElement < UBound(MyArray))
  206.         Min = UBound(MyArray)
  207.         Index = Min - 1
  208.         While (Index >= NextElement)
  209.             If nOrder = ASCENDING_ORDER Then
  210.                 If MyArray(Index) < MyArray(Min) Then
  211.                     Min = Index
  212.                 End If
  213.             ElseIf nOrder = DESCENDING_ORDER Then
  214.                 If MyArray(Index) >= MyArray(Min) Then
  215.                     Min = Index
  216.                 End If
  217.             End If
  218.             Index = Index - 1
  219.             gIterations = gIterations + 1
  220.         Wend
  221.         TEMP = MyArray(Min)
  222.         MyArray(Min) = MyArray(NextElement)
  223.         MyArray(NextElement) = TEMP
  224.         NextElement = NextElement + 1
  225.         gIterations = gIterations - 1
  226.     Wend

  227. End Sub

  228. Sub ShellSort(MyArray(), ByVal nOrder As Integer)
  229. Dim Distance
  230. Dim Size
  231. Dim Index
  232. Dim NextElement
  233. Dim TEMP

  234.     Size = UBound(MyArray) - LBound(MyArray) + 1
  235.     Distance = 1

  236.     While (Distance <= Size)
  237.         Distance = 2 * Distance
  238.     Wend

  239.     Distance = (Distance / 2) - 1
  240.      
  241.     While (Distance > 0)
  242.      
  243.         NextElement = LBound(MyArray) + Distance
  244.      
  245.         While (NextElement <= UBound(MyArray))
  246.             Index = NextElement
  247.             Do
  248.                 If Index >= (LBound(MyArray) + Distance) Then
  249.                     If nOrder = ASCENDING_ORDER Then
  250.                         If MyArray(Index) < MyArray(Index - Distance) Then
  251.                             TEMP = MyArray(Index)
  252.                             MyArray(Index) = MyArray(Index - Distance)
  253.                             MyArray(Index - Distance) = TEMP
  254.                             Index = Index - Distance
  255.                             gIterations = gIterations + 1
  256.                         Else
  257.                             Exit Do
  258.                         End If
  259.                     ElseIf nOrder = DESCENDING_ORDER Then
  260.                         If MyArray(Index) >= MyArray(Index - Distance) Then
  261.                             TEMP = MyArray(Index)
  262.                             MyArray(Index) = MyArray(Index - Distance)
  263.                             MyArray(Index - Distance) = TEMP
  264.                             Index = Index - Distance
  265.                             gIterations = gIterations + 1
  266.                         Else
  267.                             Exit Do
  268.                         End If
  269.                     End If
  270.                 Else
  271.                     Exit Do
  272.                 End If
  273.             Loop
  274.             NextElement = NextElement + 1
  275.             gIterations = gIterations + 1
  276.         Wend
  277.         Distance = (Distance - 1) / 2
  278.         gIterations = gIterations + 1
  279.     Wend
  280.      
  281. End Sub
复制代码




回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表