CSC 272 - Software II: Principles of Programming Languages

Dr. R. M. Siegfried

A bubble sort program in BASIC

100 REM - Declare Array to be an array
110 DIM ARRAY(10)
120 GOSUB 1000 ' read an array from a file
130 GOSUB 2000 ' print the array on the screen
140 GOSUB 3000 ' sort the array
150 GOSUB 2000
180 REM
300 GOTO 9990
1000 REM - read array from a file
1010 INPUT "Enter file name "; FILENAME$
1020 OPEN FILENAME$ FOR INPUT AS #1
1030 REM
1040 VECTORLEN% = 0
1050 WHILE NOT EOF(1)
1060   ARRAYLEN% = ARRAYLEN% + 1
1070   INPUT #1, ARRAY(ARRAYLEN%)
1080 WEND
1090 RETURN
2000 REM - Display the array on the screen
2010 FOR I = 1 TO ARRAYLEN%
2020   PRINT "X("; I; ") = "; ARRAY(I)
2030 NEXT I
2035 PRINT
2040 RETURN
3000 REM - A classic bubble sort
3010 PASS = 1
3020 S$ = "No"
3030 FOR I = 1 TO ARRAYLEN%-PASS
3040   IF ARRAY(I) <= ARRAY(I+1) THEN 3090
3050     S$ = "Yes"
3060     TEMP = ARRAY(I)
3070     ARRAY(I) = ARRAY(I+1)
3080     ARRAY(I+1) = TEMP
3090 NEXT I
3100 PASS = PASS + 1
3110 IF S$ = "Yes" THEN 3020
3120 RETURN
9990 CLOSE #1
9999 END

[Back to the BASIC summary]