Results 1 to 3 of 3

Thread: This sad sort function

  1. #1

    Thread Starter
    Fanatic Member coox's Avatar
    Join Date
    Oct 1999
    Posts
    550

    Question

    Could one of you geniuses have a look at this sort function, maybe tell me why it's so slow and where I could speed things up? Cheers awfully...

    Sub Sorter()
    Dim Swap As Boolean, Swapped As Boolean, ErrCode As Boolean
    Dim Loop1 As Integer, Loop2 As Integer
    Dim Temp As String
    '
    Do 'Keep running through all items till no more swaps are made
    Swapped = False
    For Loop1 = 1 To ItemCount 'The Total number of items
    Swap = False
    For Loop2 = 1 To Trim(Len(Items(Loop1 - 1))) 'The num of chars in first item
    On Error GoTo ErrorHandler 'In case second item is shorter than first
    If Asc(UCase(Mid(Items(Loop1 - 1), Loop2, 1))) > _
    Asc(UCase(Mid(Items(Loop1), Loop2, 1))) Then
    If ErrCode = True Then Exit For
    Swap = True
    Exit For
    ElseIf Asc(UCase(Mid(Items(Loop1 - 1), Loop2, 1))) < _
    Asc(UCase(Mid(Items(Loop1), Loop2, 1))) Then
    Exit For
    End If
    Next Loop2
    If Swap = True Then 'Do the swap
    Temp = Items(Loop1 - 1)
    Items(Loop1 - 1) = Items(Loop1)
    Items(Loop1) = Temp
    Swapped = True
    End If
    Next Loop1
    Loop While Swapped = True
    '
    Exit Sub
    ErrorHandler:
    ErrCode = True
    Resume Next
    End Sub

  2. #2
    Hyperactive Member
    Join Date
    Mar 2000
    Posts
    292
    Someone correct me if I'm wrong but it looks like your using a bubble sort. Search on the net for 'quick sort' (sorry the code I have is in Java not VB) it should be much faster.
    "People who think they know everything are a great annoyance to those of us who do."

  3. #3
    Guest
    strangely enough i have a VB book with the quicksort included, apparently you can download the code from

    http://www.sams.com/product_support

    check it out

    other info you may be interested to know:

    the quicksort was first written in 1962 and is generally considered the fasted text / numbers sorting algorithm ever devised.

    Maybe i should get out more!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width