Sinclair QL Beginner's Manual page 119

Hide thumbs Also See for QL:
Table of Contents

Advertisement

4. We now repeat the process but this time we are comparing green with magenta and we get:
5. Finally we move left again comparing green with blue. This time there is no need to move or
change anything. We exit from the loop and place green in position 3. We are then ready to focus
on the sixth item, cyan.
PROBLEM ANALYSIS
1. We will first store the colour$ in an array colour$(8) and use:
comp$ the current colour being compared
p
to point at the position where we think the colour in comp$ might go.
2. A FOR loop will focus attention on positions 2 to 8 in turn (a single item is already sorted).
3. A REPeat loop will allow comparisons until we find where the comp$ value actually
goes.
REPeat compare
IF comp$ need go no further left EXIT
copy a colour into the position on its right
and decrease p
END REPeat compare
4. After EXIT from the REPeat loop the colour in comp$ is placed in position p and the FOR loop
continues.
Program Design
1 Declare array colour$
2 Read colours into the array
3 FOR item = 2 TO 8
LET p = item
LET comp$ = colour$(p)
REPEAT compare
IF comp$ > = colour$(p-1) : EXIT compare
LET colour$(p) = colour$(p-1)
LET p = p - 1
END REPeat compare
LET colour$(p) = comp$
END FOR item
4 PRINT sorted array colour$
5 DATA
Further testing reveals a fault. It arises very easily if we have data in which the first item is not in its
correct position at the start. A simple change of initial data to:

Advertisement

Table of Contents
loading

Table of Contents