Bubble Sort Routine

Author(s): Robert Schlesinger
Publication: Syntax
Pub Details: Syntax/Syntax ZX80 v2 n1 (Volume 2, Issue 1)
Date: January 1981
Pages: 2-3

“BUBBLE SORT” sorts numbers into ascending order. The sort routine starts on line 200; lines 1-190 provide random numbers to give the routine something to sort. The program generates 10 random numbers, prints them, puts them into ascending order and then prints them under the original numbers. You can use BUBBLE SORT with a string variable where the string variables are letters equal to 1 to 26 to put names or words in alphabetical order.

   1 REM * SET UP DIM ARRAY
10 DIM A(10)
30 FOR R=0 TO 10
43 LET S=RND(100)
45 LET A(R)=S
50 NEXT R
100 REM * PRINT OUT ROUTINE
105 LET P=0
107 LET X=0
110 PRINT A(X);" ";
115 LET X=X+1
120 IF X=10 THEN GO TO 150
130 IF X<10 THEN GO TO 110
150 LET P=P+1
160 IF P=2 THEN GO TO 300
190 REM **** BUBBLE SORT ROUTINE
200 FOR I=0 TO 10
210 LET J=9
220 IF A(J-1)<A(J) THEN GO TO 260
230 LET Y=A(J-1)
240 LET A(J-1)=A(J)
250 LET A(J)=Y
260 LET J=J-1
270 IF J>I THEN GO TO 220
280 NEXT I
290 GO TO 107
300 STOP

Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.