258
275
28121
28121
31121
35121
242
26121
275
28121
282
31121
There will be a pause while the program sorts the data. Then the output should
appears as follows:
SORTED
VALUES
25121
25121
255
26121 27121
27121
28121
28121
28121
285
28121
28121
3121121
3121121
31121
32121
33121
346
Reading the table row by row, the values are printed in ascending order, the smallest
value is 242, which is listed first and the largest value is
350 which is listed last.
Line 5 The OPEN statement again allows input from RAM.
Line
10 The one dimensional array S is dimensioned in this line. Remember that there
are thirty sales values.
Line
20.
The numeric variable N is given the value 30.
It
will be convenient to use N
instead
of
30 in the program.
If
the number
of
sales values read in from the file is
changed, only this line will have to be changed.
Lines
30
• 50 These lines read in the data and store the values sequentially in the
array S.
Line
500 This line starts the "bubble
sort"
portion
of
the program. It is necessary to
go through the array 29 times to get all the values in their correct positions. The first
time through the loop (I
=
1)
the largest sales value will be placed in S(30), the
second time through the loop
(I
= 2), the next largest will be placed in S(29), etc.
The last time through the loop (I
= 29) the next to the smallest value will be placed
in S(2). At that point S(1) must contain the smallest value, so the array is sorted.
Line
510
This line starts the
FOR!
NEXT loop in which the comparisons and possible
interchanges will be made. When I
=
1,
J will range from 1 to N - 1, when I = 2,
J will range from 1 to N - 2, and so on. When I = 29, J will only have the value
1.
Thus
as
more and more values are stored in their correct positions at the end
of
the
array S, fewer and fewer comparisons and interchanges need to
be made.
Line
520 This line does the comparison.
If
the values stored
in
adjacent locations,
SO) and S(J + 1), are already in order, then execution jumps to the bottom
of
the
inside loop. However,
if
they are out
of
order, then line 530 is executed next.
Line
530 These three statements swap the values stored in S(J) and S(J + 1). Note
that the value stored in
SO) is stored temporarily in the variable S. This is necessary
when swapping two adjacent elements in an array,
to
prevent erasing one
of
them. It
is permissible to have an array S and an ordinary numeric variable S as well.
Line
540 The two NEXT statements terminate the FOR I NEXT loops. The NEXT J
statement terminates the inner loop and the NEXT I statement terminates the outer
loop.
Line
600 When the sorting is finished, this line displays a heading for the table.
Line
610 The sorted values are displayed in six rows so that they will all fit on the
display at one time. The FOR
I NEXT loop which begins in this line increments once
for each
of
the six rows.
137