Post by tsh73 on Nov 8, 2023 19:58:35 GMT
While testing for this thread
justbasiccom.proboards.com/thread/1052/occurrences-counting
I noticed that time that SORT takes depends on how many different values are in.
Here's the test code, compared with qsort from JB example folder.
You see?
QSort takes about same time
while built-in SORT obvoiusly has troubles sorting similar numbers
(and if array is the same it takes even longer time then QSort written in BASIC!)
so I wonder
1) what algorhithm JB/LB uses for SORT
2) is it right/expected/normal behaviour for this algorhithm
3) or there is some error in code making it work longer?
justbasiccom.proboards.com/thread/1052/occurrences-counting
I noticed that time that SORT takes depends on how many different values are in.
Here's the test code, compared with qsort from JB example folder.
You see?
QSort takes about same time
while built-in SORT obvoiusly has troubles sorting similar numbers
(and if array is the same it takes even longer time then QSort written in BASIC!)
so I wonder
1) what algorhithm JB/LB uses for SORT
2) is it right/expected/normal behaviour for this algorhithm
3) or there is some error in code making it work longer?
Size of array N = 10000
--------------------
Num different, ~ 10000
built-in sort 94
example qsort 4515
--------------------
Num different, ~ 100
built-in sort 141
example qsort 4562
--------------------
Num different, ~ 10
built-in sort 672
example qsort 4235
--------------------
Num different, ~ 2
built-in sort 3187
example qsort 4484
--------------------
Num different, ~ 1
built-in sort 6859
example qsort 4484
-- Over --
N=10000
dim a$(N)
kList$ = N;" 100 10 2 1"
print "Size of array N = ";N
for j = 1 to 5
k=val(word$(kList$, j))
print "--------------------"
print "Num different, ~ ";k
'sort ======================================
gosub [generate]
t8=time$("ms")
sort a$(),a1,N
t9=time$("ms")
print "built-in sort ";t9-t8
'qsort ======================================
gosub [generate]
t8=time$("ms")
call QSort 1, N
t9=time$("ms")
print "example qsort ";t9-t8
next
print " -- Over --"
end
sub QSort Start, Finish
i = Start
j = Finish
x$ = a$(int((i+j)/2))
while i <= j
while a$(i) < x$
i = i + 1
wend
while a$(j) > x$
j = j - 1
wend
if i <= j then
a$ = a$(i)
a$(i) = a$(j)
a$(j) = a$
i = i + 1
j = j - 1
end if
wend
if j > Start then call QSort Start, j
if i < Finish then call QSort i, Finish
end sub
'-------------------------------
[generate]
randomize .001 'same numbers for both sorts
't0=time$("ms")
for i = 1 to N
a$(i) = "item";int(rnd(1)*k)
next
't1=time$("ms")
'print "generating ";t1-t0
return