Highlights
All Snippets
Top 100 Snippets
Librarians
gbCodeLib

By Language
VB6
JavaScript
Perl
HTML
SQL
Java
DOS

GBIC >> Source Code >> Visual Basic >> Snippet

QuickSort


Private Sub Quick_Sort( ByVal lower%, ByVal upper%)
'this is hard-coded for an array called Code()
    If lower >= upper Then Exit Sub
   
    Dim low%, midl%, high%
    Dim midval As String
    Dim tmp As CodeData
   
   low = lower
   high = upper
   
    'Determine a mid value by position only
   midl = (lower + upper) \ 2
   midval = Lcase $(Code(midl).Language & ":::" & Code(midl).Category & ":::" & Code(midl).Title)

    Do While low <= high
        'Find a value higher than the mid value
        '  that is below the midpoint
        Do Until low >= upper
            If Lcase $(Code(low).Language & ":::" & Code(low).Category & ":::" & Code(low).Title) >= midval Then Exit Do
           low = low + 1
        Loop
    
        'Find a value lower than the mid value
        '  that is above the midpoint
        Do Until high <= lower
            If midval >= Lcase $(Code(high).Language & ":::" & Code(high).Category & ":::" & Code(high).Title) Then Exit Do
           high = high - 1
        Loop

        If low <= high Then
            'Note we do not want to increment low and
            '  decrement high if they have collided
            If low < high Then
                'This test means we have found:
                '1. A value higher than midval
                '2. A value lower than midval
                '3. They are out of order
                '(if one or the other has not been
                ' found then low will be >= high)
               tmp = Code(low)
               Code(low) = Code(high)
               Code(high) = tmp
            End If
           low = low + 1
           high = high - 1
            'We continue until high and low collide
        End If
    Loop

    'These are the recursive calls designed to sort
    ' two halves after high and low have collided
    If lower < high Then Quick_Sort lower, high
    If low < upper Then Quick_Sort low, upper
End Sub