|
Post by bplus on Dec 22, 2023 13:30:55 GMT -5
Lets test it for 1 Million items randomly generated:
DefLng A-Z
' comment one of the following 'Const nItems = 1000000 'for timing 1 Million items Const nItems = 50 ' fits array before and after on one screen
Dim sa$(0 To nItems) 'setup with string array sa$() to hold random generated strings 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 0, nItems, sa$() time## = Timer(.01) - t## 'Cls For i = 0 To nItems Print sa$(i), Next Print Print "time:"; time##
' 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 Arr$() array ' call Qsort with Start = LBound(Arr$) and Finish = UBound(Arr$) Sub QSort (Start, Finish, Arr$()) Dim i As Long, j As Long, x$ i = Start j = Finish x$ = Arr$(Int((i + j) / 2)) While i <= j While Arr$(i) < x$ i = i + 1 Wend While Arr$(j) > x$ j = j - 1 Wend If i <= j Then Swap Arr$(i), Arr$(j) i = i + 1 j = j - 1 End If Wend If j > Start Then QSort Start, j, Arr$() If i < Finish Then QSort i, Finish, Arr$() End Sub
I am getting about 1.5 secs on my system for 1 Million items, your results may be different.
|
|
aurel
Full Member
Posts: 233
|
Post by aurel on Dec 23, 2023 3:03:43 GMT -5
How is this "the best" ..if we don't know what is original code in JustBasic
|
|
|
Post by bplus on Dec 23, 2023 14:39:41 GMT -5
Merry Christmas Aurel
|
|
aurel
Full Member
Posts: 233
|
Post by aurel on Dec 24, 2023 3:21:53 GMT -5
Merry to you too... but really ..why you not post JB code ?
|
|
|
Post by bplus on Dec 24, 2023 19:16:52 GMT -5
Merry to you too... but really ..why you not post JB code ? OK since this is Christmas, this came with JB samples: 'Sort an array. 'Adapted from Beginning Programming for Dummies by Wallace Wang
mainwin 80 'give me 80 columns MaxSize = 25 dim NumArray(MaxSize) for i = 1 to MaxSize NumArray(i) = int(rnd(1)*10) + 1 print NumArray(i); " "; next i print "(Initial array)"
call QSort 1, MaxSize
for i = 1 to MaxSize print NumArray(i); " "; next i print "(Sorted array)" end
sub QSort Start, Finish i = Start j = Finish x = NumArray(int((i+j)/2)) while i <= j while NumArray(i) < x i = i + 1 wend while NumArray(j) > x j = j - 1 wend if i <= j then a = NumArray(i) NumArray(i) = NumArray(j) NumArray(j) = a i = i + 1 j = j - 1 end if wend for k = 1 to Finish print NumArray(k); " "; next k print if j > Start then call QSort Start, j if i < Finish then call QSort i, Finish end sub
|
|
aurel
Full Member
Posts: 233
|
Post by aurel on Dec 25, 2023 5:42:03 GMT -5
thanks ..so is not that hard and i can try original in LBB
|
|