Sinclair QL Beginner's Manual page 118

Hide thumbs Also See for QL:
Table of Contents

Advertisement

150 END FOR record
160 CLOSE #5
The data is printed, as before, but this time each pair of fields is held in the variable rec$ before being
printed on the screen. You have the opportunity to manipulate it into any desired form.
Note that more than one string variable may be used at the file creation stage with INPUT and PRINT
but the whole record so created may be retrieved from the Microdrive file with a single string variable
(rec$ in the above example).
AN INSERTION SORT
The following colours are available in the low resolution screen mode (in code number order 0-7).
black blue red magenta green cyan yellow white
EXAMPLE
Write a program to sort the colours into alphabetical order using an "insertion" sort.
Method
We place the eight colours in an array colour$ which we divide into two parts:
We take the leftmost item of the unsorted part and compare it with each item, from right to left, in the
sorted part until we find its right place. As we compare we shuffle the sorted items to the right so that
when we find the right place to insert we can do so immediately without further shuffling.
Suppose we have reached the point where four items are sorted and we now focus on green, the
leftmost item in the unsorted part.
1. We place green in the variable, comp$, and set a variable, p, to 5.
2. The variable, p, will eventually indicate where we think green should go. When w know that green
should move left, then we decrease the value of p.
3. We compare green with red. If green is greater than (nearer to Z) or equal to red we exit and
green stays where it is.
Otherwise we copy red in to position 5 thus and decrease the value of p thus:

Advertisement

Table of Contents
loading

Table of Contents