Results 1 to 14 of 14

Thread: Blonde Moment

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Apr 2001
    Location
    Masterton, New Zealand
    Posts
    20

    Question

    Does anyone know of a really quick/good sort routine for arrays ??

    I.E.
    1,4,5,8,6,3
    should be
    1,3,4,5,6,8

    Having a blonde moment and do you think for the life of me I can remember a descent one.

    If anyone could help please.

  2. #2
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,628
    you could do the "bubble sort" method in which you start at the top and move down comparing 1 to 2, 2 to 3, etc. If 2 is less than 1, swap them and start over. if 3 < 2 swap them and start over. This works really well as long as you dont have thousands of array elements.
    When you finally get to the last 2 elements AND THEY DONT HAVE TO BE SWAPPED you are done. else you have to start over.

    should be a piece of cake to perform.

    Its called bubble sort because the smaller numbers "bubble" up to the top.
    My light show youtube page (it's made the news) www.youtube.com/@lightsofelberfeld
    Contact me on the socials www.facebook.com/lordorwell

  3. #3
    TheSarlacc
    Guest

    here is code

    dirs is the array name and numdirs is the number
    of elements in the array

    'sort dirs
    exchange = True
    While exchange
    exchange = False
    For I = 2 To numdirs
    If dirs(I - 1) > dirs(I) Then
    exchange = True
    tmp = dirs(I): dirs(I) = dirs(I - 1): dirs(I - 1) = tmp
    End If
    Next I
    Wend

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Apr 2001
    Location
    Masterton, New Zealand
    Posts
    20
    thanks, it might be a bit slow, is there another way to do it?

  5. #5
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,628
    memory checks to arrays are surprisingly fast.
    Now if you were sorting a listbox, then THAT would be slow.

    How many array elements are you expecting to have?
    My light show youtube page (it's made the news) www.youtube.com/@lightsofelberfeld
    Contact me on the socials www.facebook.com/lordorwell

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Apr 2001
    Location
    Masterton, New Zealand
    Posts
    20
    About 1.3 million

  7. #7
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,628
    ok i'll post a better method tomorrow morning.
    I dont have time right now.
    My light show youtube page (it's made the news) www.youtube.com/@lightsofelberfeld
    Contact me on the socials www.facebook.com/lordorwell

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Apr 2001
    Location
    Masterton, New Zealand
    Posts
    20
    Thanks.

  9. #9
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,628
    ok here is method one designed for large lists.
    it will probably still take forever if you have 1.3 million elements though. I always found that when you generate a new add-in to a list it was better to sort the list as you went along.

    But here goes.
    the following is public domain code typed in from the "Visual Basic 5 from the ground up" reference.

    This method is called Merge Sort.
    It splits the list into smaller lists (by recursively calling itself), sorts them, then recombines them.
    Code:
    sub Mergesort (a$(), Start as Integer, Finish as Integer
      Dim Middle as Integer
      middle = (Start + Finish) \ 2 'integer result
      MergeSort A$(), Start, Middle
      MergeSort A$(), Start, Middle, Finish
      Merge A$(), Start, Middle, Finish
    End if
    End Sub
    
    Sub Merge(A$(), Start As Integer, Middle as Integer, Finish as Integer)
    'local vars:  Temp$(), Begin1, End1, Begin2, End2,  Templocation1
    
       ReDim Temp$(Start to Finish)
       Dim Begin1 as Integer, End1 as integer, Begin2 as integer
       Dim End2 as Integer, TempLocation as integer, I as integer
       Begin1 = Start
       End1 = Middle
       Begin2 = End1 + 1
       End2 = Finish
       Templocation = start
       Do While Begin1 <= End1 and begin2 <= end2
         if a$(begin1) <= a$(begin2) then
           Temp$(templocation) = a$(begin1)
           Templocation = templocation + 1
           begin1 = begin1 + 1
         else 
           temp$(templocation) = a$(begin2)
           templocation = templocation + 1
           begin2 = begin2 + 1
         end if
       loop
       if begin1 <= end1 then
          for i = begin1 to end1
            temp$(templocation) = a$(i)
            templocation = templocation + 1
          next i
       elseif begin2 <= end2 then
          for i = begin2 to end2
            temp$(templocation) = a$(i)
            templocation = templocation + 1
          next i
       for i = start to finish
          a$(i) = temp$(i)
       next i
    end sub
    My light show youtube page (it's made the news) www.youtube.com/@lightsofelberfeld
    Contact me on the socials www.facebook.com/lordorwell

  10. #10
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Here's a way I just came up with.
    It uses the numbers you give as the array indexes in an array.
    Then you loop through the array, and if the values at those indexes contain a value, then print it.
    Therefore we've just sorted the array ...

    Code:
    Option Explicit
    
    Dim dstArray() As Long
    Dim srcArray() As Long
    Dim highestSoFar As Long
    
    Private Sub sort(sourceArray() As Long)
        Dim i As Long
        For i = 0 To UBound(sourceArray)
            If (sourceArray(i) > highestSoFar) Then
                ReDim Preserve dstArray(sourceArray(i))
                highestSoFar = sourceArray(i)
            End If
            dstArray(sourceArray(i)) = 1
        Next i
    End Sub
    
    Private Sub Command1_Click()
        Dim i As Long
        ReDim dstArray(0)
        ReDim srcArray(0)
        
        'fill the array with random numbers
        For i = 0 To 1000000
            ReDim Preserve srcArray(i)
            srcArray(i) = CLng(Rnd * 100000)
        Next i
    
        'Now sort the array of values
        sort srcArray()
        
        'Now dstArray() contains a nummerically sorted version or srcArray()
        For i = 1 To UBound(dstArray)
            If (dstArray(i) = 1) Then
                List1.AddItem i
            End If
        Next i
        
        MsgBox "done"
        
    End Sub
    As far as I can tell it works
    And its very fast too ...

    - jamie
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  11. #11
    billfaceuk
    Guest
    How do I use that code if I'm populating an array like this?

    Code:
    Dim bFile() As String
    Dim gCount As String
    Dim v7 As String, v8 As String, v9 As String, i As Integer
    gCount = 0
    Open "C:\My Documents\Vb5.0\postcodes.txt" For Input As #4
    Do Until EOF(4)
    Input #4, v7, v8, v9
    ReDim Preserve bFile(gCount) As String
    bFile(gCount) = v7 + " - " + v8 + " - " + v9
    Loop
    gCount = gCount + 1
    Close #4

  12. #12
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,628
    what is postcodes generated by?
    and are the values all numeric
    My light show youtube page (it's made the news) www.youtube.com/@lightsofelberfeld
    Contact me on the socials www.facebook.com/lordorwell

  13. #13
    billfaceuk
    Guest
    it's in this format:

    "text","text","number between 1-12"

  14. #14
    rickm
    Guest
    You could use an ADO Recordset that is not bound to a table. Then it will sort them based on text characteristics. It would be a bit slower than this last bit of code however. If you want to know how, let me know.

    Rick

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