Radio Shack TRS-80 Model 100 Basic Manual page 142

Basic language lab
Hide thumbs Also See for TRS-80 Model 100:
Table of Contents

Advertisement

Experiment #4 Sorting
The thirty sales values in file SALEDA.DO are to be sorted and printed out in
ascending order. Sorting can
be
easily accomplished if the sales are stored in a one
dimensional array.
The sorting routine which will be used is called a "bubble sort." This is a simple
algorithm that is easy to understand and to program.
The bubble sort works as follows:
Starting at the beginning of the array, the first two values are compared.
If
the
first is larger than the second, they are interchanged; otherwise nothing is done.
The second and third values are then compared. If the second is larger than the
third, they are interchanged, and so forth through the entire array. When the end
of the array is reached, the largest value will be stored there. The process then
begins over at the beginning of the array. This time, however, it will only be
necessary to go up through the next to the last element. In this fashion, the
largest element goes to the end, the next largest goes to the next to the end
position, etc.
The name "bubble sort" is descriptive of this process because of the way the large
values pop up, one at a time, into their correct positions at the end of the array.
The following program will read in the thirty sales values, sort them and display them
in ascending order. Delete the previous program from memory with the NEW
command and enter the following
program:
5
OPEN "RAM:SALEoA.oo" FOR INPUT AS 1
10
DIM S(30)
20
N = 30
30
FOR I = 1 TO 30
lI0
INPUT
#
1
t
S ( I )
50
NEXT I
50121 FOR I =
1
TO N
-
1
510 FOR J =
1
TO N
-
I
52121 IF S(J)
<
S(J +
1)
GoTo
5110
53121 S=S(J)
:
S(J)=S(J+l)
S(J+l)=S
540 NEXT J
·
NEXT I
·
6.00 PRINT TAB(10) "SORTED SALES"
610 FOR R = 1 TO 6
620 FOR C =
1
TO 5
630 I = (R
-
1 )
*
5 + C
6110
PRINT TAB«C
-
1)
*
7 )
S ( I ) ;
650 NEXT C
·
PRINT
·
660 NEXT R
Execute the program.
136

Advertisement

Table of Contents
loading

Table of Contents