|
-
Nov 16th, 2014, 09:13 PM
#4
Frenzied Member
Re: VB6 Dual-Pivot-QuickSort
 Originally Posted by georgekar
I change the use of tmp so If we want we can change it to string without need for another var.
Code:
Public Sub NaiveQuickSortExtended(Arr() As Long, ByVal LB As Long, ByVal UB As Long)
Dim M1 As Long, M2 As Long
Dim Piv As Long, Tmp As Long '<- adjust types here, when switching to something different than Long
If UB - LB = 1 Then
M1 = LB: GoTo there4
Else
M1 = (LB + UB) \ 2
If Arr(M1) = Arr(LB) Then
M2 = UB
M1 = LB
Do
M1 = M1 + 1
If M1 > M2 Then GoTo there3
Loop Until Arr(M1) <> Arr(LB)
End If
End If
Piv = Arr(M1)
M1 = LB
M2 = UB
'create the Pivot-Element
Do
Do While (Arr(M1) < Piv): M1 = M1 + 1: Loop
Do While (Arr(M2) > Piv): M2 = M2 - 1: Loop
If M1 <= M2 Then
Tmp = Arr(M1): Arr(M1) = Arr(M2): Arr(M2) = Tmp 'swap
M1 = M1 + 1
M2 = M2 - 1
End If
Loop Until M1 > M2
If LB < M2 Then NaiveQuickSortExtended Arr, LB, M2
If M1 < UB Then NaiveQuickSortExtended Arr, M1, UB
Exit Sub
there3:
For M1 = LB + 1 To UB - 1
If Arr(M1) <> Arr(LB) Then Exit For
Next M1
If M1 < UB - 1 Then
NaiveQuickSortExtended Arr(), M1, UB
ElseIf M1 < UB Then
there4:
If Arr(M1) > Arr(UB) Then Tmp = Arr(M1): Arr(M1) = Arr(UB): Arr(UB) = Tmp
Else
If Arr(UB) < Arr(LB) Then Tmp = Arr(LB): Arr(LB) = Arr(UB): Arr(UB) = Tmp
End If
End Sub
where is the sort direction? Please make sorting routine be ready to use for beginner.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|