FORTRAN Bubble Sort Speghetti Code
This program is written the style of the earliest FORTRAN. It reads
up to 15 integers, stopping either when it gets 15, or when
-1 is input. It then sorts them and prints the result.
The read loop is created by the GOTO 10 instruction,
and exits by jumping either to line 40 (if a -1 was input) or
70 after the 15th number is entered.
The inner sort loop extends from line 50 to the
GOTO 50, and the outer from 70 to GOTO 70. The
90 line leaves the inner loop, and the 100 line leaves the
outer one.
Printing is accomplished with a DO loop, the most
advanced control construct available.
DIMENSION NUMS(15)
C
C READ IN THE ARRAY, UNTIL -1 OR 15 NUMBERS READ.
C LEAVE THE NUMBER READ IN N.
N = 1
10 READ 15, IN
15 FORMAT(I3)
IF(IN + 1) 20,40,20
20 NUMS(N) = IN
IF(N - 15) 25,70,70
25 N = N + 1
GOTO 10
C
C SORT THE ARRAY USING BUBBLE SORT.
40 N = N - 1
70 ISCAN = 1
IOK = 1
ISTOP = N
IF(ISTART - ISTOP) 50, 110, 110
50 IF(NUMS(ISCAN) - NUMS(ISCAN+1)) 90,90,60
C
C SWAP
60 J = NUMS(ISCAN)
NUMS(ISCAN) = NUMS(ISCAN+1)
NUMS(ISCAN+1) = J
IOK = 0
C
C TIME TO ISTART OVER?
90 IF(ISCAN - (ISTOP - 1)) 80,100,100
C
C NEXT PAIR
80 ISCAN = ISCAN + 1
GOTO 50
C
C NEXT ISCAN
100 IF(IOK) 105,105,110
105 ISTOP = ISTOP - 1
GOTO 70
C
C PRINT
110 DO 120 I=1, N
120 PRINT 130, NUMS(I)
130 FORMAT(I10)
STOP
END