03-04-2023, 06:30 PM
(03-04-2023, 06:14 PM)SMcNeill Wrote: @TerryRitchie A working example for you:
Code: (Select All)Screen _NewImage(800, 600, 32)
Type DATATYPE
a As Integer
b As Integer
c As Integer
End Type
Const Limit = 1000000 'one million defalt limit
ReDim SortedList(1 To Limit) As DATATYPE
For i = 1 To Limit
SortedList(i).a = Int(Rnd * 32767) + 1
SortedList(i).b = Int(Rnd * 32767) + 1
SortedList(i).c = Int(Rnd * 32767) + 1
Next
For i = 1 To 30
Print SortedList(i).a, SortedList(i).b, SortedList(i).c
Next
Sleep
FakeSort SortedList()
Print "=== Sorted ==="
For i = 1 To 30
Print SortedList(i).a, SortedList(i).b, SortedList(i).c
Next
Sub FakeSort (Array() As DATATYPE)
Dim S(1 To 32767) As String
Dim TempArray(1 To Limit) As DATATYPE
For i = 1 To Limit 'build the sorted index in a single pass
S(Array(i).a) = S(Array(i).a) + MKL$(i)
Next
Count = 0
For i = 1 To 32767
For J = 1 To Len(S(i)) Step 4 'we stored our index as long values
Count = Count + 1
t$ = Mid$(S(i), J, 4)
index = CVL(t$)
TempArray(Count) = Array(index)
Next
Next
For i = 1 To Limit
Array(i) = TempArray(i)
Next
End Sub
This is a very interesting approach. When I set the limit to 32767 I get speeds on average of .040 seconds which, while blazingly fast, unfortunately is still ~4 times slower than the Qsort routine mdijkens posted at .012 seconds for 32767 indexes.