A Lecture in CE Freshman Seminar Series:
Ten Puzzling Problems in Computer Engineering
May 2016 Sorting Networks Slide 1
This presentation belongs to the lecture series entitled
“Ten Puzzling Problems in Computer Engineering,” devised for a ten-week, one-unit, freshman seminar course by Behrooz Parhami, Professor of Computer Engineering at University of California, Santa Barbara. The material can be used freely in teaching and other educational settings.
Unauthorized uses, including any use for financial gain, are prohibited. © Behrooz Parhami
Edition Released Revised Revised Revised Revised
First May 2007 May 2008 May 2009 May 2010 May 2011
May 2012 May 2015 May 2016
May 2016 Sorting Networks Slide 2
Railroad Tracks and Switches
May 2016 Sorting Networks Slide 3
Coupling and Decoupling of Train Cars
Train cars and engines can be coupled and decoupled quickly
May 2016 Sorting Networks
An engine can push a string of cars, or pull a desired subset by decoupling them from the rest
Slide 4
Railroad Yards Have Many Tracks and Switches
May 2016 Sorting Networks Slide 5
Rearranging Trains
Train cars
B A D C
Engine
Track
Sorted order D C
Stub or lead
B A
B A D C
Stack or LIFO data structure in CE
Sorting algorithm: Assemble train in stub, beginning with the last car
Repeat: If the next car is X, decouple train after X, push X into stub
Stub or lead
D C
B A
B A D C
Stub or lead
May 2016
C A B D
Queue or FIFO
Sorting Networks
Siding
Slide 6
Goleta Amtrak Station and Its Siding
May 2016 Sorting Networks Slide 7
May 2016 Sorting Networks Slide 8
Rearrangement with Change of Direction
A B C D
B A D C
C A B D
What types of sorting are possible with a turnaround loop? A wye?
C A
Configuration after the first few steps in sorting the train
Turnaround loop
Wye
Complete the sorting process, assuming that it is okay for the engine to end up behind the train
May 2016 Sorting Networks Slide 9
May 2016 Sorting Networks Slide 10
Delivering Train Cars in a Specific Order
Cars in the train below have been sorted according to their delivery points.
However, it is still nontrivial to deposit car A in stub 1, car B in stub 2, and car C in siding 3. Cars can be pulled or pushed by the engine.
2
C B A D
1
3
2
C B A D
3
1
2
C B A
1
3
Is there a better initial ordering of the cars for the deliveries in this puzzle?
May 2016 Sorting Networks Slide 11
Train Passing Puzzle
The trains below must pass each other using a siding that can hold only one car or one engine. Show how this can be done.
B A 1 2
B A
B
B 1 2
1 2
A
A
1 2
2 A
1 2 A
B
Sorting Networks May 2016 Slide 12
Fast Combining or Reordering of Train Cars
C A B D
Forming multiple trains from incoming cars
C A B D
Train reordering via a set of stubs
Cars are pushed or pulled by an engine
Alternatively, movement in one direction may be achieved via sloped rails
Switches used to be adjusted manually, but nowadays, electronic control is used
May 2016 Sorting Networks Slide 13
1
3
4
2
Target track
1
3
4
2
Sorting Train Cars in Parallel
2 2 1
4
1
3
1
4
3
2
3
4
Unconditional exchange Compare-exchange
2
4
1
3
The net in stick diagram schematic
Track 1
Track 2
Track 3
Track 4
2
4
1
3
Track 1
Track 2
Track 3
Track 4
Is adding this compare-exchange element sufficient for producing a valid sorting network?
Sorting Networks Slide 14 May 2016
c d a b
Validating a Sorting Network min( a , b ) max( a , b ) min( c , d ) max( c , d )
In the example above, it was fairly easy to show the validity of the sorting network.
Generally, it is much more difficult min( a , b , c , d )
????
????
max( a , b , c , d )
Track 1
Track 2
Track 3
Track 4
Stick diagram schematic
How would one establish the validity of this 16-input sorting network?
More importantly, how does one come up with this design in the first place?
Slide 15 May 2016 Sorting Networks
The Zero-One Principle
A sorting net built of comparators is valid if it correctly sorts all 0-1 sequences
0
0
0
0
0
0
0
0
0
0
0
1
0
0
0
1
0
0
1
0
0
0
0
1
0
0
1
1
0
0
1
1
1
0
0
0
0
1
0
0
1
0
0
1
0
1
0
1
0
0
0
1
0
0
0
1
1
0
1
0
0
1
1
0
0
0
1
1
0
0
1
1
1
0
1
1
0
1
1
1
0
0
1
1
0
0
1
1
1
1
0
0
0
0
1
1
1
1
0
1
0
1
1
1
1
1
1
0
0
1
1
1
1
1
1
1
So, we can validate a sorting network using 2 n rather than n ! input patterns
1
1
1
1 n = 12: 2 n = 4096, n ! = 479,001,600 (thousands vs. half a billion)
0
1
1
1
0
1
1
1
May 2016 Sorting Networks Slide 16
A 16-Input Sorting Network
Use 4-input sorters, follow by (4, 4)-mergers, and end with an (8, 8)-merger
11
13
3
0
4
15
9
1
6
14
2
7
5
10
8
12
0
3
11
13
1
4
9
15
2
6
7
14
5
8
10
12
9
11
13
15
0
1
3
4
8
10
12
14
2
5
6
7
12
13
14
15
8
9
10
11
4
5
6
7
0
1
2
3
Using the 0-1 principle, we can validate this network via 16 + 25 + 81 tests
4-sorter tests (4, 4)-merger tests
Sorting Networks May 2016
(8, 8)-merger tests
Slide 17
Insertion Sort and Selection Sort x
0 x
1 x
2 x n–2 x n–1
.
.
.
(n–1)-sorter
.
.
. y
0
.
.
. y
1 y
2 x
0 x
1 x
2
(n–1)-sorter
.
.
. y n–2 x n–2 y n–1 x n–1
Insertion sort
Insertion sort
Selection sort
Selection sort
Parallel ins ertio n s ort = Parallel s election sort = Parallel bub ble so rt!
3 3 3 0 0 0 0 0 0 0
.
.
.
C ( n ) = n ( n – 1)/2
D ( n ) = 2 n – 3
Cost
Delay
=
Q
( n 3 )
4
0
5
2
1
4
0
0
4
5
3
4
5
2
3
4
2
5
1
3
2
4
1
5
2
3
1
4
2
1
3
1
2
1
2
3
4
5 y
0 y
1 y
2 y n–2 y n–1
Fig. 7.8 Sorting network based on insertion sort or selection sort.
May 2016 Sorting Networks Slide 18
n = 9, 25 mod ules, 9 levels n = 12, 39 modules , 9 levels
The Best Sorting Networks n = 6, 12 modules , 5 levels
Which
10-input sorting network is better?
n = 9, 25 modules , 8 levels
Cost
Delay = 29
9 = 261 Cost
Delay = 31
7 = 217
Criterion 1: The number of sticks or compare-exchange blocks (cost)
Criterion 2: The number of compare-exchanges in sequence (delay)
May 2016
The most cost-effective n -input sorting network may be n = 16, 61 mo dules, 9 lev els neither the fastest design, nor the lowest-cost design
Sorting Networks Slide 19
x
0 x
1 x
2 x
3
Electronic Sorting Networks
Electronic sorting networks are built of 2-sorters, building blocks that can sort two inputs a min( a , b ) b max( a , b ) y
0 y
1 y
2 y
3
3
2
5
1
2
3
1
5
1
3
2
5
1
2
3
5
Applications of sorting networks:
Directing information packets to their destinations in a network router
Connecting n processors to n memory modules in a parallel computer
May 2016 Sorting Networks Slide 20