Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Alphabetizing Anyone?
#4
And for variable length strings in an array called sa$() (sa=String Array):
Code: (Select All)
DefLng A-Z
Const nItems = 1000000
ReDim Shared sa$(1 To nItems) 'setup with string array sa$() shared so dont have to pass as parameter
For x = 1 To nItems ' make a random list to sort
    b$ = ""
    r = (Rnd * 5) \ 1 + 2
    For i = 0 To r
        b$ = b$ + Mid$("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789.?", (Rnd * 64) \ 1 + 1, 1)
    Next
    sa$(x) = b$
    Print b$,
Next
Print
Print "Press any to sort"
Sleep
t## = Timer(.01)
QSort 1, nItems
time## = Timer(.01) - t##
Cls
For i = 1 To nItems
    Print sa$(i),
Next
Print
Print "time:"; time##


' modified for QB64 from JB
' This is the best all purpose sort routine around, don't worry how it works, it just does!
' To use this sub rountine store all the string values you want to sort into sa$() array
' call Qsort with Start = 1 and Finish = number of Items in your array
Sub QSort (Start, Finish) 'sa$ needs to be   DIM SHARED !!!!     array
    Dim i As Long, j As Long, x$
    i = Start
    j = Finish
    x$ = sa$(Int((i + j) / 2))
    While i <= j
        While sa$(i) < x$
            i = i + 1
        Wend
        While sa$(j) > x$
            j = j - 1
        Wend
        If i <= j Then
            Swap sa$(i), sa$(j)
            i = i + 1
            j = j - 1
        End If
    Wend
    If j > Start Then QSort Start, j
    If i < Finish Then QSort i, Finish
End Sub
b = b + ...
Reply


Messages In This Thread
Alphabetizing Anyone? - by NakedApe - 12-20-2023, 05:42 PM
RE: Alphabetizing Anyone? - by SMcNeill - 12-20-2023, 06:17 PM
RE: Alphabetizing Anyone? - by NakedApe - 12-20-2023, 08:20 PM
RE: Alphabetizing Anyone? - by bplus - 12-20-2023, 09:53 PM
RE: Alphabetizing Anyone? - by NakedApe - 12-21-2023, 05:27 PM
RE: Alphabetizing Anyone? - by mnrvovrfc - 12-21-2023, 05:32 PM
RE: Alphabetizing Anyone? - by bplus - 12-21-2023, 09:42 PM
RE: Alphabetizing Anyone? - by SMcNeill - 02-25-2024, 03:24 AM
RE: Alphabetizing Anyone? - by SpriggsySpriggs - 12-21-2023, 06:02 PM
RE: Alphabetizing Anyone? - by TerryRitchie - 12-21-2023, 06:08 PM
RE: Alphabetizing Anyone? - by SpriggsySpriggs - 12-21-2023, 07:46 PM
RE: Alphabetizing Anyone? - by NakedApe - 12-21-2023, 08:06 PM



Users browsing this thread: 1 Guest(s)