在使用VBA进行写程序时,经常会做排序,下面将会给出一些常用的排序算法的实现,方便大家写程序参考,若代码中出现了错误,欢迎高手指正。主要算法有:1、(冒泡排序)Bubblesort2、(选择排序)Selectionsort3、(插入排序)Insertionsort4、(快速排序)Quicksort5、(合并排序)Mergesort6、(堆排序)Heapsort7、(组合排序)CombSort8、(希尔排序)ShellSort9、(基数排序)RadixSort10、ShakerSort第一种(冒泡排序)BubblesortPublicSubBubbleSort(ByReflngArray()AsLong)DimiOuterAsLongDimiInnerAsLongDimiLBoundAsLongDimiUBoundAsLongDimiTempAsLongiLBound=LBound(lngArray)iUBound=UBound(lngArray)'冒泡排序ForiOuter=iLBoundToiUBound-1ForiInner=iLBoundToiUBound-iOuter-1'比较相邻项IflngArray(iInner)lngArray(iInner+1)Then'交换值iTemp=lngArray(iInner)lngArray(iInner)=lngArray(iInner+1)lngArray(iInner+1)=iTempEndIfNextiInnerNextiOuterEndSub2、(选择排序)Selectionsort1.PublicSubSelectionSort(ByReflngArray()AsLong)2.DimiOuterAsLong3.DimiInnerAsLong4.DimiLBoundAsLong5.DimiUBoundAsLong6.DimiTempAsLong7.DimiMaxAsLong8.9.iLBound=LBound(lngArray)10.iUBound=UBound(lngArray)11.12.'选择排序13.ForiOuter=iUBoundToiLBound+1Step-114.15.iMax=016.17.'得到最大值得索引18.ForiInner=iLBoundToiOuter19.IflngArray(iInner)lngArray(iMax)TheniMax=iInner20.NextiInner21.22.'值交换23.iTemp=lngArray(iMax)24.lngArray(iMax)=lngArray(iOuter)25.lngArray(iOuter)=iTemp26.27.NextiOuter28.EndSub复制代码第三种(插入排序)Insertionsort1.PublicSubInsertionSort(ByReflngArray()AsLong)2.DimiOuterAsLong3.DimiInnerAsLong4.DimiLBoundAsLong5.DimiUBoundAsLong6.DimiTempAsLong7.8.iLBound=LBound(lngArray)9.iUBound=UBound(lngArray)10.11.ForiOuter=iLBound+1ToiUBound12.13.'取得插入值14.iTemp=lngArray(iOuter)15.16.'移动已经排序的值17.ForiInner=iOuter-1ToiLBoundStep-118.IflngArray(iInner)=iTempThenExitFor19.lngArray(iInner+1)=lngArray(iInner)20.NextiInner21.22.'插入值23.lngArray(iInner+1)=iTemp24.NextiOuter25.EndSub复制代码第四种(快速排序)Quicksort1.PublicSubQuickSort(ByReflngArray()AsLong)2.DimiLBoundAsLong3.DimiUBoundAsLong4.DimiTempAsLong5.DimiOuterAsLong6.DimiMaxAsLong7.8.iLBound=LBound(lngArray)9.iUBound=UBound(lngArray)10.11.'若只有一个值,不排序12.If(iUBound-iLBound)Then13.ForiOuter=iLBoundToiUBound14.IflngArray(iOuter)lngArray(iMax)TheniMax=iOuter15.NextiOuter16.17.iTemp=lngArray(iMax)18.lngArray(iMax)=lngArray(iUBound)19.lngArray(iUBound)=iTemp20.21.'开始快速排序22.InnerQuickSortlngArray,iLBound,iUBound23.EndIf24.EndSub25.26.PrivateSubInnerQuickSort(ByReflngArray()AsLong,ByValiLeftEndAsLong,ByValiRightEndAsLong)27.DimiLeftCurAsLong28.DimiRightCurAsLong29.DimiPivotAsLong30.DimiTempAsLong31.32.IfiLeftEnd=iRightEndThenExitSub33.34.iLeftCur=iLeftEnd35.iRightCur=iRightEnd+136.iPivot=lngArray(iLeftEnd)37.38.Do39.Do40.iLeftCur=iLeftCur+141.LoopWhilelngArray(iLeftCur)iPivot42.43.Do44.iRightCur=iRightCur-145.LoopWhilelngArray(iRightCur)iPivot46.47.IfiLeftCur=iRightCurThenExitDo48.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.InnerQuickSortlngArray,iLeftEnd,iRightCur-160.InnerQuickSortlngArray,iRightCur+1,iRightEnd61.EndSub复制代码第五种(合并排序)Mergesort1.PublicSubMergeSort(ByReflngArray()AsLong)2.DimarrTemp()AsLong3.DimiSegSizeAsLong4.DimiLBoundAsLong5.DimiUBoundAsLong6.7.iLBound=LBound(lngArray)8.iUBound=UBound(lngArray)9.10.ReDimarrTemp(iLBoundToiUBound)11.12.iSegSize=113.DoWhileiSegSizeiUBound-iLBound14.15.'合并A到B16.InnerMergePasslngArray,arrTemp,iLBound,iUBound,iSegSize17.iSegSize=iSegSize+iSegSize18.19.'合并B到A20.InnerMergePassarrTemp,lngArray,iLBound,iUBound,iSegSize21.iSegSize=iSegSize+iSegSize22.23.Loop24.EndSub25.26.PrivateSubInnerMergePass(ByReflngSrc()AsLong,ByReflngDest()AsLong,ByValiLBoundAsLong,iUBoundAsLong,ByValiSegSizeAsLong)27.DimiSegNextAsLong28.29.iSegNext=iLBound30.31.DoWhileiSegNext=iUBound-(2*iSegSize)32.'合并33.InnerMergelngSrc,lngDest,iSegNext,iSegNext+iSegSize-1,iSegNext+iSegSize+iSegSize-134.35.iSegNext=iSegNext+iSegSize+iSegSize36.Loop37.38.IfiSegNext+iSegSize=iUBoundThen39.InnerMergelngSrc,lngDest,iSegNext,iSegNext+iSegSize-1,iUBound40.Else41.ForiSegNext=iSegNextToiUBound42.lngDest(iSegNext)=lngSrc(iSegNext)43.NextiSegNext44.EndIf45.46.EndSub47.48.PrivateSubInnerMerge(ByReflngSrc()AsLong,ByReflngDest()AsLong,ByValiStartFirstAsLong,ByValiEndFirstAsLong,ByValiEndSecondAsLong)49.DimiFirstAsLong50.DimiSecondAsLong51.DimiResultAsLong52.DimiOuterAsLong53.54.iFirst=iStartFirst55.iSecond=iEndFirst+156.iResult=iStartFirst57.58.DoWhile(iFirst=iEndFirst)And(iSecond=iEndSecond)59.60.IflngSrc(iFirst)=lngSrc(iSecond)Then61.lngDest(iResult)=lngSrc(iFirst)62.iFirst=iFirst+163.Else64.lngDest(iResult)=lngSrc(iSecond)65.iSecond=iSecond+166.EndIf67.68.iResult=iResult+169.Loop70.71.IfiFirstiEndFirstThen72.ForiOuter=iSecondToiEndSecond73.lngDest(iResult)=lngSrc(iOuter)74.iResult=iResult+175.NextiOuter76.Else77.ForiOuter=iFirstToiEndFirst78.lngDest(iResult)=lngSrc(iOuter)79.iResult=iResult+180.NextiOuter81.EndIf82.EndSub复制代码第六种(堆排序)Heapsort1.PublicSubHeapSort(ByReflngArray()AsLong)2.DimiLBoundAsLong3.DimiUBoundAsLong4.DimiArrSizeAsLong5.DimiRootAsLong6.DimiChildAsLong7.DimiElementAsLong8.DimiCurrentAsLong9.DimarrOut()AsLong10.11.iLBound=LBound(lngArray)12.iUBound=UBound(