'***************************************************** '* Sorting an array with straight insertion method * '* ------------------------------------------------- * '* REFERENCE: * '* "NUMERICAL RECIPES By W.H. Press, B.P. Flannery, * '* S.A. Teukolsky and W.T. Vetterling, Cambridge * '* University Press, 1986" [BIBLI 08]. * '* * '* Basic Version By J-P Moreau * '* (www.jpmoreau.fr) * '* ------------------------------------------------- * '* SAMPLE RUN: * '* * '* Table to be sorted: * '* * '* 374.7499 144.5918 787.0175 944.9621 612.4481 * '* 332.1016 58.5269 666.6479 513.2499 656.3724 * '* 573.1826 666.3972 444.1546 970.7115 439.0748 * '* 442.4243 435.0529 868.6649 594.3906 287.9611 * '* 713.5782 45.7716 53.6917 710.1408 737.0276 * '* * '* Sorted table (straight insertion): * '* * '* 45.7716 53.6917 58.5269 144.5918 287.9611 * '* 332.1016 374.7499 435.0529 439.0748 442.4243 * '* 444.1546 513.2499 573.1826 594.3906 612.4481 * '* 656.3724 666.3972 666.6479 710.1408 713.5782 * '* 737.0276 787.0175 868.6649 944.9621 970.7115 * '***************************************************** defint i-n dim A(100) 'Table to be sorted cls N=25 'initialize size of table TMAXVALUE = 1000 RANDOMIZE TIMER 'generate random table of numbers (from 0 to 1000) for i=1 to N A(i)=TMAXVALUE*Rnd(1) next i print print " Table to be sorted:" gosub 2000 'call TWRIT(N,A) 'call sorting subroutine gosub 1000 'call PIKSRT(N,A) print : print print " Sorted table (straight insertion):" gosub 2000 'call TWRIT(N,A) print END '***************************************************** '* Sorts an array ARR of length N in ascending order * '* by straight insertion. * '* ------------------------------------------------- * '* INPUTS: * '* N size of table A * '* A table to be sorted * '* OUTPUT: * '* A table sorted in ascending order * '* * '* NOTE: Straight insertion is a Nē routine and * '* should only be used for relatively small * '* arrays (N<100). * '***************************************************** 1000 'SUBROUTINE PIKSRT(N,A) for j=2 to N aa=A(j) for i=j-1 to 1 step -1 if A(i)<=aa then goto 10 A(i+1)=A(i) next i i=0 10 A(i+1)=aa next j return 'write table of size N to standard output 2000 'SUBROUTINE TWRIT(N,A) f\$=" ###.####" print for i=1 to N print using F\$; A(I); if INT(i/5)=i/5 then print next i return 'end of file sort1.bas