Results 1 to 10 of 10

Thread: Sorting an array

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2001
    Location
    The Dark Side of the Moon
    Posts
    448

    Sorting an array

    Does anyone have any optimized code for array sorting?

  2. #2
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Numerical or string ?
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  3. #3
    Lively Member Ali G's Avatar
    Join Date
    Nov 2001
    Location
    Staines
    Posts
    120
    i don't knows abou' optimizd, but dis code from me main man Aarn Young is wicked, respeck
    VB Code:
    1. Public Sub Sort(ByRef aArray As Variant)
    2.     Dim lCount As Long, lCount2 As Long
    3.     Dim vTemp As Variant
    4.     Dim vItem1 As Variant, vItem2 As Variant
    5.    
    6.     For lCount = LBound(aArray) To UBound(aArray) - (1 - LBound(aArray))
    7.         For lCount2 = LBound(aArray) To UBound(aArray) - (1 - LBound(aArray)) - lCount
    8.             vItem1 = aArray(lCount2 + 1)
    9.             vItem2 = aArray(lCount2)
    10.             If vItem1 < vItem2 Then
    11.                 vTemp = aArray(lCount2)
    12.                 aArray(lCount2) = aArray(lCount2 + 1)
    13.                 aArray(lCount2 + 1) = vTemp
    14.             End If
    15.         Next
    16.     Next
    17. End Sub

  4. #4
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Pfft.
    This ownz. I wrote it.

    VB Code:
    1. Option Explicit
    2.  
    3. Private Function sortInts(arrInt() As Long) As Long()
    4.     Dim i As Long, tempN As Long, allOk As Boolean
    5.     Do
    6.         allOk = True
    7.         For i = 0 To UBound(arrInt) - 1
    8.             If arrInt(i) > arrInt(i + 1) Then
    9.                 allOk = False
    10.                 tempN = arrInt(i)
    11.                 arrInt(i) = arrInt(i + 1)
    12.                 arrInt(i + 1) = tempN
    13.             End If
    14.         Next
    15.         If allOk Then Exit Do
    16.     Loop
    17.     sortInts = arrInt
    18. End Function
    19.  
    20. Private Sub Form_Load()
    21.     Dim x() As Long, i As Long
    22.     ReDim x(10)
    23.     For i = 0 To 10
    24.         x(i) = CLng(Rnd * 1000)
    25.         Debug.Print x(i)
    26.     Next
    27.     x = sortInts(x)
    28.    
    29.     For i = 0 To 10
    30.         Debug.Print x(i)
    31.     Next
    32. End Sub
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  5. #5
    jim mcnamara
    Guest
    What these guys showed is great for smaller lists, There a bunch of other sorting algorithms, and each one is 'optimized' for a particular task.

    In general a quicksort is the fastest easy-to-code algorithm.

    However, MS has built-in fast sorting algorithms for use in controls. ListBox.Sorted = True makes a listbox sort any items added to it. All you have to do is to add the items, then read them back. No sorting. You have to lpad numeric value. There are limits to the size of the array when you do this - 32767 items.
    Code:
    For i=0 to ubound(myArr)
            List1.AddItem myArr(i)
    Next
    For i = 0 to List1.ListCount - 1
           myArr(i) = list1.list(i)
    next
    If you work with numeric values, use lpad

    Code:
    Function lpad(i as integer, iLen as integer) as string
               Dim tmp as string
               tmp = cstr(i)
               lpad = right(Space(iLen) & tmp,iLen)
    End function
    'sorting numbers 
    For i=0 to ubound(myArr)
            List1.AddItem lpad(myArr(i),10)
    Next
    For i = 0 to List1.ListCount - 1
           myArr(i) = Cint(list1.list(i))
    next

  6. #6
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    My algorithm is good with huge lists too I might mention.
    But anyway if you want to use the listbox approach, hide it, then add the items, then unhide it.
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  7. #7
    jim mcnamara
    Guest
    Jamie - your algorithm is a bubblesort. The number of operations goes up exponentially as a function of list length. If the list is already in reasonable shape, this algorithm is perfect. It's the best choice. However. If the order is completely random the number of operations is:

    operations = (n-1) ^ 2

    For a quicksort on the same random array:

    operations = log(n-1) ^ 2

    The number of operations (time consumed) goes up a lot slower as you add more & more items to a quicksort as compared to a bubblesort.

  8. #8
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Correction, if the list is completely random, statistically speaking its (n - 1) ^ 2 operations
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  9. #9
    jim mcnamara
    Guest
    Hey! good - you've been reading your CS texts. Keep us all in line out here.

  10. #10
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Nah not reading anything. Just common sense...
    Anyway I didnt know the algorithm I gave had a name. It just came to me while I was sitting here doing nothing...
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

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