• No results found

Some sorting machines

N/A
N/A
Protected

Academic year: 2021

Share "Some sorting machines"

Copied!
16
0
0

Bezig met laden.... (Bekijk nu de volledige tekst)

Hele tekst

(1)

Some sorting machines

Citation for published version (APA):

Bruijn, de, N. G. (1979). Some sorting machines. (Eindhoven University of Technology : Dept of Mathematics : memorandum; Vol. 7905). Technische Hogeschool Eindhoven.

Document status and date: Published: 01/01/1979

Document Version:

Publisher’s PDF, also known as Version of Record (includes final page, issue and volume numbers)

Please check the document version of this publication:

• A submitted manuscript is the version of the article upon submission and before peer-review. There can be important differences between the submitted version and the official published version of record. People interested in the research are advised to contact the author for the final version of the publication, or visit the DOI to the publisher's website.

• The final author version and the galley proof are versions of the publication after peer review.

• The final published version features the final layout of the paper including the volume, issue and page numbers.

Link to publication

General rights

Copyright and moral rights for the publications made accessible in the public portal are retained by the authors and/or other copyright owners and it is a condition of accessing publications that users recognise and abide by the legal requirements associated with these rights. • Users may download and print one copy of any publication from the public portal for the purpose of private study or research. • You may not further distribute the material or use it for any profit-making activity or commercial gain

• You may freely distribute the URL identifying the publication in the public portal.

If the publication is distributed under the terms of Article 25fa of the Dutch Copyright Act, indicated by the “Taverne” license above, please follow below link for the End User Agreement:

www.tue.nl/taverne Take down policy

If you believe that this document breaches copyright please contact us at: openaccess@tue.nl

providing details and we will investigate your claim.

(2)

EINDHOVEN UNIVERSITY OF TECHNOLOGY

Department of Mathematics

Memorandum 79-5

Issued July 1979

SOME SORTING MACHINES

by

N.G. de Bruijn

Eindhoven University of Technology Department of Mathematics

P.O.Box 513

5600 MB Eindhoven The Netherlands

(3)

Some sorting machines by N.G. de Bruijn.

1. Introduction. Recently Ph.Armstrong devised a sorting memory with a remarkable feature: immediately after the last entry of a sequence of numbers has been read into the machine, the numbers come out again, but now sorted as to increasing order. The design was modified by M. Rem [IJ. In section 10 of this note we describe the machine and a particular way of using it . Sections 2-9 are devoted to proving that

this procedure sorts every input sequence. Such a proof had not yet been given by Armstrong and Rem.

2. Sorting by means of m1n1swaps. We repeat some of the material in [2J . Let n be a positive integer. A permutation rr of {I, ... ,n} is a one-to-one mapping of that set onto itself. With rr we associate the array (rr(I), ••• ~(n)).

If 1 ~' i < j ~ n then S .. rr denotes the permutation we get from rr by sorting 1.J

the i-th and j-th position in the following sense. I f S . . rr =wthen w(k) = 1.J

rr(k) for all k which differ from both i and j, and wei) = min(rr(i),rr(j)) , w(j)

then

= max(rr(i),rr(j)). The operator S .. 1.J S .. is called a miniswap. We shall

1J

Let rr be the o identity permutation,

is called a swap; if j = i + 1 abbreviate this S 1.. . 1 ,1.+ to S .. 1. and rr the one defined by rr (i)

e e

=

n + 1 - i (I ~ i ~ n). The following theorem is due to R.W.Floyd (see [3J, section 5.3.4, problem 36, p. 241; solution p. 640; two further proofs are presented in [2J ).

Theorem 2.1. If a sequence of miniswaps sorts rr then it sorts every rr, e i.e. if 1 ~ i) ~ n, ••. ,1 ~ 1. N ~ n, and S.

s.

rr rr 0 ' 1.1 1.N e then S.

...

s.

rr = rr 1.) 1.N 0 for all permutations rr.

As a curiosity we mention a generalisation, although we have no use for it 1.n the present note. First we introduce the word coswap (see [2J): If ) ~ P < q ~ n then w = C rr is obtained from n by sorting the values p

pq

(4)

= TI(k) if TI(k) 1S different from both p and q. A minicoswap is a coswap C with q = p + I, and will be abbreviated to C .

pq p

Theorem 2.2. If MI""'~ are miniswaps or minicoswaps (i.e. elements of the set {SI,S2""'Sn_I'CI "" ,Cn_ I }) and MI ..• ~ TIe =no ' then MI "'~ ~

= TI for all permutations TI. o

Proof. InD] two proofs were given for Floyd's theorem, the first one of which uses the fact that miniswaps preserve a certain partial order. In

section 7.2 of that paper it is remarked that minicoswaps also preserve that partial order. Therefore that proof can still be used for Theorem 2.2.

3. Sorting real arrays. In section 2 we have sorted permutations, but we might as well sort arrays (a(I), ..• ,a(n)) of real numbers which are not necessarily pairwise different. If I $ i < j $ n, the miniswap Si is defined as follows: S. a = S means that SCi) = min(a(i) ,a(i+I)), S(i+I)=

1

max(a(i),a(i+I)) and S(k) = a(k) for all other k.

If a is a real array then there is at least one permutation n of {I, ... ,n} such that a(p) $ a(q) for all p,q with n(p) $ n(q). Such a

permutation is said to be compatible with a.

It is easy to see that if TI is compatible with a, and I $ i < n, then S.TI is compatible with S.a. Therefore Theorem 2.1 leads to the

1 1

following:

Theorem 3.1. If I $ i

l < n, ... ,1 $ iN < n, if a is any real array, and if y, with S·

11 then y 1S sorted, 1.e. y(l) ~ y(2) $

TI

e

$

y

en).

4. An example. We shall present a miniswap sequence that sorts every array of length n.

Let n be a positive integer, n ~ 2, let c be the largest odd number not exceeding n, and d the largest even number not exceeding n. Define

the operators

Q,

R, T by

Q (4.1)

(4.2)

(5)

We can show that T sorts every array of length n, by application of

n

Theorem 3.1. It suffices to show that T sorts the array (n,n-I, ... ,3,2,1).

n

We shall not present n formal proof of this, but just show what happens in the cases n=6 and n=7. If we observe along which lines the numbers move 1n fig. 1 and fig. 2, we understand the pattern for general n. The top line in the diagrams presents the array we start from, the next line is obtained

from this line by application of

Q,

the third one from the second by R, etc.

Fig. 1 Fig. 2

These sorting programs were presented in Knuth

D

J

.

(Exercise 37, section 5.3.4; the solution refers to H.Seward (1954), A.Grasselli and to Kautz et.al.)

5. Another example. We shall describe a more complicated miniswap sequence

that sorts every array of length 3n. This example will play a role in the

next sections. The array index runs from 1 to 3n. We define, for 1 ~ i ~ 2n, W. by

1

W. 1 = S. 1 S1+ . 1 .. . S2 . n

As in section 4, let c be the largest odd number not exceeding n, and d the largest even number not exceeding n. We define Q,R,T

k as 1n (4.1),(4.2),(4.3), and similarly

Q*

=

S S 2n+d-1 2n+d-3 S2n+c-1 S2n+c-3 .•. S2n+4 S2n+2'

* * * *

*

RR

QQ

RR

QQ

RR , ... Finally we take

(6)

(5. 1 )

This K is a product of miniswaps, and we claim that it sorts every array of length 3n. According to Theorem 3.1 it suffices to show that it sorts

(3n,3n-I, •.. ,2,1). Fig. 3 shows this for the case n=3, fig. 4 for the

case n=4. In these figures we have written at the end of each line the

operation which turns it into the next line. It is indicated how the numbers move, and it will be clear that for general n we get the same patterns. We have omitted these move indications in the last n lines

S1nce it is clear from section 4 that the further operations sort the first n as well as the last n entries. We do not bother to study the moving pattern for these details, and we do not bother to prove that

the last line of the diagram is superfluous.

9 8 7 6 5 43 2 I W R*

3

~4

)<2

I Q*

I'~

X

I * 3987 I q2 W 2R

!

I 9 8 7 6 5 2)(4 Q* I II~I J )c I W R* 3 I 9 7 254

!:

2~6

15

Q* 3

!

1 ! J I I

X,

W R* I

9~465

!

:

l

4 8 7 5

X

6 4 Q* I I I I I X - J W R* 3 I 2 4 9~ 7~ 5

!

!!

5 9 6 7 Q* I ! I I ' X I W R* 3 459 687 I I I I I X )(' 6 3>s 2 4 5 6 9

X

7 8 QQ*

I

I

I 1

I

I X2 4 5 6 7 9 8 RR* I I

I

!

I

X

QQ* I 234 5 7 ~ 9 I 1 1 I 1 I 1 I I 234 5 6 7 8 9 Fig. 3.

6. A modification. Consider, instead of fig. 4, a case where we apply the same operations to a different top row. We specify that this top

row has the "medium" numbers n+I, .•• ,2n (here 5, ... ,8) in the middle, 1n some order, the "large" numbers 2n+I, ... ,3n on the left, in some

(7)

~ig. 4. 'ig. 5 • WaR

*

QQ

*

RR*

Q

Q*

RR*

w

Ilf

cf

w

2

lf

cf

W3

lf

cf

w

4

lf

cf

w

s

lf

cf

W6

lf

Q*

w

7

lf

rt

W R* 8

QQ*

RR*

QQ*

RR*

(8)

and the "small" numbers I, ••• ,n on the right, 1n some order. We take the example (9 II 12 10 7 5 8 6 3

4

2). The picture we get is different from fig. 4. But if in each row we only look at the set of places with large numbers, the set of places with medium numbers, the set of places with small numbers, we see that the figures are equal. This 1S easily seen from the following fact. Let us call two vectors equivalent if they have the same sets of places for large, medium and small numbers. (In this sense the first rows of figures 4 and 5 are equivalent). Then we have: if S 1S a miniswap, and a is equivalent to S, then Sa is equivalent to SS. It follows that, indeed, the i-th row in fig. 4 is equivalent to the i-th row in fig. 5.

As a result of this observation we note that in each case where some

W. is applied, either a small number jumps to the left over a sequence of

J

large and medium numbers, or a medium number jumps over a sequence of large numbers. This is obvious in fig.

4,

and we now know that it stays true in fig. 5 (because its rows are equivalent to those of fig. 4). In all cases a number Jumps over a sequence of larger numbers.

Let us now consider, instead of W. , the operation V. which is not

1 1

described as a miniswap sequence but as a cyclic permutation. If Va

=

S, then

SCi) = a(2n+I), S(i+l) = a(i), S(i+2) a ( i + I ) , ••. , S (2n+ I ) a(2n),

and S(j) = a(j) if j < i or j > 2n+ I.

In cases where W. has the effect that a number jumps over": larger numbers,

1

the effect of V. is equal to t"he one of l-l •• Therefore the operation

1 1

(5.2) sorts every array with the property that it has n large numbers, followed

by n medium numbers, followd by n small numbers. And if we omit Z from (5.2),

V1Z.

* *

*

V R Q V2 IR

2n n- Q V

*

2R Q V

* *

IR, (5.3)

this may leave the large and small numbers ill-sorted, but still sorts the medium numbers. We still end with an array formed by the small numbers (in some order), followed by the medium numbers (sorted), followed by the large

(9)

7. A simple sorting machine. In this section we describe a machine that does about the same thing as one of the machines of Armstrong and Rem. \-Je want to sort a sequence of n real numbers a , . .. ,a . In the procedure

I n

we use n copies of a number which is ~ all a. (we shall

1. call it m) and

n cop1.es of a number ~ all a. (we

1. call it M) • In the machine there 1.S an array ("the interior") of n places for real numbers, to the left there is an "input queue" and an "output queue". Between these queues and the interior we have a waiting room w with place for a single real number. We picture the machine in fig. 6.

Fig. 6.

During the execution the interior contains n numbers. The input queue starts with 2n numbers and ends with 0 numbers, the output queue starts with 0 and ends with 2n. The waiting room starts being empty.

The program consists of 2n-1 executions of {P

I;P2;P3;P4}, followed

by a single execution of {P

I;P2;P3}. As usua~ {PI ;P2;P3} means the execution of first PI' then P

2, then P3• And PI,P2,P3,P4 are defined as

follows.

PI. Push the rightmost number from the input queue into the waiting room.

P2. In the array of length n+l , formed by w+interior, execute the swapping program to be described by (7.1) .

P3. Push the number in the waiting room into the output queue.

P4. In the interior, execute the swapping program to be described by (7.2).

The swapping programs P

2 and P4 are as follows. (We give the index

o

to the waiting room, and the indices I, ..• ,n to the n places in the interior).

(10)

(7.) )

(7.2)

(where c 1S the largest odd number not exceeding n, and d is the largest

even number not exceeding n). Note that the swaps in (7.) can be executed simultaneously without changing the total effect, and that the same thing can be said for the swaps in (7.2).

At the start, i.e. before the first execution of {p);P2;P3;P4}, the input queue contains 2n numbers, viz. M,M, .•. ,M,a) , ... ,a

n. Further, the

waiting room is empty, and the interior contains rn, . .. ,m. We claim that after the 2n-th execution of {P);P

2;P3;P4} the output queue contains

m, .•. ,m,b), .•• ,b

n, where the b's satisfy b) $ ••• $ bn, and are obtained

from the a's by sorting .. Moreover, the interior contains M, •.• ,M.

We give an example of how the machine works, with n=3. The waiting room 1S empty each time just before execution of PI and just after exe

-cution of P3• We shall restrict the work state to the situations with

empty waiting room.

We present the initial state and the states we get from it by application of {p);P2;P 3}, P4, {Pt;P2;P3}, etc. in fig. 7. M M M 7 2

4

\

1m

m m] M M M 7

;~

14

m m} M M M 7

;1

1m

4

m)

---~ M t1J17r \2 m

1)

m mJ-M

M

~

JM /'

:3

_ _ ,u,ml...m m

2l

mrnm m m m 2 4 m m m 2 4 7

t1c~.2!.1

Fig. 7.

(11)

Indeed, (2,4,7) ~s the result of sorting (7,2,4).

8. A proof that the machine of section 7 sorts properly. To any state

of the machine (the word '~tate" taken in the sense of the end of section 4) we attach an array of length 3n just by concatenating output queue, input queue and interior (in this order). We show the arrays obtained from the example of section 4 in fig. 8

M M M 7 2 4 m m m 2 m M M M 7 2 4 m m 3 m M M M 7 2 m 4 m 4 m m MMM 7 2 m 4 5 m m M M M 7 m 2 4 6 m m m M M M 7 2 4 7 m m m M M M 2 7 4 8 m m m 2 MMM 4 7 9 m m m 2 M 11 4 M 7 10 m m m 2 4 M M 7 M 1 I m m m 2 4 M 7 M M 12 m m m 2 4 7 M M M Fig. 8.

Theprogram that leads from top row to bottom row is exactly the operation (5.3) (with n=3). In section 6 it was explained that it sorts the medium numbers. Thus we have shown that the machine of section 7 functions properly.

9. Modifying the order of operations. In connection with (7. 1) and (7.2)

we already remarked that certain swaps can be executed simultaneously without altering their total effect. And instead of simultaneous execution of these sets of swaps, we might as well execute them in any order we like. But we may do more: it is even possible to put some swaps of the second execution of {P

I;P2;P3;P4} before some swaps in the first execution of

{PI ;P

2;P3;P4}· Let us explain this by means of diagrams (cf. the "sorting networks" in Knuth [3 ] , section 5.3.4) . In these diagrams the vertical

(12)

lines correspond to the n positions in the interior of the machine. Horizontal connections between neighbouring lines are miniswaps. Time proceeds in the vertical direction from top to bottom. The horizontal lines to the left of the leftmost vertical line indicate

the operations S (which swap between the waiting room and the first

o '

interior position). Along segments of the vertical line corresponding

to a position we can write the content of that position. Input and output can be indicated over and under the horizontal lines on the left. This is done in fig. 9 for the case n=3, with the same input as in fig. 8.

In fig. 9 we have dotted two horizontal lines since they can be omitted

if we wish. They indicate swaps which are superfluous, whatever the input is. (In the case of general n, we can omit the first and the last horizontal line between the verticals 2,3,4, similarly the first two and the last two

horizontal lines between the verticals 4,5,6, etc.).

We can alter the order of the swapping operations by shifting the horizontal lines up and down as long as we do not alter the topology of the picture. What we keep is that along every vertical line (except the last one) we find horizontal lines to the left and to the right, strictly alternating. It is clear that if we start with content m,m,m and if we take input 4,2,7,M,M,M, the arrangement of fig. 10 becomes identical to the one of fig. 9 (apart from the topological distortions). It follows that the framework of fig. 10 (i.e. the system of horizontal and vertical lines) represents a machine that sorts every input sequence of length 3

(provided that the machine initially contains m's, and that the input

ends with 3 M's).

In fig. II we have pictured a possible diagram of the machine for

n=5. In fig. 12 we have a similar diagram, but it is special in the sense

that 1n every column the horizontal lines are equidistant. This figure

corresponds exactly to what happens in the machine of Arnstrong and Rem,

described in section 10.

10. Description of the machine ofArrnstrong and Rem. This description is

taken from [IJ, and is in terms of processing bit sequences. The real

numbers to be sorted are described as bit sequences of a fixed length w.

The sequences can be conceived as trains running through the machine,

passing various switches in its interior . The most significant bit of

(13)

m m m 4-+ m m m 4 ~ ~-'11 4

-

-

---+-m 4 m III /, III 4 2-+ m 4 m +-m 2 m 4 2-+ m +m 2 m 7-+ m 2 4 m 2 +-m 7 2 4 7-+ +m 7 4 M+ 2 7 4 2 4 +2 M 4 7 M+ 2 7 4 M-+ 4 M 7 +4 M 7 M +2 M 4 7 M-+ 4 M 7 +4 M 7 M M-+ 7 M M 1 - - - - M +7 M M M M-+ 7 +7 M Fig. 9 Fig. 10 I

--

-

- I---Fig. II Fig. 12

(14)

Special trains are m (the sequence 0 •.• 0) and M(the sequence 1 .•• 1). The layout of the rails is like in figure 13. It is pictured for the case n=3, corresponding to the example in section 7.

Fig. 13

The finite pieces of rails need not all have the same length (the length is the number of bits the piece of rails can carry). We require that the sum of the lengths of the two pieces of rails between two switches equals the word length.

The switches can be in positions "through" or "back" (fig. 14).

" t h r o u g h " X

"back"

A

Fig. 14

In fig. 14 we.have given names p,s to the incoming rails and q,r to the outgoing rails. The condition above on the lengths guaranteees that whenever a locomotive comes to a switch, there is a second locomotive at that switch at the same moment. The switches are operated as follows. If the two locomotives are equal (both 0 or 1), the position of the switch is irrelevant, and it stays irrelevant as long as the bit on p equals the one on s. But as soon as there arrives a 1 on p and a 0 on S, the switch is put ~n the "through" position, and it stays in that position until new locomotives arrive. Similarly, as soon as the first difference between p and s is that p has a 0 and q a 1, the switch ~s set to back, and it stays ~n that position until new locomotives arrive.

At the beginning, the interior of the machine ~s filled with zeros, on the input rails on the left we have the trains to be sorted, followed by M's, on the input rails on the right we have M's from the start. The effect of these M's on the right is that we can as well say that the right-most switch is permanently in the "back" position. Accordingly we omit that switch in what follows. Let us demonstrate the relation with the machine of section 7 by taking the same inputs as in the example of section 7.

(15)

\ole take word' length 4, and we give the finite pieces of rail all length 2. Th~ situation is pictured in fig. 15. HQads of trains 'are underlined.

1111111 11111111001000010

o

0 0 0 0 0

___

XX>O

o o

o

o

o

o

Fig. 15.

The three "rings" in this fig. 15 correspond to the three positions in the "interior" of the machine of fig. 7. l.;re still have to' describe what numbers have to be attached to these rings. A train is not always entirely within a single ring, but we notice that each ring always contains just one locomotive. This locomotive determines a train, and the binary number represented by that train ~s the number we attach to the ring.

During the procedure it may happen that a switch is set with the effect that an initial segment of a train is interchanged with a corres-ponding segment of another train. This can only occur in cases where these segments were equal, so we might as well ignore this effect.

The result of the switching rules is that the numbers attached to the rings are swapped: the smaller one goes to the left, the larger to the right. Although the setting of a switch may occur after the locomotives have passed, we may as well think that the switch is set just before the locomotives get to the switch.

In this example the first and third switch are passed by locomotives simultaneously, and we can also consider as simultaneous the swappings corresponding to those switches. The same thing holds for the second and fourth switch. Therefore the machine corresponds to the one of section 7.

However, if we alter the length of the rails, like in fig. 16, we get to the setup of section 9. Note that it is essential that the pieces of rails all have at least length 1.

I I I 1 I I I I I I I I I I I 0010000 1

a

a

0 0 0 0

---X:X:X:>

0 0 0 0 0 0 0

(16)

References.

I. P.N.Armstrongand M.Rem. A serial sorting machine. Report MR 78/3.

2. N.G. de Bruijn. Sorting by means of swappings. Discrete Math. 9, 333-339 (1974).

3. D.E.Knuth. The art of computer programming, Vol. 3: Sorting and searching (Addison-Wesley, Reading, Mass., 1973).

Referenties

GERELATEERDE DOCUMENTEN

Copyright and moral rights for the publications made accessible in the public portal are retained by the authors and/or other copyright owners and it is a condition of

Copyright and moral rights for the publications made accessible in the public portal are retained by the authors and/or other copyright owners and it is a condition of

Copyright and moral rights for the publications made accessible in the public portal are retained by the authors and/or other copyright owners and it is a condition of

Copyright and moral rights for the publications made accessible in the public portal are retained by the authors and/or other copyright owners and it is a condition of

Copyright and moral rights for the publications made accessible in the public portal are retained by the authors and/or other copyright owners and it is a condition of

Copyright and moral rights for the publications made accessible in the public portal are retained by the authors and/or other copyright owners and it is a condition of

Copyright and moral rights for the publications made accessible in the public portal are retained by the authors and/or other copyright owners and it is a condition of

Copyright and moral rights for the publications made accessible in the public portal are retained by the authors and/or other copyright owners and it is a condition of