VBA排序的十种算法.doc

上传人:w****2 文档编号:6588972 上传时间:2020-02-29 格式:DOC 页数:18 大小:62KB
返回 下载 相关 举报
VBA排序的十种算法.doc_第1页
第1页 / 共18页
VBA排序的十种算法.doc_第2页
第2页 / 共18页
VBA排序的十种算法.doc_第3页
第3页 / 共18页
点击查看更多>>
资源描述
在使用VBA进行写程序时,经常会做排序,下面将会给出一些常用的排序算法的实现,方便大家写程序参考,若代码中出现了错误,欢迎高手指正。主要算法有:1、(冒泡排序)Bubble sort2、(选择排序)Selection sort3、(插入排序)Insertion sort4、(快速排序)Quick sort5、(合并排序)Merge sort6、(堆排序)Heap sort7、(组合排序)Comb Sort8、(希尔排序)Shell Sort9、(基数排序)Radix Sort10、Shaker Sort第一种 (冒泡排序)Bubble sortPublic Sub BubbleSort(ByRef lngArray() As Long) Dim iOuter As Long Dim iInner As Long Dim iLBound As Long Dim iUBound As Long Dim iTemp As Long iLBound = LBound(lngArray) iUBound = UBound(lngArray) 冒泡排序 For iOuter = iLBound To iUBound - 1 For iInner = iLBound To iUBound - iOuter - 1 比较相邻项 If lngArray(iInner) lngArray(iInner + 1) Then 交换值 iTemp = lngArray(iInner) lngArray(iInner) = lngArray(iInner + 1) lngArray(iInner + 1) = iTemp End If Next iInner Next iOuterEnd Sub2、(选择排序)Selection sort1. Public Sub SelectionSort(ByRef lngArray() As Long)2. Dim iOuter As Long3. Dim iInner As Long4. Dim iLBound As Long5. Dim iUBound As Long6. Dim iTemp As Long7. Dim iMax As Long8.9. iLBound = LBound(lngArray)10. iUBound = UBound(lngArray)11.12. 选择排序13. For iOuter = iUBound To iLBound + 1 Step -114.15. iMax = 016.17. 得到最大值得索引18. For iInner = iLBound To iOuter19. If lngArray(iInner) lngArray(iMax) Then iMax = iInner20. Next iInner21.22. 值交换23. iTemp = lngArray(iMax)24. lngArray(iMax) = lngArray(iOuter)25. lngArray(iOuter) = iTemp26.27. Next iOuter28. End Sub复制代码第三种 (插入排序)Insertion sort1. Public Sub InsertionSort(ByRef lngArray() As Long)2. Dim iOuter As Long3. Dim iInner As Long4. Dim iLBound As Long5. Dim iUBound As Long6. Dim iTemp As Long7.8. iLBound = LBound(lngArray)9. iUBound = UBound(lngArray)10.11. For iOuter = iLBound + 1 To iUBound12.13. 取得插入值14. iTemp = lngArray(iOuter)15.16. 移动已经排序的值17. For iInner = iOuter - 1 To iLBound Step -118. If lngArray(iInner) lngArray(iMax) Then iMax = iOuter15. Next iOuter16.17. iTemp = lngArray(iMax)18. lngArray(iMax) = lngArray(iUBound)19. lngArray(iUBound) = iTemp20.21. 开始快速排序22. InnerQuickSort lngArray, iLBound, iUBound23. End If24. End Sub25.26. Private Sub InnerQuickSort(ByRef lngArray() As Long, ByVal iLeftEnd As Long, ByVal iRightEnd As Long)27. Dim iLeftCur As Long28. Dim iRightCur As Long29. Dim iPivot As Long30. Dim iTemp As Long31.32. If iLeftEnd = iRightEnd Then Exit Sub33.34. iLeftCur = iLeftEnd35. iRightCur = iRightEnd + 136. iPivot = lngArray(iLeftEnd)37.38. Do39. Do40. iLeftCur = iLeftCur + 141. Loop While lngArray(iLeftCur) iPivot46.47. If iLeftCur = iRightCur Then Exit Do48.49. 交换值50. iTemp = lngArray(iLeftCur)51. lngArray(iLeftCur) = lngArray(iRightCur)52. lngArray(iRightCur) = iTemp53. Loop54.55. 递归快速排序56. lngArray(iLeftEnd) = lngArray(iRightCur)57. lngArray(iRightCur) = iPivot58.59. InnerQuickSort lngArray, iLeftEnd, iRightCur - 160. InnerQuickSort lngArray, iRightCur + 1, iRightEnd61. End Sub复制代码第五种 (合并排序)Merge sort1. Public Sub MergeSort(ByRef lngArray() As Long)2. Dim arrTemp() As Long3. Dim iSegSize As Long4. Dim iLBound As Long5. Dim iUBound As Long6.7. iLBound = LBound(lngArray)8. iUBound = UBound(lngArray)9.10. ReDim arrTemp(iLBound To iUBound)11.12. iSegSize = 113. Do While iSegSize iUBound - iLBound14.15. 合并A到B16. InnerMergePass lngArray, arrTemp, iLBound, iUBound, iSegSize17. iSegSize = iSegSize + iSegSize18.19. 合并B到A20. InnerMergePass arrTemp, lngArray, iLBound, iUBound, iSegSize21. iSegSize = iSegSize + iSegSize22.23. Loop24. End Sub25.26. Private Sub InnerMergePass(ByRef lngSrc() As Long, ByRef lngDest() As Long, ByVal iLBound As Long, iUBound As Long, ByVal iSegSize As Long)27. Dim iSegNext As Long28.29. iSegNext = iLBound30.31. Do While iSegNext = iUBound - (2 * iSegSize)32. 合并33. InnerMerge lngSrc, lngDest, iSegNext, iSegNext + iSegSize - 1, iSegNext + iSegSize + iSegSize - 134.35. iSegNext = iSegNext + iSegSize + iSegSize36. Loop37.38. If iSegNext + iSegSize = iUBound Then39. InnerMerge lngSrc, lngDest, iSegNext, iSegNext + iSegSize - 1, iUBound40. Else41. For iSegNext = iSegNext To iUBound42. lngDest(iSegNext) = lngSrc(iSegNext)43. Next iSegNext44. End If45.46. End Sub47.48. Private Sub InnerMerge(ByRef lngSrc() As Long, ByRef lngDest() As Long, ByVal iStartFirst As Long, ByVal iEndFirst As Long, ByVal iEndSecond As Long)49. Dim iFirst As Long50. Dim iSecond As Long51. Dim iResult As Long52. Dim iOuter As Long53.54. iFirst = iStartFirst55. iSecond = iEndFirst + 156. iResult = iStartFirst57.58. Do While (iFirst = iEndFirst) And (iSecond = iEndSecond)59.60. If lngSrc(iFirst) iEndFirst Then72. For iOuter = iSecond To iEndSecond73. lngDest(iResult) = lngSrc(iOuter)74. iResult = iResult + 175. Next iOuter76. Else77. For iOuter = iFirst To iEndFirst78. lngDest(iResult) = lngSrc(iOuter)79. iResult = iResult + 180. Next iOuter81. End If82. End Sub复制代码第六种 (堆排序)Heap sort1. Public Sub HeapSort(ByRef lngArray() As Long)2. Dim iLBound As Long3. Dim iUBound As Long4. Dim iArrSize As Long5. Dim iRoot As Long6. Dim iChild As Long7. Dim iElement As Long8. Dim iCurrent As Long9. Dim arrOut() As Long10.11. iLBound = LBound(lngArray)12. iUBound = UBound(lngArray)13. iArrSize = iUBound - iLBound14.15. ReDim arrOut(iLBound To iUBound)16.17. Initialise the heap18. Move up the heap from the bottom19. For iRoot = iArrSize 2 To 0 Step -120.21. iElement = lngArray(iRoot + iLBound)22. iChild = iRoot + iRoot23.24. Move down the heap from the current position25. Do While iChild iArrSize26.27. If iChild iArrSize Then28. If lngArray(iChild + iLBound) = lngArray(iChild + iLBound) Then Exit Do36.37. lngArray(iChild 2) + iLBound) = lngArray(iChild + iLBound)38. iChild = iChild + iChild39. Loop40.41. Move the node42. lngArray(iChild 2) + iLBound) = iElement43. Next iRoot44.45. Read of values one by one (store in array starting at the end)46. For iRoot = iUBound To iLBound Step -147.48. Read the value49. arrOut(iRoot) = lngArray(iLBound)50. Get the last element51. iElement = lngArray(iArrSize + iLBound)52.53. iArrSize = iArrSize - 154. iCurrent = 055. iChild = 156.57. Find a place for the last element to go58. Do While iChild = iArrSize59.60. If iChild iArrSize Then61. If lngArray(iChild + iLBound) = lngArray(iChild + iLBound) Then Exit Do69.70. lngArray(iCurrent + iLBound) = lngArray(iChild + iLBound)71. iCurrent = iChild72. iChild = iChild + iChild73.74. Loop75.76. Move the node77. lngArray(iCurrent + iLBound) = iElement78. Next iRoot79.80. Copy from temp array to real array81. For iRoot = iLBound To iUBound82. lngArray(iRoot) = arrOut(iRoot)83. Next iRoot84. End Sub复制代码第七种 (组合排序)Comb Sort1. Public Sub CombSort(ByRef lngArray() As Long)2. Dim iSpacing As Long3. Dim iOuter As Long4. Dim iInner As Long5. Dim iTemp As Long6. Dim iLBound As Long7. Dim iUBound As Long8. Dim iArrSize As Long9. Dim iFinished As Long10.11. iLBound = LBound(lngArray)12. iUBound = UBound(lngArray)13.14. Initialise comb width15. iSpacing = iUBound - iLBound16.17. Do18. If iSpacing 1 Then19. iSpacing = Int(iSpacing / 1.3)20.21. If iSpacing = 0 Then22. iSpacing = 1 Dont go lower than 123. ElseIf iSpacing 8 And iSpacing lngArray(iInner) Then36. Swap37. iTemp = lngArray(iOuter)38. lngArray(iOuter) = lngArray(iInner)39. lngArray(iInner) = iTemp40.41. Not finished42. iFinished = 043. End If44. Next iOuter45.46. Loop Until iFinished47. End Sub复制代码第八种 (希尔排序)Shell Sort1. Public Sub ShellSort(ByRef lngArray() As Long)2. Dim iSpacing As Long3. Dim iOuter As Long4. Dim iInner As Long5. Dim iTemp As Long6. Dim iLBound As Long7. Dim iUBound As Long8. Dim iArrSize As Long9.10. iLBound = LBound(lngArray)11. iUBound = UBound(lngArray)12.13. Calculate initial sort spacing14. iArrSize = (iUBound - iLBound) + 115. iSpacing = 116.17. If iArrSize 13 Then18. Do While iSpacing iArrSize19. iSpacing = (3 * iSpacing) + 120. Loop21.22. iSpacing = iSpacing 923. End If24.25. Start sorting26. Do While iSpacing27.28. For iOuter = iLBound + iSpacing To iUBound29.30. Get the value to be inserted31. iTemp = lngArray(iOuter)32.33. Move along the already sorted values shifting along34. For iInner = iOuter - iSpacing To iLBound Step -iSpacing35. No more shifting needed, we found the right spot!36. If lngArray(iInner) iMax Then iMax = lngArray(iLoop)19. Next iLoop20.21. Calculate how many sorts are needed22. Do While iMax23. iSorts = iSorts + 124. iMax = iMax 25625. Loop26.27. iMax = 128.29. Do the sorts30. For iLoop = 1 To iSorts31.32. If iLoop And 1 Then33. Odd sort - src to dest34. InnerRadixSort lngArray, arrTemp, iLBound, iUBound, iMax35. Else36. Even sort - dest to src37. InnerRadixSort arrTemp, lngArray, iLBound, iUBound, iMax38. End If39.40. Next sort factor41. iMax = iMax * 25642. Next iLoop43.44. If odd number of sorts we need to swap the arrays45. If (iSorts And 1) Then46. For iLoop = iLBound To iUBound47. lngArray(iLoop) = arrTemp(iLoop)48. Next iLoop49. End If50. End Sub51.52. Private Sub InnerRadixSort(ByRef lngSrc() As Long, ByRef lngDest() As Long, ByVal iLBound As Long, ByVal iUBound As Long, ByVal iDivisor As Long)53. Dim arrCounts(255) As Long54. Dim arrOffsets(255) As Long55. Dim iBucket As Long56. Dim iLoop As Long57.58. Count the items for each bucket59. For iLoop = iLBound To iUBound60. iBucket = (lngSrc(iLoop) iDivisor) And 25561. arrCounts(iBucket) = arrCounts(iBucket) + 162. Next iLoop63.64. Generate offsets65. For iLoop = 1 To 25566. arrOffsets(iLoop) = arrOffsets(iLoop - 1) + arrCounts(iLoop - 1) + iLBound67. Next iLoop68.69. Fill the buckets70. For iLoop = iLBound To iUBound71. iBucket = (lngSrc(iLoop) iDivisor) And 25572. lngDest(arrOffsets(iBucket) = lngSrc(iLoop)73. arrOffsets(iBucket) = arrOffsets(iBucket) + 174. Next iLoop75. End Sub复制代码第十种 Shaker Sort1. Public Sub ShakerSort(ByRef lngArray() As Long)2. Dim iLower As Long3. Dim iUpper As Long4. Dim iInner As Long5. Dim iLBound As Long6. Dim iUBound As Long7. Dim iTemp As Long8. Dim iMax As Long9. Dim iMin As Long10.11. iLBound = LBound(lngArray)12. iUBound = UBound(lngArray)13.14. iLower = iLBound - 115. iUpper = iUBound + 116.17. Do While iLower lngArray(iMax) Then28. iMax = iInner29. ElseIf lngArray(iInner) lngArray(iMin) Then30. iMin = iInner31. End If32. Next iInner33.34. Swap the largest with last slot of the subarray35. iTemp = lngArray(iMax)36. lngArray(iMax) = lngArray(iUpper)37. lngArray(iUpper) = iTemp38.39. Swap the smallest with the first slot of the subarray40. iTemp = lngArray(iMin)41. lngArray(iMin) = lngArray(iLower)42. lngArray(iLower) = iTemp43.44. Loop45. End Sub复制代码
展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 临时分类 > 人文社科


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!