1、VBA排序的十种算法在使用VBA进行写程序时,经常会做排序,下面将会给出一些常用的排序算法的实现,方便大家写程序参考,若代码中出现了错误,欢迎高手指正。主要算法有:1、(冒泡排序)Bubblesort2、(选择排序)Selectionsort3、(插入排序)Insertionsort4、(快速排序)Quicksort5、(合并排序)Mergesort6、(堆排序)Heapsort7、(组合排序)CombSort8、(希尔排序)ShellSort9、(基数排序)RadixSort10、ShakerSort第一种(冒泡排序)BubblesortPublicSubBubbleSort(ByRefln
2、gArray()AsLong)DimiOuterAsLongDimiInnerAsLongDimiLBoundAsLongDimiUBoundAsLongDimiTempAsLongiLBound=LBound(lngArray)iUBound=UBound(lngArray)冒泡排序ForiOuter=iLBoundToiUBound-1ForiInner=iLBoundToiUBound-iOuter-1比较相邻项IflngArray(iInner)lngArray(iInner+1)Then交换值iTemp=lngArray(iInner)lngArray(iInner)=lngArra
3、y(iInner+1)lngArray(iInner+1)=iTempEndIfNextiInnerNextiOuterEndSub2、(选择排序)Selectionsort1. PublicSubSelectionSort(ByReflngArray()AsLong)2. DimiOuterAsLong3. DimiInnerAsLong4. DimiLBoundAsLong5. DimiUBoundAsLong6. DimiTempAsLong7. DimiMaxAsLong8. iLBound=LBound(lngArray)9. iUBound=UBound(lngArray)10.
4、选择排序11. ForiOuter=iUBoundToiLBound+1Step-112. iMax=013. 得到最大值得索引14. ForiInner=iLBoundToiOuter15. IflngArray(iInner)lngArray(iMax)TheniMax=iInner16. NextiInner17. 值交换18. iTemp=lngArray(iMax)19. lngArray(iMax)=lngArray(iOuter)20. lngArray(iOuter)=iTemp21. NextiOuter22. EndSub复制代码第三种(插入排序)Insertionsort
5、1. PublicSubInsertionSort(ByReflngArray()AsLong)2. DimiOuterAsLong3. DimiInnerAsLong4. DimiLBoundAsLong5. DimiUBoundAsLong6. DimiTempAsLong7. iLBound=LBound(lngArray)8. iUBound=UBound(lngArray)9. ForiOuter=iLBound+1ToiUBound10. 取得插入值11. iTemp=lngArray(iOuter)12. 移动已经排序的值13. ForiInner=iOuter-1ToiLBou
6、ndStep-114. IflngArray(iInner)lngArray(iMax)TheniMax=iOuter13. NextiOuter14. iTemp=lngArray(iMax)15. lngArray(iMax)=lngArray(iUBound)16. lngArray(iUBound)=iTemp17. 开始快速排序18. InnerQuickSortlngArray,iLBound,iUBound19. EndIf20. EndSub21. PrivateSubInnerQuickSort(ByReflngArray()AsLong,ByValiLeftEndAsLon
7、g,ByValiRightEndAsLong)22. DimiLeftCurAsLong23. DimiRightCurAsLong24. DimiPivotAsLong25. DimiTempAsLong26. IfiLeftEnd=iRightEndThenExitSub27. iLeftCur=iLeftEnd28. iRightCur=iRightEnd+129. iPivot=lngArray(iLeftEnd)30. Do31. Do32. iLeftCur=iLeftCur+133. LoopWhilelngArray(iLeftCur)iPivot37. IfiLeftCur=
8、iRightCurThenExitDo38. 交换值39. iTemp=lngArray(iLeftCur)40. lngArray(iLeftCur)=lngArray(iRightCur)41. lngArray(iRightCur)=iTemp42. Loop43. 递归快速排序44. lngArray(iLeftEnd)=lngArray(iRightCur)45. lngArray(iRightCur)=iPivot46. InnerQuickSortlngArray,iLeftEnd,iRightCur-147. InnerQuickSortlngArray,iRightCur+1
9、,iRightEnd48. EndSub复制代码第五种(合并排序)Mergesort1. PublicSubMergeSort(ByReflngArray()AsLong)2. DimarrTemp()AsLong3. DimiSegSizeAsLong4. DimiLBoundAsLong5. DimiUBoundAsLong6. iLBound=LBound(lngArray)7. iUBound=UBound(lngArray)8. ReDimarrTemp(iLBoundToiUBound)9. iSegSize=110. DoWhileiSegSizeiUBound-iLBound1
10、1. 合并A到B12. InnerMergePasslngArray,arrTemp,iLBound,iUBound,iSegSize13. iSegSize=iSegSize+iSegSize14. 合并B到A15. InnerMergePassarrTemp,lngArray,iLBound,iUBound,iSegSize16. iSegSize=iSegSize+iSegSize17. Loop18. EndSub19. PrivateSubInnerMergePass(ByReflngSrc()AsLong,ByReflngDest()AsLong,ByValiLBoundAsLon
11、g,iUBoundAsLong,ByValiSegSizeAsLong)20. DimiSegNextAsLong21. iSegNext=iLBound22. DoWhileiSegNext=iUBound-(2*iSegSize)23. 合并24. InnerMergelngSrc,lngDest,iSegNext,iSegNext+iSegSize-1,iSegNext+iSegSize+iSegSize-125. iSegNext=iSegNext+iSegSize+iSegSize26. Loop27. IfiSegNext+iSegSize=iUBoundThen28. Inner
12、MergelngSrc,lngDest,iSegNext,iSegNext+iSegSize-1,iUBound29. Else30. ForiSegNext=iSegNextToiUBound31. lngDest(iSegNext)=lngSrc(iSegNext)32. NextiSegNext33. EndIf34. EndSub35. PrivateSubInnerMerge(ByReflngSrc()AsLong,ByReflngDest()AsLong,ByValiStartFirstAsLong,ByValiEndFirstAsLong,ByValiEndSecondAsLon
13、g)36. DimiFirstAsLong37. DimiSecondAsLong38. DimiResultAsLong39. DimiOuterAsLong40. iFirst=iStartFirst41. iSecond=iEndFirst+142. iResult=iStartFirst43. DoWhile(iFirst=iEndFirst)And(iSecond=iEndSecond)44. IflngSrc(iFirst)iEndFirstThen54. ForiOuter=iSecondToiEndSecond55. lngDest(iResult)=lngSrc(iOuter
14、)56. iResult=iResult+157. NextiOuter58. Else59. ForiOuter=iFirstToiEndFirst60. lngDest(iResult)=lngSrc(iOuter)61. iResult=iResult+162. NextiOuter63. EndIf64. EndSub复制代码第六种(堆排序)Heapsort1. PublicSubHeapSort(ByReflngArray()AsLong)2. DimiLBoundAsLong3. DimiUBoundAsLong4. DimiArrSizeAsLong5. DimiRootAsLo
15、ng6. DimiChildAsLong7. DimiElementAsLong8. DimiCurrentAsLong9. DimarrOut()AsLong10. iLBound=LBound(lngArray)11. iUBound=UBound(lngArray)12. iArrSize=iUBound-iLBound13. ReDimarrOut(iLBoundToiUBound)14. Initialisetheheap15. Moveuptheheapfromthebottom16. ForiRoot=iArrSize2To0Step-117. iElement=lngArray
16、(iRoot+iLBound)18. iChild=iRoot+iRoot19. Movedowntheheapfromthecurrentposition20. DoWhileiChildiArrSize21. IfiChildiArrSizeThen22. IflngArray(iChild+iLBound)=lngArray(iChild+iLBound)ThenExitDo29. lngArray(iChild2)+iLBound)=lngArray(iChild+iLBound)30. iChild=iChild+iChild31. Loop32. Movethenode33. ln
17、gArray(iChild2)+iLBound)=iElement34. NextiRoot35. Readofvaluesonebyone(storeinarraystartingattheend)36. ForiRoot=iUBoundToiLBoundStep-137. Readthevalue38. arrOut(iRoot)=lngArray(iLBound)39. Getthelastelement40. iElement=lngArray(iArrSize+iLBound)41. iArrSize=iArrSize-142. iCurrent=043. iChild=144. F
18、indaplaceforthelastelementtogo45. DoWhileiChild=iArrSize46. IfiChildiArrSizeThen47. IflngArray(iChild+iLBound)=lngArray(iChild+iLBound)ThenExitDo54. lngArray(iCurrent+iLBound)=lngArray(iChild+iLBound)55. iCurrent=iChild56. iChild=iChild+iChild57. Loop58. Movethenode59. lngArray(iCurrent+iLBound)=iEl
19、ement60. NextiRoot61. Copyfromtemparraytorealarray62. ForiRoot=iLBoundToiUBound63. lngArray(iRoot)=arrOut(iRoot)64. NextiRoot65. EndSub复制代码第七种(组合排序)CombSort1. PublicSubCombSort(ByReflngArray()AsLong)2. DimiSpacingAsLong3. DimiOuterAsLong4. DimiInnerAsLong5. DimiTempAsLong6. DimiLBoundAsLong7. DimiUB
20、oundAsLong8. DimiArrSizeAsLong9. DimiFinishedAsLong10. iLBound=LBound(lngArray)11. iUBound=UBound(lngArray)12. Initialisecombwidth13. iSpacing=iUBound-iLBound14. Do15. IfiSpacing1Then16. iSpacing=Int(iSpacing/1.3)17. IfiSpacing=0Then18. iSpacing=1Dontgolowerthan119. ElseIfiSpacing8AndiSpacinglngArra
21、y(iInner)Then29. Swap30. iTemp=lngArray(iOuter)31. lngArray(iOuter)=lngArray(iInner)32. lngArray(iInner)=iTemp33. Notfinished34. iFinished=035. EndIf36. NextiOuter37. LoopUntiliFinished38. EndSub复制代码第八种(希尔排序)ShellSort1. PublicSubShellSort(ByReflngArray()AsLong)2. DimiSpacingAsLong3. DimiOuterAsLong4
22、. DimiInnerAsLong5. DimiTempAsLong6. DimiLBoundAsLong7. DimiUBoundAsLong8. DimiArrSizeAsLong9. iLBound=LBound(lngArray)10. iUBound=UBound(lngArray)11. Calculateinitialsortspacing12. iArrSize=(iUBound-iLBound)+113. iSpacing=114. IfiArrSize13Then15. DoWhileiSpacingiArrSize16. iSpacing=(3*iSpacing)+117
23、. Loop18. iSpacing=iSpacing919. EndIf20. Startsorting21. DoWhileiSpacing22. ForiOuter=iLBound+iSpacingToiUBound23. Getthevaluetobeinserted24. iTemp=lngArray(iOuter)25. Movealongthealreadysortedvaluesshiftingalong26. ForiInner=iOuter-iSpacingToiLBoundStep-iSpacing27. Nomoreshiftingneeded,wefoundtheri
24、ghtspot!28. IflngArray(iInner)iMaxTheniMax=lngArray(iLoop)16. NextiLoop17. Calculatehowmanysortsareneeded18. DoWhileiMax19. iSorts=iSorts+120. iMax=iMax25621. Loop22. iMax=123. Dothesorts24. ForiLoop=1ToiSorts25. IfiLoopAnd1Then26. Oddsort-srctodest27. InnerRadixSortlngArray,arrTemp,iLBound,iUBound,iMax28. Else29. Evensort-desttosrc30. InnerRadixSortarrTemp,lngArray,iLBound,iUBound,iMax31. EndIf32. Nextsortfactor33. iMax=iMax*25634. NextiLoop35. Ifoddnumbero
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1