Highlights
All Snippets
Top 100 Snippets
Librarians
gbCodeLib

By Language
VB6
JavaScript
Perl
HTML
SQL
Java
DOS

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

Bubble sort (improved)


'faster verson of bubble sort - maybe 1/3 faster
'sort Array called 'Info'
'make temp variable same type as Info
'Remove the LCase$ if you don't want to ignore case
'reverse the > If you want a descending sort

Dim Exchange As Boolean
Dim i As Long , temp
Do
   Exchange = False

    For i = Ubound (Info) To Lbound (Info) + 1 Step - 1
        If Lcase $(Info(i - 1)) > Lcase $(Info(i)) Then
            'swap places
           temp = Info(i - 1)
           Info(i - 1) = Info(i)
           Info(i) = temp
        End If
    Next

    For i = Lbound (Info) + 1 To Ubound (Info)
        If Lcase $(Info(i - 1)) > Lcase $(Info(i)) Then
            'swap places
           temp = Info(i - 1)
           Info(i - 1) = Info(i)
           Info(i) = temp
        End If
    Next

Loop While Exchange