•
Some
Techniques
Suppose we have reached the point where four items are sorted and we now focus
on green, the leftmost item
in
the unsorted part.
2 3 4
I
,,:"
6
7 8
black
blue
magenta
red
cyan
yellow
white
sorted part
t
unsorted part
1.
We
place green
In
the variable, comp$, and set a vanable,
p,
to
5.
2.
The variable, p, will eventually Indicate where we think green should go. When
we 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
,n
to
POSition
5 thus and decrease the value of p thus:
•
black
2 3
blue magenta
4
red
I
I
5
red
6
cyan
7
yellow
8
white
4.
We
now repeat the process but
thiS
time we are comparing green with magenta
and we get:
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.
•
5
black
black
2
blue
2
blue
3 4
magenta magenta
I
3 4
green magenta
5
red
5
red
I
I
6
cyan
6
cyan
I
7
yellow
7
yellow
8
white
8
white
1.
We
will first store the co/our$
In
an array colour$(B) and use: PROBLEM ANALYSIS
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.
4.
12/84
A REPeal loop
will
allow comparisons until we find where the comp$ value actually
goes.
REPeal compare
IF
comp$ need
go
no further left EXIT
copy
a colour
Into
the position on
its
right
and decrease
p
END REPeal compare
After EXIT from the REPeal loop the colour
in
comp$ is placed
In
position p and
the
FOR loop continues.
97