Does anyone have any optimized code for array sorting?
Printable View
Does anyone have any optimized code for array sorting?
Numerical or string ?
i don't knows abou' optimizd, but dis code from me main man Aarn Young is wicked, respeck
VB Code:
Public Sub Sort(ByRef aArray As Variant) Dim lCount As Long, lCount2 As Long Dim vTemp As Variant Dim vItem1 As Variant, vItem2 As Variant For lCount = LBound(aArray) To UBound(aArray) - (1 - LBound(aArray)) For lCount2 = LBound(aArray) To UBound(aArray) - (1 - LBound(aArray)) - lCount vItem1 = aArray(lCount2 + 1) vItem2 = aArray(lCount2) If vItem1 < vItem2 Then vTemp = aArray(lCount2) aArray(lCount2) = aArray(lCount2 + 1) aArray(lCount2 + 1) = vTemp End If Next Next End Sub
Pfft.
This ownz. I wrote it.
VB Code:
Option Explicit Private Function sortInts(arrInt() As Long) As Long() Dim i As Long, tempN As Long, allOk As Boolean Do allOk = True For i = 0 To UBound(arrInt) - 1 If arrInt(i) > arrInt(i + 1) Then allOk = False tempN = arrInt(i) arrInt(i) = arrInt(i + 1) arrInt(i + 1) = tempN End If Next If allOk Then Exit Do Loop sortInts = arrInt End Function Private Sub Form_Load() Dim x() As Long, i As Long ReDim x(10) For i = 0 To 10 x(i) = CLng(Rnd * 1000) Debug.Print x(i) Next x = sortInts(x) For i = 0 To 10 Debug.Print x(i) Next End Sub
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.
If you work with numeric values, use lpadCode:For i=0 to ubound(myArr)
List1.AddItem myArr(i)
Next
For i = 0 to List1.ListCount - 1
myArr(i) = list1.list(i)
next
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
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.
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.
Correction, if the list is completely random, statistically speaking its (n - 1) ^ 2 operations
Hey! good - you've been reading your CS texts. Keep us all in line out here.
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...