ImageVerifierCode 换一换
格式:DOCX , 页数:17 ,大小:18.47KB ,
资源ID:3618653      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/3618653.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(VBA排序的十种算法.docx)为本站会员(b****5)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

VBA排序的十种算法.docx

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