Basically Useful BASIC
Ascending/Descending Sort
Rick Keck
Overland Park, KS
At some point in time, every computer system user needs the services of a sort program. There has been much work done in the field of "sorting theory" and from this there has come a variety of different sorting methods. Some of these include the bubble sort, shell sort, binary sort, and tag sort. The benefit of this exists in the ability to select a method which is best for sorting data based upon the data's unique characteristics (if any). The factors which influence the decision of which sorting method to use include the following data characteristics: volume, relative order, and storage form (random access or sequential files). In a majority of cases, a simple sorting method will work fine. The standard order of sorting data is to have it sorted from smallest to biggest (ascending order). On occasion, sorting of data may need to be done from biggest to smallest (descending order). The following modified bubble sort routine allows the data to be sorted in either ascending or descending order. Note that the data is handled by character string variables so as to allow alphanumeric data to be sorted.
100 REM**************************** 110 REM* * 120 REM* ASCENDING / DESCENDING * 130 REM* * 140 REM* SORT ROUTINE * 150 REM* * 160 REM* BY RICK KECK 01/81 * 170 REM**************************** 180 DIM C$(100) 190 DATA "JOHN", "BILL", "MARY" 200 DATA "CAROLINE", "FRED", "SUE" 210 DATA "JOE" 220 REM* N HOLDS # OF DATA 230 N = 7 240 REM* READ DATA INTO C$ 250 FOR J = 1 TO N 260 READ C$(J) 270 NEXT J 280 REM* ASCENDING OR DESCENDING 290 PRINT : PRINT : PRINT 300 PRINT : PRINT "WHAT ORDER DO YOU WISH TO SORT :" 310 PRINT : PRINT " A - ASCENDING (SMALL TO BIG)" 320 PRINT : PRINT " D - DESENDING (BIG TO SMALL)" 330 PRINT : PRINT 340 INPUT A$ 350 IF A$<>"A" AND A$<>"D" THEN 340 360 PRINT : PRINT 370 REM************************** BILL 380 REM* SORT BEGINS * CAROLINE 390 REM************************** FRED 400 FOR K = 1 TO (N - 1) JOE 410 IF A$ = "A" THEN 440 JOHN 420 IF C$ = (K)> = C$(K + 1) THEN 540 MARY 430 IF A$ = "D" THEN 450 SUE 440 IF C$(K) < = C$(K + 1) THEN 540 450 FOR J = K TO 1 STEP -1 460 IF A$ = "A" THEN 490 470 IF C$(J)> = C$(J + 1) THEN 540 480 IF A$ = "D" THEN 500 490 IF C$(J) < = C$(J + 1) THEN 540 500 T$ = C$ (J) SUE 510 C$(J) = C$(J + 1) MARY 520 C$(J + 1) = T$ JOHN 530 NEXT J JOE 540 NEXT K FRED 550 REM**************************** CAROLINE 560 REM* SORT ENDS * BILL 570 REM**************************** 580 FOR L = 1 TO N 590 PRINT C$(L) 600 NEXT L 610 PRINT : PRINT "NORMAL TERMINATION" 620 END