home *** CD-ROM | disk | FTP | other *** search
-
- Public Sub QuickSort(lonLower, lonUpper)
-
- Dim lonRandomPivot As Long
- Dim lonTempLower As Long
- Dim lonTempUpper As Long
- Dim strLastItem As String
- Dim strTempItem As String
-
- Randomize Timer
-
- ' Only sort if the lower boundary is lesser
- ' than the upper boundary.
- If lonLower < lonUpper Then
- If lonUpper - lonLower = 1 Then
- ' Switch the upper and lower array items
- ' if the lower is greater than the upper.
- If strArray(lonLower) > strArray(lonUpper) Then
- strTempItem = strArray(lonUpper)
- strArray(lonUpper) = strArray(lonLower)
- strArray(lonLower) = strTempItem
- End If
- Else
- ' Pick a random "pivot" item.
- lonRandomPivot = Int(Rnd _
- * (lonUpper - lonLower + 1)) + lonLower
- ' Switch the upper array item with the
- ' pivot item.
- strTempItem = strArray(lonUpper)
- strArray(lonUpper) = strArray(lonRandomPivot)
- strArray(lonRandomPivot) = strTempItem
- ' Store the upper array item.
- strLastItem = strArray(lonUpper)
- Do
- ' Define the temporary upper and
- ' lower boundaries.
- lonTempUpper = lonUpper
- lonTempLower = lonLower
- ' Move down towards the pivot item,
- ' looping until the pivot item is greater
- ' than or equal to the temporary
- ' lower boundary.
- Do While (lonTempLower < lonTempUpper) And _
- (strArray(lonTempLower) <= strLastItem)
- lonTempLower = lonTempLower + 1
- Loop
- ' Move up towards the pivot item, looping
- ' until the pivot item is less than
- ' or equal to the temporary upper
- ' boundary.
- Do While (lonTempUpper > lonTempLower) And _
- (strArray(lonTempUpper) >= strLastItem)
- lonTempUpper = lonTempUpper - 1
- Loop
- ' If the pivot item hasn't been
- ' reached, then two of the items on
- ' either side of the pivot are out
- ' of order. If so, then switch them.
- If lonTempLower < lonTempUpper Then
- strTempItem = strArray(lonTempUpper)
- strArray(lonTempUpper) = strArray(lonTempLower)
- strArray(lonTempLower) = strTempItem
- End If
- Loop While (lonTempLower < lonTempUpper)
- ' Switch the temporary lower boundary
- ' item with the original upper boundary
- ' item.
- strTempItem = strArray(lonTempLower)
- strArray(lonTempLower) = strArray(lonUpper)
- strArray(lonUpper) = strTempItem
- ' Call the QuickSort routine again
- ' recursively, using new upper and
- ' lower boundaries.
- If (lonTempLower - lonLower) < (lonUpper - lonTempLower) Then
- QuickSort lonLower, lonTempLower - 1
- QuickSort lonTempLower + 1, lonUpper
- Else
- QuickSort lonTempLower + 1, lonUpper
- QuickSort lonLower, lonTempLower - 1
- End If
- End If
- End If
-
- End Sub
-