This is faster than combsort on my system..
I'm using a quicksort algorithm iteratively with 2 stacks set up to hold pointers to the lower and upper level of each segment being sorted. The size of the stacks could be increased if you were sorting millions of items... but it probably isn't necessary.
What do you guys think? Should I add features/get rid of bad programming pieces?
code:
function qs($a)
DIM $ls[32],$us[32],$sp,$L,$U,$m,$p,$i,$j,$t
$ls[0]=0
$us[0]=UBOUND($a)
$sp=0
While $sp >=0
$l=$ls[$sp]
$u=$us[$sp]
While $L < $U
$p=$L+($U-$L)/2
$t=$a[$L]
$A[$L]=$A[$P]
$A[$P]=$t
$i=$L+1
$j=$U
:L1
While ($i<$j) AND $A[$L] > $A[$i]
$i=$i+1
Loop
While ($j>=$i) AND $A[$j] > $A[$L]
$j=$j-1
Loop
IF $i >= $j goto L2 ENDIF
$t=$A[$i]
$A[$i]=$A[$j]
$A[$j]=$t
$j=$j-1
$i=$i+1
Goto L1
:L2
$t=$a[$l]
$a[$l]=$a[$j]
$a[$j]=$t
$m=$j
If $m-$l <= $u - $m
If $m+1 < $u
$ls[$sp]=$m+1
$us[$sp]=$u
$sp=$sp+1
Endif
$u=$m-1
Else
If $m-1 > $l
$ls[$sp]=$l
$us[$sp]=$m-1
$sp=$sp+1
Endif
$l=$m+1
Endif
Loop
$sp=$sp-1
Loop
$qs=$a
Endfunction
On my system:
at 10,000 random elements:
combsort takes 90% longer
at 1,000 random elements:
combsort takes 50% longer
at 100 random elements:
combsort takes 33% longer
.....
When the number of elements is < 12, quicksort takes longer on average, but still under 20 ticks on a PIII-400/Win95 system.
Brian
P.S. .. anyone want to try to add multi-dimensional array capabilities?