Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need a sorting routine
#11
(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
Print "=== Sorted ==="
Print
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.
New to QB64pe? Visit the QB64 tutorial to get started.
QB64 Tutorial
Reply


Messages In This Thread
Need a sorting routine - by TerryRitchie - 03-04-2023, 06:53 AM
RE: Need a sorting routine - by mnrvovrfc - 03-04-2023, 08:58 AM
RE: Need a sorting routine - by TerryRitchie - 03-04-2023, 05:34 PM
RE: Need a sorting routine - by mdijkens - 03-04-2023, 09:00 AM
RE: Need a sorting routine - by TerryRitchie - 03-04-2023, 06:16 PM
RE: Need a sorting routine - by bplus - 03-04-2023, 10:02 AM
RE: Need a sorting routine - by RhoSigma - 03-04-2023, 10:03 AM
RE: Need a sorting routine - by SMcNeill - 03-04-2023, 10:46 AM
RE: Need a sorting routine - by SMcNeill - 03-04-2023, 06:14 PM
RE: Need a sorting routine - by TerryRitchie - 03-04-2023, 06:30 PM
RE: Need a sorting routine - by SMcNeill - 03-04-2023, 06:27 PM
RE: Need a sorting routine - by SMcNeill - 03-04-2023, 06:47 PM
RE: Need a sorting routine - by TerryRitchie - 03-04-2023, 06:52 PM
RE: Need a sorting routine - by SMcNeill - 03-04-2023, 07:14 PM
RE: Need a sorting routine - by Dimster - 03-05-2023, 02:49 PM
RE: Need a sorting routine - by TempodiBasic - 03-11-2023, 02:26 AM
RE: Need a sorting routine - by TempodiBasic - 03-11-2023, 02:34 AM



Users browsing this thread: 8 Guest(s)