找回密码
 加入我们

QQ登录

只需一步,快速开始

搜索
查看: 4434|回复: 3

VB QuickSort

  [复制链接]

12

主题

144

回帖

0

精华

铜牌会员

积分
281
发表于 2011-12-1 12:02:18 | 显示全部楼层 |阅读模式

  1. Option Explicit

  2. Private Sub QuicksortInt(list() As Integer, ByVal min As Integer, ByVal max As Integer)
  3. Dim med_value As Integer
  4. Dim hi As Integer
  5. Dim lo As Integer
  6. Dim i As Integer

  7.     ' If the list has no more than CutOff elements,
  8.     ' finish it off with SelectionSort.
  9.     If max <= min Then Exit Sub

  10.     ' Pick the dividing value.
  11.     i = Int((max - min + 1) * Rnd + min)
  12.     med_value = list(i)

  13.     ' Swap it to the front.
  14.     list(i) = list(min)

  15.     lo = min
  16.     hi = max
  17.     Do
  18.         ' Look down from hi for a value < med_value.
  19.         Do While list(hi) >= med_value
  20.             hi = hi - 1
  21.             If hi <= lo Then Exit Do
  22.         Loop
  23.         If hi <= lo Then
  24.             list(lo) = med_value
  25.             Exit Do
  26.         End If

  27.         ' Swap the lo and hi values.
  28.         list(lo) = list(hi)
  29.       
  30.         ' Look up from lo for a value >= med_value.
  31.         lo = lo + 1
  32.         Do While list(lo) < med_value
  33.             lo = lo + 1
  34.             If lo >= hi Then Exit Do
  35.         Loop
  36.         If lo >= hi Then
  37.             lo = hi
  38.             list(hi) = med_value
  39.             Exit Do
  40.         End If
  41.       
  42.         ' Swap the lo and hi values.
  43.         list(hi) = list(lo)
  44.     Loop
  45.    
  46.     ' Sort the two sublists.
  47.     QuicksortInt list(), min, lo - 1
  48.     QuicksortInt list(), lo + 1, max
  49. End Sub
  50. Private Sub QuicksortSingle(list() As Single, ByVal min As Integer, ByVal max As Integer)
  51. Dim med_value As Single
  52. Dim hi As Integer
  53. Dim lo As Integer
  54. Dim i As Integer

  55.     ' If the list has no more than CutOff elements,
  56.     ' finish it off with SelectionSort.
  57.     If max <= min Then Exit Sub

  58.     ' Pick the dividing value.
  59.     i = Int((max - min + 1) * Rnd + min)
  60.     med_value = list(i)

  61.     ' Swap it to the front.
  62.     list(i) = list(min)

  63.     lo = min
  64.     hi = max
  65.     Do
  66.         ' Look down from hi for a value < med_value.
  67.         Do While list(hi) >= med_value
  68.             hi = hi - 1
  69.             If hi <= lo Then Exit Do
  70.         Loop
  71.         If hi <= lo Then
  72.             list(lo) = med_value
  73.             Exit Do
  74.         End If

  75.         ' Swap the lo and hi values.
  76.         list(lo) = list(hi)
  77.       
  78.         ' Look up from lo for a value >= med_value.
  79.         lo = lo + 1
  80.         Do While list(lo) < med_value
  81.             lo = lo + 1
  82.             If lo >= hi Then Exit Do
  83.         Loop
  84.         If lo >= hi Then
  85.             lo = hi
  86.             list(hi) = med_value
  87.             Exit Do
  88.         End If
  89.       
  90.         ' Swap the lo and hi values.
  91.         list(hi) = list(lo)
  92.     Loop
  93.    
  94.     ' Sort the two sublists.
  95.     QuicksortSingle list(), min, lo - 1
  96.     QuicksortSingle list(), lo + 1, max
  97. End Sub
  98. Private Sub QuicksortDouble(list() As Double, ByVal min As Integer, ByVal max As Integer)
  99. Dim med_value As Double
  100. Dim hi As Integer
  101. Dim lo As Integer
  102. Dim i As Integer

  103.     ' If the list has no more than CutOff elements,
  104.     ' finish it off with SelectionSort.
  105.     If max <= min Then Exit Sub

  106.     ' Pick the dividing value.
  107.     i = Int((max - min + 1) * Rnd + min)
  108.     med_value = list(i)

  109.     ' Swap it to the front.
  110.     list(i) = list(min)

  111.     lo = min
  112.     hi = max
  113.     Do
  114.         ' Look down from hi for a value < med_value.
  115.         Do While list(hi) >= med_value
  116.             hi = hi - 1
  117.             If hi <= lo Then Exit Do
  118.         Loop
  119.         If hi <= lo Then
  120.             list(lo) = med_value
  121.             Exit Do
  122.         End If

  123.         ' Swap the lo and hi values.
  124.         list(lo) = list(hi)
  125.       
  126.         ' Look up from lo for a value >= med_value.
  127.         lo = lo + 1
  128.         Do While list(lo) < med_value
  129.             lo = lo + 1
  130.             If lo >= hi Then Exit Do
  131.         Loop
  132.         If lo >= hi Then
  133.             lo = hi
  134.             list(hi) = med_value
  135.             Exit Do
  136.         End If
  137.       
  138.         ' Swap the lo and hi values.
  139.         list(hi) = list(lo)
  140.     Loop
  141.    
  142.     ' Sort the two sublists.
  143.     QuicksortDouble list(), min, lo - 1
  144.     QuicksortDouble list(), lo + 1, max
  145. End Sub
  146. Private Sub QuicksortString(list() As String, ByVal min As Integer, ByVal max As Integer)
  147. Dim med_value As String
  148. Dim hi As Integer
  149. Dim lo As Integer
  150. Dim i As Integer

  151.     ' If the list has no more than CutOff elements,
  152.     ' finish it off with SelectionSort.
  153.     If max <= min Then Exit Sub

  154.     ' Pick the dividing value.
  155.     i = Int((max - min + 1) * Rnd + min)
  156.     med_value = list(i)

  157.     ' Swap it to the front.
  158.     list(i) = list(min)

  159.     lo = min
  160.     hi = max
  161.     Do
  162.         ' Look down from hi for a value < med_value.
  163.         Do While list(hi) >= med_value
  164.             hi = hi - 1
  165.             If hi <= lo Then Exit Do
  166.         Loop
  167.         If hi <= lo Then
  168.             list(lo) = med_value
  169.             Exit Do
  170.         End If

  171.         ' Swap the lo and hi values.
  172.         list(lo) = list(hi)
  173.       
  174.         ' Look up from lo for a value >= med_value.
  175.         lo = lo + 1
  176.         Do While list(lo) < med_value
  177.             lo = lo + 1
  178.             If lo >= hi Then Exit Do
  179.         Loop
  180.         If lo >= hi Then
  181.             lo = hi
  182.             list(hi) = med_value
  183.             Exit Do
  184.         End If
  185.       
  186.         ' Swap the lo and hi values.
  187.         list(hi) = list(lo)
  188.     Loop
  189.    
  190.     ' Sort the two sublists.
  191.     QuicksortString list(), min, lo - 1
  192.     QuicksortString list(), lo + 1, max
  193. End Sub
  194. Private Sub QuicksortVariant(list() As Variant, ByVal min As Integer, ByVal max As Integer)
  195. Dim med_value As Variant
  196. Dim hi As Integer
  197. Dim lo As Integer
  198. Dim i As Integer

  199.     ' If the list has no more than CutOff elements,
  200.     ' finish it off with SelectionSort.
  201.     If max <= min Then Exit Sub

  202.     ' Pick the dividing value.
  203.     i = Int((max - min + 1) * Rnd + min)
  204.     med_value = list(i)

  205.     ' Swap it to the front.
  206.     list(i) = list(min)

  207.     lo = min
  208.     hi = max
  209.     Do
  210.         ' Look down from hi for a value < med_value.
  211.         Do While list(hi) >= med_value
  212.             hi = hi - 1
  213.             If hi <= lo Then Exit Do
  214.         Loop
  215.         If hi <= lo Then
  216.             list(lo) = med_value
  217.             Exit Do
  218.         End If

  219.         ' Swap the lo and hi values.
  220.         list(lo) = list(hi)
  221.       
  222.         ' Look up from lo for a value >= med_value.
  223.         lo = lo + 1
  224.         Do While list(lo) < med_value
  225.             lo = lo + 1
  226.             If lo >= hi Then Exit Do
  227.         Loop
  228.         If lo >= hi Then
  229.             lo = hi
  230.             list(hi) = med_value
  231.             Exit Do
  232.         End If
  233.       
  234.         ' Swap the lo and hi values.
  235.         list(hi) = list(lo)
  236.     Loop
  237.    
  238.     ' Sort the two sublists.
  239.     QuicksortVariant list(), min, lo - 1
  240.     QuicksortVariant list(), lo + 1, max
  241. End Sub

  242. Private Sub QuicksortLong(list() As Long, ByVal min As Integer, ByVal max As Integer)
  243. Dim med_value As Long
  244. Dim hi As Integer
  245. Dim lo As Integer
  246. Dim i As Integer

  247.     ' If the list has no more than CutOff elements,
  248.     ' finish it off with SelectionSort.
  249.     If max <= min Then Exit Sub

  250.     ' Pick the dividing value.
  251.     i = Int((max - min + 1) * Rnd + min)
  252.     med_value = list(i)

  253.     ' Swap it to the front.
  254.     list(i) = list(min)

  255.     lo = min
  256.     hi = max
  257.     Do
  258.         ' Look down from hi for a value < med_value.
  259.         Do While list(hi) >= med_value
  260.             hi = hi - 1
  261.             If hi <= lo Then Exit Do
  262.         Loop
  263.         If hi <= lo Then
  264.             list(lo) = med_value
  265.             Exit Do
  266.         End If

  267.         ' Swap the lo and hi values.
  268.         list(lo) = list(hi)
  269.       
  270.         ' Look up from lo for a value >= med_value.
  271.         lo = lo + 1
  272.         Do While list(lo) < med_value
  273.             lo = lo + 1
  274.             If lo >= hi Then Exit Do
  275.         Loop
  276.         If lo >= hi Then
  277.             lo = hi
  278.             list(hi) = med_value
  279.             Exit Do
  280.         End If
  281.       
  282.         ' Swap the lo and hi values.
  283.         list(hi) = list(lo)
  284.     Loop
  285.    
  286.     ' Sort the two sublists.
  287.     QuicksortLong list(), min, lo - 1
  288.     QuicksortLong list(), lo + 1, max
  289. End Sub

  290. ' Sort an array of integers.
  291. Public Sub SortIntArray(list() As Integer)
  292.     QuicksortInt list, LBound(list), UBound(list)
  293. End Sub
  294. ' Sort an array of longs.
  295. Public Sub SortLongArray(list() As Long)
  296.     QuicksortLong list, LBound(list), UBound(list)
  297. End Sub
  298. ' Sort an array of singles.
  299. Public Sub SortSingleArray(list() As Single)
  300.     QuicksortSingle list, LBound(list), UBound(list)
  301. End Sub
  302. ' Sort an array of doubles.
  303. Public Sub SortDoubleArray(list() As Double)
  304.     QuicksortDouble list, LBound(list), UBound(list)
  305. End Sub
  306. ' Sort an array of strings.
  307. Public Sub SortStringArray(list() As String)
  308.     QuicksortString list, LBound(list), UBound(list)
  309. End Sub
  310. ' Sort an array of variants.
  311. Public Sub SortVariantArray(list() As Variant)
  312.     QuicksortVariant list, LBound(list), UBound(list)
  313. End Sub
复制代码

评分

参与人数 1水晶币 +20 收起 理由
马大哈 + 20 很给力!

查看全部评分

275

主题

3017

回帖

1

精华

管理员

嗷嗷叫的老马

积分
17064

论坛牛人贡献奖关注奖最佳版主进步奖人气王疯狂作品奖精英奖赞助论坛勋章乐于助人勋章

QQ
发表于 2011-12-2 14:54:34 | 显示全部楼层
建议增加使用示例......
我就是嗷嗷叫的老马了......

857

主题

2632

回帖

2

精华

管理员

此生无悔入华夏,  长居日耳曼尼亚。  

积分
36130
发表于 2011-12-2 16:10:11 | 显示全部楼层
马大哈 发表于 2011-12-2 14:54
建议增加使用示例......

老马,这还要使用示例吗?明眼人一下子就看明白了啊:

  1. ' Sort an array of integers.

  2. Public Sub SortIntArray(list() As Integer)

  3.     QuicksortInt list, LBound(list), UBound(list)

  4. End Sub

  5. ' Sort an array of longs.

  6. Public Sub SortLongArray(list() As Long)

  7.     QuicksortLong list, LBound(list), UBound(list)

  8. End Sub

  9. ' Sort an array of singles.

  10. Public Sub SortSingleArray(list() As Single)

  11.     QuicksortSingle list, LBound(list), UBound(list)

  12. End Sub

  13. ' Sort an array of doubles.

  14. Public Sub SortDoubleArray(list() As Double)

  15.     QuicksortDouble list, LBound(list), UBound(list)

  16. End Sub

  17. ' Sort an array of strings.

  18. Public Sub SortStringArray(list() As String)

  19.     QuicksortString list, LBound(list), UBound(list)

  20. End Sub

  21. ' Sort an array of variants.

  22. Public Sub SortVariantArray(list() As Variant)

  23.     QuicksortVariant list, LBound(list), UBound(list)

  24. End Sub
复制代码

275

主题

3017

回帖

1

精华

管理员

嗷嗷叫的老马

积分
17064

论坛牛人贡献奖关注奖最佳版主进步奖人气王疯狂作品奖精英奖赞助论坛勋章乐于助人勋章

QQ
发表于 2011-12-4 00:33:21 | 显示全部楼层
Tesla.Angela 发表于 2011-12-2 16:10
老马,这还要使用示例吗?明眼人一下子就看明白了啊:

还是要考虑初学者啊.

不是所有人都一上来就会使用的,嘿嘿.

我的那些模块不也封装得很简单么?而且还写了示例.

结果还不是一大堆人问用法.
我就是嗷嗷叫的老马了......

您需要登录后才可以回帖 登录 | 加入我们

本版积分规则

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