IBM 1130 User Manual page 602

Computing system
Hide thumbs Also See for 1130:
Table of Contents

Advertisement

Section
Subsections
Page
75
40
I
00
01
A DETAILED LOOK AT AN
1130
RECORD SORT
An
improvement on the simple exchange technique
consists of making alternate passes in opposite
directions, attempting to move the high records to
the bottom and the low records to the top of the file.
This is called an alternating pair exchange sort.
The procedure starts in exactly the same manner
as in pair exchange, by comparing the keys of
successive records. After
an exchange is made,
the high key is compared with the key of the next
record in sequence, and these comparisons con-
tinue until either a higher key is found or the end of
the file is reached. All intermediate records (and
their keys) are shifted up one pOSition. During this
first downward pass, therefore, a high record can
move down many positions, but lower records can
move up only one pOSition.
The second pass is in the upward direction (from
the bottom to the top of the file) and tends to move
the smaller records closer to the beginning. Dur-
ing this, and during every other even-numbered
pass, a high record can move down only one position,
but a low record can move up many positions.
Successive passes continue to alternate, with odd-
numbered passes in ascending sequence and even-
numbered passes in descending sequence. The file
is in sequence when no interchanges occur during
a pass. A final output pass is required to verify the
correct sequence.
The example below illustrates the alternating
exchange technique. The first pass, proceeding
downward, recognizes that
89
and
56
are out of se-
quence and exchanges them. The high of the com-
pare,
89,
is then compared, in turn, with
02, 08,
and
21;
since
89
is higher in each case, it moves to
the bottom of the file. The low of the compare,
56,
moves up one, and all intermediate keys also move
up one pOSition. In the second pass, the compari-
sons start with
89
and move upward. The first out-
of-sequence keys are
02
and
56.
The
56
drops down
one pOSition, while the
02
moves up two positions,
since it is lower than the
13.
During the next down-
ward pass, the
56
and
08
are out of sequence; the
08
moves up one position, and the
56
moves down two
positions, since it is higher than the intermediate
21. During the fourth (upward) pass the out-of-se-
quence
08
and
13
are interchanged. The final out-
put pass is needed to check the completion of the
sort.
End
Input
Pass 1
End
End
End
Pass 2
Pass 3
Pass 4
13
13
02
02
02
8 K 5 6 -
/
13
13~08
56
02:><--"'5~0~
-----13
02
08
08
21
21
08
21
21
56
56
21
8
1
9
89
89
89
t
t
~
t
Output
02
08
13
21
56
89
The arrows indicate the direction of the pass.
The example shows that the maximum number of
passes is equal to the distance, measured in num-
ber of keys or records, which is the largest sepa-
ration of a key from its final place in the sequence.
In
this case,
89
is four pOSitions away from its final
(bottom) position in the file and, therefore, at most
four passes (plus the output pass) are required to
complete the sort.
In
general, the alternating
exchange method requires slightly more complex
programming than the earlier exchange method, but
it results in a smaller number of compares and,
frequently, fewer passes. The following pages
show this method in more detail.

Advertisement

Table of Contents
loading

Table of Contents