1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| Sub QuickSort( mData, nLo as Long, nHi as Long )
Dim nPivot as Long
If nLo < nHi Then
nPivot = QSPartition( mData, nLo, nHi )
If nPivot > 0 Then
QuickSort( mData, nLo, nPivot-1 )
EndIf
QuickSort( mData, nPivot+1, nHi )
EndIf
End Sub
'-----------------------------------------------------------------------------------
Function QSPartition( mData, nLo as Long, nHi as Long ) as Long
Dim nStoreIndex as Long
Dim nPivotIndex as Long
Dim vPivotValue as Variant
Dim mDataCur as Variant
Dim nI as Long
nPivotIndex = (nLo + nHi) \ 2 'Choose pivot in the middle
vPivotValue = mData( nPivotIndex ) 'Save value to match
SwapData( mData, nPivotIndex, nHi ) 'Move pivot to the end
nStoreIndex = nLo 'Store starting with left
For nI = nLo to (nHi-1) 'Examine all but the pivot
If ComparePlace(mData(nI),vPivotValue) < 0 Then 'Current < Saved?
SwapData( mData, nI, nStoreIndex ) 'Swap
nStoreIndex = nStoreIndex + 1 'Increase storeIndex
EndIf
Next
SwapData( mData, nStoreIndex, nHi ) 'Move Saved to StoreIndex
QSPartition = nStoreIndex 'Return StoreIndex as Pivot
End Function
'-----------------------------------------------------------------------------------
Function ComparePlace( a, b )
ComparePlace = ( a.Place - b.Place )
End Function
Sub SwapData( mData, nI as Long, nJ as Long )
Dim vSwapData as Variant
vSwapData = mData(nI)
mData(nI) = mData(nJ)
mData(nJ) = vSwapData
End Sub |
Partager