




已阅读5页,还剩13页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
在使用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 t
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年广州市劳动合同模板
- 2025年新乡市诚城卓人学校招聘教师若干名模拟试卷及一套答案详解
- 2025年深圳市建筑工程行业员工劳动合同
- 2025广东广州市黄埔区教育局招聘事业编制教职员206人考前自测高频考点模拟试题(含答案详解)
- 2025海南省高校毕业生三支一扶计划招募模拟试卷附答案详解(模拟题)
- 2025湖北襄阳市枣阳市招聘事业单位人员206人模拟试卷及答案详解(夺冠系列)
- 湖南、广西2025-2026学年高三上学期阶段性检测(二)英语 含答案
- 江西心理考试题库及答案
- 【借款协议】过桥资金借款合同8篇
- 篮球主教练考试题及答案
- 【市质检】福州市2024-2025学年高三年级第一次质量检测 地理试卷(含答案)
- 四川蜀道铁路运营管理集团行测笔试题库
- JB T 5496-2015 振动筛制造技术条件
- HGT 4686-2014 液氨泄漏的处理处置方法
- 《答谢中书书》教学设计
- 愚公移山说课稿讲解课件
- 《城市的起源与发展》课件
- 4.CSR社会责任法律法规合规性评价表
- 15D501 建筑物防雷设施安装
- 小学生解决万以内退位减法错误类型及影响研究
- 水利工程中挡土墙的选型与稳定性验算
评论
0/150
提交评论