Hello,
I got below quicksort code from one of the online forums. What i observed was entire sort is fine except the middle (pivot) value. It did an extra swap which spoiled the output. Can someone please fix this code for me?
I actually need to sort a string array which includes numbers as well. Can someone please make below code suitable for string comparison as well?
Sort output
30422931250
30422936060
30439815220
767329405210
767355886950
767351008940
767428295340
767456457710
767434830870
Private Sub QuickSort(ByVal 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
Thanks in advance
Bookmarks