10 'Program Name--BUBBLE    7/3/82
20 'Adapted from Apple program of 10/27/82
30 '
40 'This program demonstrates how a computer
50 'does a bubble sort.
60 '
70 '     Archie Abaire
80 '     July 1982
90 '
100 DELAY = 100:' Timing delay factor
120 DIM LYST$(12)
130 DEF FNXY$(R,C) = CHR$(27) + "=" + CHR$(R+32) + CHR$(C+32)
140 '**  Warning -- RESTORE instruction has been   **
150 '**  used in subroutine for computer generated **
160 '**  list.                                     **
170 '
180 '*     Introduction to program     *
190 '
200 PRINT CHR$(26);
210 XY$ = FNXY$(5,0):PRINT XY$;
220 ON ERROR GOTO 200
230 PRINT "Hello! My name is Ozzie. What's yours?"
240 XY$ = FNXY$(23,0):PRINT XY$;"Type response and press 'RETURN'";
250 XY$ = FNXY$(8,0):PRINT XY$;:INPUT "",QNAME$ 
260 IF LEN(QNAME$) > 10 THEN QNAME$ = LEFT$(QNAME$,10)
265 PNAME$ = LEFT$(QNAME$,1)
266 FOR I = 2 TO LEN(QNAME$)
267    PNAME$ = PNAME$ + CHR$(ASC(MID$(QNAME$,I,1))+32)
268 NEXT
280 PRINT CHR$(26);
285 XY$ = FNXY$(5,0):PRINT XY$;
290 PRINT "Hello, ";PNAME$;"!":PRINT
300 PRINT "You are about to see an example of how a"
310 PRINT "computer does its work.":PRINT
320 PRINT "The bubble sort gets its name from the way"
330 PRINT "that items in a list seem to 'bubble' to"
340 PRINT "their correct positions like bubbles in a"
350 PRINT "fish tank.":PRINT
360 PRINT "Shortly you will see the kinds of operations"
370 PRINT "that the computer has to perform to put a"
380 PRINT "list into order."
390 GOSUB 18080
1000 PRINT CHR$(26);
1010 XY$ = FNXY$(3,18):PRINT XY$;
1040 '
1050 PRINT "Menu":PRINT
1060 PRINT TAB(15);"Bubble Sort"
1070 PRINT:PRINT
1080 PRINT TAB(8);"1. Let me make a list for you"
1090 PRINT
1100 PRINT TAB(8);"2. You give me a list"
1110 PRINT
1120 PRINT TAB(8);"3. Return to instructions"
1122 PRINT
1124 PRINT TAB(8);"4. Exit"
1130 XY$ = FNXY$(23,1):PRINT XY$;
1140 PRINT "Please enter a number ";:CHOICE$ = INPUT$(1)
1150 IF CHOICE$ = "9" THEN GOSUB 20000
1160 IF CHOICE$ = "1" THEN GOSUB 18000
1170 IF CHOICE$ = "2" THEN GOSUB 7040
1180 IF CHOICE$ = "3" THEN 200
1185 IF CHOICE$ = "4" THEN RUN "B:MENU"
1190 IF CHOICE$ < "1" OR CHOICE$ > "3" THEN 1000
3000 XY$ = FNXY$(1,11):PRINT CHR$(26);XY$;"*** Bubble Sort ***"
3010 XY$ = FNXY$(3,14):PRINT XY$;"List"
3020 M = 4:' M controls vertical spacing
3025 IF CHOICE$ = "2" THEN GOSUB 21000
3030 FOR I = 1 TO 8
3040    M = M + 2
3050    XY$ = FNXY$(M,11):PRINT XY$;LYST$(I)
3060 NEXT
3070 Z = FRE(0)
4000 ' Set screen format
4010 '
4020 M = 4
4030 FOR I = 1 TO 8
4040    M = M + 2:A = 0
4050    XY$ = FNXY$(M,8):PRINT XY$;I
4060       FOR J=1 TO 25:A=A+1:NEXT:' Timing delay loop
4070 NEXT
4080 XY$ = FNXY$(4,24):PRINT XY$;"Pass"
4090 GOSUB 18080
4100 XY$ = FNXY$(22,24):PRINT XY$;"Temporary"
4110 XY$ = FNXY$(23,24):PRINT XY$;"Register";
5000 '
5010 ' ** Beginning of sort procedure **
5020 '
5030 B10$ = STRING$(10," "):Z = FRE(0)
5040 FOR LL = 8 TO 2 STEP -1
5050    PASS = 9 - LL
5060    XY$ = FNXY$(4,29):PRINT XY$;" ";XY$;PASS
5070    FLAG1$ = "OFF"
5080    ' ** Sort loop **
5090    FOR L = 1 TO LL - 1
5100       FLAG2 = 0:' Decision flag
5110       IF LYST$(L) <= LYST$(L+1) THEN FLAG2 = 1
5120       XY$ = FNXY$(2*L+4,21):PRINT XY$;"*"
5130       XY$ = FNXY$(2*L+6,21):PRINT XY$;"*"
5140       GOSUB 15000
5150       IF FLAG2 = 1 THEN 5400
5160       ' ** Swap occurs here **
5170       XY$ = FNXY$(2*L+4,11):PRINT XY$;B10$
5180       TEMP$ = LYST$(L)
5190       XY$ = FNXY$(2*L+4,24):PRINT XY$;B10$;XY$;TEMP$
5200       GOSUB 15000
5210       LYST$(L) = LYST$(L+1)
5220       XY$ = FNXY$(2*L+4,11):PRINT XY$;B10$;XY$;LYST$(L)
5230       XY$ = FNXY$(2*L+6,11):PRINT XY$;B10$
5240       GOSUB 15000
5250       XY$ = FNXY$(2*L+6,11):PRINT XY$;TEMP$
5260       XY$ = FNXY$(2*L+4,24):PRINT XY$;B10$
5270       LYST$(L+1) = TEMP$
5300       FLAG1$ = "ON ":' End of swap
5400       XY$ = FNXY$(2*L+4,21):PRINT XY$;" "
5410       XY$ = FNXY$(2*L+6,21):PRINT XY$;" "
5420    NEXT
5430    IF FLAG1$ = "OFF" THEN 6000
5440    GOSUB 15000
5450 NEXT
5460 ' ** End of sort routine **
5470 '
6000 ' ** Job Exit **
6010 '
6020 XY$ = FNXY$(8,24):PRINT XY$;"Job"
6030 XY$ = FNXY$(9,24):PRINT XY$;"Completed"
6040 XY$ = FNXY$(22,24):PRINT XY$;B10$
6050 XY$ = FNXY$(23,24):PRINT XY$;B10$;
6060 GOSUB 18080
6160 ' Set delay to normal
6180 IF DELAY = 1 THEN DELAY = 100:GOTO 1000:' Return to menu
6200 XY$ = FNXY$(10,0):PRINT CHR$(26);XY$;
6210 PRINT "Would you like to see how fast I can go? (Y/N)"
6220 Q$ = INPUT$(1)
6230 IF Q$ = "N" THEN 1000
6240 IF Q$ <> "Y" THEN 6220 
6250 DELAY = 1:GOSUB 18000
6260 GOTO 3000
7000 ' ** User keys list **
7010 '
7040 MSG$(1) = "Enter the words you would like to"
7050 MSG$(2) = "have sorted. They may have up to"
7060 MSG$(3) = "ten characters. Numbers will be"
7070 MSG$(4) = "treated as if they were letters."
7080 MSG$(5) = "TO CORRECT ERRORS"
7090 MSG$(6) = "use backspace arrow"
7100 MSG$(7) = "at right end of key"
7110 MSG$(8) = "board."
7120 MSG$(9) = "Press RETURN key"
7130 MSG$(10) = "to enter each word."
7140 '
7145 PRINT CHR$(26)
7150 FOR I = 1 TO 4
7160    XY$ = FNXY$(2*I,3)
7170    PRINT XY$;MSG$(I)
7180 NEXT
7190 XY$ = FNXY$(11,21):PRINT XY$;MSG$(5)
7200 FOR I = 6 TO 8
7210    XY$ = FNXY$(I+7,21)
7220    PRINT XY$;MSG$(I)
7230 NEXT
7240 ' Prenumber list
7250 FOR I = 1 TO 8
7260    XY$ = FNXY$(10+I,3):PRINT XY$;I
7270 NEXT
7280 '
8000 ' ** Enter list **
8010 '
8020 MARK$ = STRING$(10,".")
8030 FOR I = 1 TO 8
8040    XY$ = FNXY$(21,21)
8050    PRINT XY$;MARK$;XY$;
8060    INPUT "",WORD$
8070    PRINT XY$;SPC(LEN(WORD$) + 1)
8080    LYST$(I) = LEFT$(WORD$,10)
8090    XY$ = FNXY$(10+I,6)
8100    PRINT XY$;LYST$(I)
8120 NEXT
8140 '
8150 ' Extra copy of original list
8160 '
8170 FOR I = 1 TO 8
8180    EXTRALYST$(I) = LYST$(I)
8190 NEXT
8200 RETURN
9999 END
15000 ' ** Delay loop **
15010 '
15020 FOR I = 1 TO DELAY
15030    A = A + 1
15040 NEXT
15050 A = 0
15060 RETURN
15070 '
17900 ' ** Data for computer generated list **
17910 '
18000 DATA DEMOCRITUS,OZZIE,ZENO,ARCHIMEDES
18010 DATA CARVER,KEPLER,BOOLE,PASTEUR,HARVEY,HERO
18020 FOR I = 1 TO 7
18030    READ LYST$(I)
18040 NEXT
18050 LYST$(8) = QNAME$
18060 RESTORE
18070 RETURN
18075 ' ** 'Ready to continue' display **
18080 XY$ = FNXY$(23,0)
18090 PRINT XY$;"Press any key to continue...";
18100 Q$ = INPUT$(1)
18110 PRINT XY$;SPC(30);
18120 RETURN
18130 '
20000 ' ** Modify delay factor **
20010 '
20020 XY$ = FNXY$(10,0):PRINT CHR$(26);XY$
20030 'COUNT = COUNT - 1
20040 PRINT "Initial delay factor = 100"
20050 PRINT:PRINT "Present delay factor =";DELAY
20060 PRINT:INPUT "Change factor to ",NEWDELAY
20070 IF NEWDELAY = 0 THEN 20150
20080 IF NEWDELAY < 0 THEN NEWDELAY = 100
20090 IF NEWDELAY > 500 THEN NEWDELAY = 500
20100 DELAY = NEWDELAY
20150 RETURN
21000 '** Reuse user's list **
21010 '
21020 FOR I = 1 TO 8
21030    LYST$(I) = EXTRALYST$(I)
21040 NEXT
21050 RETURN
N
21000 '** Reuse user's list **
21010 '
21020 FOR I = 1 TO 8
21030    LYST$(I) = EXTRALYST$(I)
21040