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.
Printable View
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.
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.
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
thanks, it might be a bit slow, is there another way to do it?
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?
About 1.3 million
ok i'll post a better method tomorrow morning.
I dont have time right now.
Thanks.
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
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 ...
As far as I can tell it works ;)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
And its very fast too ...
- jamie
How do I use that code if I'm populating an array like this?
:confused: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
what is postcodes generated by?
and are the values all numeric:confused:
it's in this format:
"text","text","number between 1-12"
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