Here's a fast version of the bubblesort method:
function quicksort(lo,hi)
sorted = 0
while sorted = 0
sorted = 1
for a=lo to hi-1
if test(a) > test(a+1)
h = test(a) : test(a)=test(a+1) : test(a+1)=h : sorted = 0
endif
next a
endwhile
endfunction
replace test() with your array name. Parameters are:
Lo = lowest index number : hi = highest index number
This way you can sort different areas of your array
here's the example code:
sync on
sync rate 0
dim test(5000)
for a=1 to 5000
test(a) = rnd(5000)
print test(a)
sync
next a
print "Press a key to sort"
suspend for key
quicksort(1,5000)
for a=1 to 5000
print test(a)
sync
next a
wait 1000
end
function quicksort(lo,hi)
sorted = 0
while sorted = 0
sorted = 1
for a=lo to hi-1
if test(a) > test(a+1)
h = test(a) : test(a)=test(a+1) : test(a+1)=h : sorted = 0
endif
next a
endwhile
endfunction
it sorts a 5000 array in 230ms here