Last active
January 29, 2023 06:44
-
-
Save tmplinshi/42691e3c17a8968cae239849707e7cdc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
' https://stackoverflow.com/a/152333 | |
' Example: QuickSort arr, LBound(arr), UBound(arr) | |
Private Sub QuickSort(ByRef Field() As String, ByVal LB As Long, ByVal UB As Long) | |
Dim P1 As Long, P2 As Long, Ref As String, TEMP As String | |
P1 = LB | |
P2 = UB | |
Ref = Field((P1 + P2) / 2) | |
Do | |
Do While (Field(P1) < Ref) | |
P1 = P1 + 1 | |
Loop | |
Do While (Field(P2) > Ref) | |
P2 = P2 - 1 | |
Loop | |
If P1 <= P2 Then | |
TEMP = Field(P1) | |
Field(P1) = Field(P2) | |
Field(P2) = TEMP | |
P1 = P1 + 1 | |
P2 = P2 - 1 | |
End If | |
Loop Until (P1 > P2) | |
If LB < P2 Then Call QuickSort(Field, LB, P2) | |
If P1 < UB Then Call QuickSort(Field, P1, UB) | |
End Sub |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sub QuickSortDes(ByRef av As Variant, ByVal iBeg As Long, ByVal iEnd As Long) | |
' from https://www.mrexcel.com/board/threads/do-a-quicksort-in-descending-order.550009/#post-2717838 | |
Dim iLo As Long | |
Dim iHi As Long | |
Dim Temp As Variant | |
Dim vSep As Variant | |
iLo = iBeg | |
iHi = iEnd | |
vSep = av((iBeg + iEnd) / 2) | |
Do | |
Do While av(iLo) > vSep ' descending | |
iLo = iLo + 1 | |
Loop | |
Do While av(iHi) < vSep ' descending | |
iHi = iHi - 1 | |
Loop | |
If iLo <= iHi Then | |
Temp = av(iLo) | |
av(iLo) = av(iHi) | |
av(iHi) = Temp | |
iLo = iLo + 1 | |
iHi = iHi - 1 | |
End If | |
Loop While iLo <= iHi | |
If iBeg < iHi Then QuickSortDes av, iBeg, iHi | |
If iLo < iEnd Then QuickSortDes av, iLo, iEnd | |
End Sub |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment