Post by Rod on Nov 26, 2022 5:46:42 GMT -5
This is how I would do it.
'lets get a repeatable set of random numbers to help debug
randomize .5
'build the array adding an index and results column
starttime =time$("ms")
an=1000
dim ary$(an,5)
a=1
b=2
c=3
index=4
result=5
for i= 1 to an
n$="000"+str$(int(rnd(0)*200)):n$=right$(n$,3):ary$(i,a)=n$
n$="000"+str$(int(rnd(0)*300)):n$=right$(n$,3):ary$(i,b)=n$
n$="000"+str$(int(rnd(0)*400)):n$=right$(n$,3):ary$(i,c)=n$
ary$(i,index)=ary$(i,a)+ary$(i,b)
next
print "Array built in ";time$("ms")-starttime;" ms"
'break sum on a, sort results and display top ten
starttime =time$("ms")
'put the array in a order
sort ary$(,1,an,a
pos=1
while pos<an
count=0
v$=ary$(pos,a)
while ary$(pos,a)=v$ and pos<an
'if it meets our condition increment the count
'and zero the previous result
count=count+1
ary$(pos,result)=""
pos=pos+1
wend
'store the result to the last matching element
ary$(pos-1,result)=str$(count)
wend
'sort on result descending and display the top ten results
sort ary$(,an,1,result
for n= 1 to 10
print ary$(n,a),ary$(n,result)
next
Print "Sorted on a , break sum on a, sorted on result in ";time$("ms")-starttime;" ms"
'break sum on a+b, sort results and display top ten
starttime =time$("ms")
'put the array in a+b order
sort ary$(,1,an,index
pos=1
while pos<an
count=0
v$=ary$(pos,a)+ary$(pos,b)
while ary$(pos,a)+ary$(pos,b)=v$ and pos<an
'if it meets our condition increment the count
'and zero the previous result
count=count+1
ary$(pos,result)=""
pos=pos+1
wend
'store the result to the last matching element
ary$(pos-1,result)=str$(count)
wend
'sort on result descending and display the top ten results
sort ary$(,an,1,result
for n= 1 to 10
print ary$(n,a);" ";ary$(n,b),ary$(n,result)
next
Print "Sorted on ab, break sum on ab , sorted on result in ";time$("ms")-starttime;" ms"