|
-
Nov 5th, 2001, 10:25 AM
#1
Thread Starter
Hyperactive Member
Sorting an array
Does anyone have any optimized code for array sorting?
-
Nov 5th, 2001, 10:31 AM
#2
Retired VBF Adm1nistrator
Microsoft MVP : Visual Developer - Visual Basic [2004-2005]
-
Nov 5th, 2001, 10:32 AM
#3
Lively Member
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
-
Nov 5th, 2001, 10:34 AM
#4
Retired VBF Adm1nistrator
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
Microsoft MVP : Visual Developer - Visual Basic [2004-2005]
-
Nov 5th, 2001, 11:49 AM
#5
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
-
Nov 5th, 2001, 11:53 AM
#6
Retired VBF Adm1nistrator
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]
-
Nov 5th, 2001, 12:02 PM
#7
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.
-
Nov 5th, 2001, 12:05 PM
#8
Retired VBF Adm1nistrator
Correction, if the list is completely random, statistically speaking its (n - 1) ^ 2 operations
Microsoft MVP : Visual Developer - Visual Basic [2004-2005]
-
Nov 5th, 2001, 12:08 PM
#9
Hey! good - you've been reading your CS texts. Keep us all in line out here.
-
Nov 5th, 2001, 12:09 PM
#10
Retired VBF Adm1nistrator
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|