Top Banner
Sorting Networks 6CCS3PAL-7CCSMPDA
66

Sorting Networks - King's College LondonA sorting network is a special kind of sorting algorithm, where the sequence of comparisons is not data-dependent. This makes sorting networks

May 11, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Sorting Networks - King's College LondonA sorting network is a special kind of sorting algorithm, where the sequence of comparisons is not data-dependent. This makes sorting networks

Sorting Networks

6CCS3PAL-7CCSMPDA

Page 2: Sorting Networks - King's College LondonA sorting network is a special kind of sorting algorithm, where the sequence of comparisons is not data-dependent. This makes sorting networks

Sorting networks

I Comparator

I Comparator network

I Sorting network

I 0-1 Principle

I Various sorts

I Bitonic sort

Page 3: Sorting Networks - King's College LondonA sorting network is a special kind of sorting algorithm, where the sequence of comparisons is not data-dependent. This makes sorting networks

Introduction

A sorting network consists of two types of items: comparatorsand wires. The wires are thought of as running from left toright, carrying values (one per wire) that traverse the networkall at the same time. Each comparator connects two wires.

Page 4: Sorting Networks - King's College LondonA sorting network is a special kind of sorting algorithm, where the sequence of comparisons is not data-dependent. This makes sorting networks

When a pair of values, traveling through a pair of wires,encounter a comparator, the comparator swaps the values ifand only if the top wire’s value is greater than the bottomwire’s value.

If the top wire carries x and the bottom wire carries y,then after hitting the comparatorthe top wire carries x ′ = min(x , y)x ′ = min(x , y)and the bottom one y ′ = max(x , y)y ′ = max(x , y)

Page 5: Sorting Networks - King's College LondonA sorting network is a special kind of sorting algorithm, where the sequence of comparisons is not data-dependent. This makes sorting networks

Comparator

Typically these diagrams assume an ascending comparator(sorts min,max)

But sometimes we will use a descending comparator (sortsmax,min)

Page 6: Sorting Networks - King's College LondonA sorting network is a special kind of sorting algorithm, where the sequence of comparisons is not data-dependent. This makes sorting networks

Introduction

A network of wires and comparators that correctly sorts allpossible inputs into ascending order is called a sorting network.A simple sorting network is shown below.The first four comparators ”sink” the largest value to thebottom and ”float” the smallest value to the top. The finalcomparator sorts out the middle two wires.

Page 7: Sorting Networks - King's College LondonA sorting network is a special kind of sorting algorithm, where the sequence of comparisons is not data-dependent. This makes sorting networks

3 3 3 1 1 1

2 2 1 3 2 2

4 4 4 2 3 3

1 1 2 4 4 4

Page 8: Sorting Networks - King's College LondonA sorting network is a special kind of sorting algorithm, where the sequence of comparisons is not data-dependent. This makes sorting networks

Sort 3 numbers?

I The basic comparator sorts 2 numbers

I Can you think of a network to sort 3 numbers correctly?

I And how do you know it is correct?

Page 9: Sorting Networks - King's College LondonA sorting network is a special kind of sorting algorithm, where the sequence of comparisons is not data-dependent. This makes sorting networks

Sort 3 numbers?

I The basic comparator sorts 2 numbers

I Can you think of a network to sort 3 numbers correctly?

I And how do you know it is correct?

Page 10: Sorting Networks - King's College LondonA sorting network is a special kind of sorting algorithm, where the sequence of comparisons is not data-dependent. This makes sorting networks

Sort 3 numbers?

I The basic comparator sorts 2 numbers

I Can you think of a network to sort 3 numbers correctly?

I And how do you know it is correct?

Page 11: Sorting Networks - King's College LondonA sorting network is a special kind of sorting algorithm, where the sequence of comparisons is not data-dependent. This makes sorting networks

Sort 3 numbers correctly

------.------------.-----

| |

------.------.-----.-----

|

-------------.-----------

Check all?123, 132, 213, 231, 312, 321

Page 12: Sorting Networks - King's College LondonA sorting network is a special kind of sorting algorithm, where the sequence of comparisons is not data-dependent. This makes sorting networks

Sort 3 numbers correctly

------.------------.-----

| |

------.------.-----.-----

|

-------------.-----------

Check all?123, 132, 213, 231, 312, 321

Page 13: Sorting Networks - King's College LondonA sorting network is a special kind of sorting algorithm, where the sequence of comparisons is not data-dependent. This makes sorting networks

312

------.------------.-----

| |

------.------.-----.-----

|

-------------.-----------

3 1 1

1 3 2

2 2 3

Page 14: Sorting Networks - King's College LondonA sorting network is a special kind of sorting algorithm, where the sequence of comparisons is not data-dependent. This makes sorting networks

321

------.------------.-----

| |

------.------.-----.-----

|

-------------.-----------

3 2 2 1

2 3 1 2

1 1 3 3

Check 231

Page 15: Sorting Networks - King's College LondonA sorting network is a special kind of sorting algorithm, where the sequence of comparisons is not data-dependent. This makes sorting networks

Parallel sorting: comparator stages

We label the n wires 0, 1, ..., n − 1A comparator [i : j ] sorts the i–th and the j–th element of adata sequence into nondecreasing order.A comparator stage is a sequential collection S of comparatorsS = [i1 : j1]...[ik : jk ] , such that all ir and js are distinct (allstart and end wires are distinct, and no other comparators inbetween).Comparators within a comparator stage can be executed inparallel.

Page 16: Sorting Networks - King's College LondonA sorting network is a special kind of sorting algorithm, where the sequence of comparisons is not data-dependent. This makes sorting networks

How many stages does the following figure have?

The figure has 2 stages.

Page 17: Sorting Networks - King's College LondonA sorting network is a special kind of sorting algorithm, where the sequence of comparisons is not data-dependent. This makes sorting networks

How many stages does the following figure have?

The figure has 2 stages.

Page 18: Sorting Networks - King's College LondonA sorting network is a special kind of sorting algorithm, where the sequence of comparisons is not data-dependent. This makes sorting networks

How many stages?

------.------------.-----

| |

------.------.-----.-----

|

-------------.-----------

3 stages

Page 19: Sorting Networks - King's College LondonA sorting network is a special kind of sorting algorithm, where the sequence of comparisons is not data-dependent. This makes sorting networks

A comparator network is a composition of comparator stages.

A sorting network is a comparator network that sorts all inputsequences.The comparator network of the example above is not a sortingnetwork, since it does not (e.g.) sort the sequences 3 1 4 2 or3 2 4 1It is the same as the introductory network, but the lastcomparator is missing

Page 20: Sorting Networks - King's College LondonA sorting network is a special kind of sorting algorithm, where the sequence of comparisons is not data-dependent. This makes sorting networks

Sorting network: Bubblesort

An example of a sorting network

The Bubblesort network has a first diagonal of n − 1comparators to move the greatest element to the last positionThe remaining n − 1 elements are sorted recursively byapplying the same procedure The second diagonal movessecond largest to second from last position etc

Page 21: Sorting Networks - King's College LondonA sorting network is a special kind of sorting algorithm, where the sequence of comparisons is not data-dependent. This makes sorting networks

Bubblesort has n(n − 1)/2 comparators, and 2n − 3comparator stages.The figure shows making BS(6) from BS(5) by adding the topline of comparators.One new one at each end (9 stages from 7)

Page 22: Sorting Networks - King's College LondonA sorting network is a special kind of sorting algorithm, where the sequence of comparisons is not data-dependent. This makes sorting networks

3 stage Bubblesort

------.------------.-----

| |

------.------.-----.-----

|

-------------.-----------

Page 23: Sorting Networks - King's College LondonA sorting network is a special kind of sorting algorithm, where the sequence of comparisons is not data-dependent. This makes sorting networks

Bubblesort: A simple compare-exchange sort

This is Bubblesort from an Interconnection Networks lecture

Algorithm Bubble-Sort (A[1..n])for i = 1...n − 1 do

for j = 1...n − 1 doif A[j ] > A[j + 1] then

switch entries of A[j ] and A[j + 1]end Bubble-Sort

To keep the code simple we included more comparisons thenwe needed

Page 24: Sorting Networks - King's College LondonA sorting network is a special kind of sorting algorithm, where the sequence of comparisons is not data-dependent. This makes sorting networks

For n = 3 the code says

for i=1 compare 12 23

for i=2 compare 12 23

------.------------.----------

| |

------.------.-----.-----.----

| |

-------------.-----------.----

We used 4 comparators and stages. We only needed 3

------.------------.-----

| |

------.------.-----.-----

|

-------------.-----------

Page 25: Sorting Networks - King's College LondonA sorting network is a special kind of sorting algorithm, where the sequence of comparisons is not data-dependent. This makes sorting networks

Odd-even transposition sort

The network odd-even transposition sort for n input dataconsists of n comparator stages.At each stage, either all inputs at odd index positions or allinputs at even index positions are compared with theirneighbours.Odd and even stages alternate.

We used this network for sorting on 1-D meshes

Page 26: Sorting Networks - King's College LondonA sorting network is a special kind of sorting algorithm, where the sequence of comparisons is not data-dependent. This makes sorting networks

Remember this?

Even-Odd Transposition Sort on 1− D Mesh

Algorithm Even-Odd-Sort-1D-MeshL[0..n − 1])For Step s = 0..n − 1 do

If Step s Even thenFor all Even processor labels i in parallel do

Compare-Exchange (L[i ], L[i + 1])else

If Step s Odd thenFor all Odd processor labels i in parallel do

Compare-Exchange (L[i ], L[i + 1])end algorithm

Page 27: Sorting Networks - King's College LondonA sorting network is a special kind of sorting algorithm, where the sequence of comparisons is not data-dependent. This makes sorting networks

I Q: How do we know these sorting networks really workproperly?

I A: It would need to be proved

I Q: How could we do that?

I A: The 0–1 principle can be used to check the correctnessof sorting networks

I But it is not an easy question to answer

Page 28: Sorting Networks - King's College LondonA sorting network is a special kind of sorting algorithm, where the sequence of comparisons is not data-dependent. This makes sorting networks

I Q: How do we know these sorting networks really workproperly?

I A: It would need to be proved

I Q: How could we do that?

I A: The 0–1 principle can be used to check the correctnessof sorting networks

I But it is not an easy question to answer

Page 29: Sorting Networks - King's College LondonA sorting network is a special kind of sorting algorithm, where the sequence of comparisons is not data-dependent. This makes sorting networks

I Q: How do we know these sorting networks really workproperly?

I A: It would need to be proved

I Q: How could we do that?

I A: The 0–1 principle can be used to check the correctnessof sorting networks

I But it is not an easy question to answer

Page 30: Sorting Networks - King's College LondonA sorting network is a special kind of sorting algorithm, where the sequence of comparisons is not data-dependent. This makes sorting networks

I Q: How do we know these sorting networks really workproperly?

I A: It would need to be proved

I Q: How could we do that?

I A: The 0–1 principle can be used to check the correctnessof sorting networks

I But it is not an easy question to answer

Page 31: Sorting Networks - King's College LondonA sorting network is a special kind of sorting algorithm, where the sequence of comparisons is not data-dependent. This makes sorting networks

I Q: How do we know these sorting networks really workproperly?

I A: It would need to be proved

I Q: How could we do that?

I A: The 0–1 principle can be used to check the correctnessof sorting networks

I But it is not an easy question to answer

Page 32: Sorting Networks - King's College LondonA sorting network is a special kind of sorting algorithm, where the sequence of comparisons is not data-dependent. This makes sorting networks

The 0-1-principle

Whether an arbitrary comparator network is a sorting networkor not is independent of the input setIt only depends on the structure of the network. The0-1-principle essentially states this fact.Theorem: (The 0-1-principle)A comparator network with n inputs that sorts all 2n

sequences of zeroes and ones is a sorting network (i.e. it sortsall sequences of arbitrary values, too).

Page 33: Sorting Networks - King's College LondonA sorting network is a special kind of sorting algorithm, where the sequence of comparisons is not data-dependent. This makes sorting networks

Theorem: (The 0-1-principle)

A comparator network with n inputs that sorts all 2n

sequences of zeroes and ones is a sorting network (i.e. it sortsall sequences of arbitrary values, too).Basic idea: if a < b but we get mistake ....b, a... in sortedsequence, then put all numbers a less than b to zero and allnumbers ≥ b to 1. The 0 for a and 1 for b are still out ofplace when we sort the 0–1 sequence

Page 34: Sorting Networks - King's College LondonA sorting network is a special kind of sorting algorithm, where the sequence of comparisons is not data-dependent. This makes sorting networks

Theorem: (The 0-1-principle)

Why useful?

A comparator network with n inputs that sorts all 2n

sequences of zeroes and ones is a sorting networkWe can check 2n inputs, or prove the network is ok for 0− 1inputs but we can’t check all sequences of n numbers (Whynot?)

Page 35: Sorting Networks - King's College LondonA sorting network is a special kind of sorting algorithm, where the sequence of comparisons is not data-dependent. This makes sorting networks

Theorem: (The 0-1-principle)

Why useful?A comparator network with n inputs that sorts all 2n

sequences of zeroes and ones is a sorting network

We can check 2n inputs, or prove the network is ok for 0− 1inputs but we can’t check all sequences of n numbers (Whynot?)

Page 36: Sorting Networks - King's College LondonA sorting network is a special kind of sorting algorithm, where the sequence of comparisons is not data-dependent. This makes sorting networks

Theorem: (The 0-1-principle)

Why useful?A comparator network with n inputs that sorts all 2n

sequences of zeroes and ones is a sorting networkWe can check 2n inputs, or prove the network is ok for 0− 1inputs but we can’t check all sequences of n numbers (Whynot?)

Page 37: Sorting Networks - King's College LondonA sorting network is a special kind of sorting algorithm, where the sequence of comparisons is not data-dependent. This makes sorting networks

Exercise: Check for all 0− 1 sequences

------.------------.-----

| |

------.------.-----.-----

|

-------------.-----------

We only need to check 100, 010, 101, 110.Why not 000, 001, 011, 111?

Page 38: Sorting Networks - King's College LondonA sorting network is a special kind of sorting algorithm, where the sequence of comparisons is not data-dependent. This makes sorting networks

Bitonic sort

A sorting network is a special kind of sorting algorithm, wherethe sequence of comparisons is not data-dependent.This makes sorting networks suitable for implementation inhardware or in parallel processor arrays.Bitonic sort [Batcher 1968] is one of the fastest sortingnetworks.The sorting network bitonic sort consists of O(nlog 2(n))comparators and sorts in O(log 2(n)) parallel time (span).The term bitonic is a technical term meaning that thesequence is twofold monotonic

Page 39: Sorting Networks - King's College LondonA sorting network is a special kind of sorting algorithm, where the sequence of comparisons is not data-dependent. This makes sorting networks

Bitonic sequence

A 0-1-sequence is called bitonic, if it contains at most twochanges between 0 and 1i.e. if there exist subsequence lengths k ,m such thata0, ..., ak−1 = 0, ak , ..., am−1 = 1, am, ..., an−1 = 0 ora0, ..., ak−1 = 1, ak , ..., am−1 = 0, am, ..., an−1 = 1

Page 40: Sorting Networks - King's College LondonA sorting network is a special kind of sorting algorithm, where the sequence of comparisons is not data-dependent. This makes sorting networks

A special network Bn

B2 is just the basic comparator.

For n even the comparator network Bn is defined as follows:

Bn = [0 : n/2] [1 : n/2 + 1] ... [n/2− 1 : n − 1]

Example: n = 8 so n/2 = 4 and edges 04, 15, 26, 37

Page 41: Sorting Networks - King's College LondonA sorting network is a special kind of sorting algorithm, where the sequence of comparisons is not data-dependent. This makes sorting networks

Theorem

For n even, let a = a0, ..., an−1 be a bitonic 0-1-sequence.

I Application of comparator network Bn to a yieldsBn(a) = b0, ..., bn/2−1, c0, ..., cn/2−1 where:

I All bi are less than or equal to all cj ,i.e. bi ≤ cj for all i , j ∈ {0, ..., n/2− 1}

I Both halfs are bitonicb0, ..., bn/2−1 is bitonic and c0, ..., cn/2−1 is bitonic

I The theorem says: Afterwards at least one half iscompletely sorted (all 0 or all 1) Why?

I Either the initial sequence a was all 0 or all 1, or

I Either the first half of the final sequence is all 0, or thesecond half is all 1

I It also says we can reapply Bn/2 to the final half sequences

Page 42: Sorting Networks - King's College LondonA sorting network is a special kind of sorting algorithm, where the sequence of comparisons is not data-dependent. This makes sorting networks

Theorem

For n even, let a = a0, ..., an−1 be a bitonic 0-1-sequence.

I Application of comparator network Bn to a yieldsBn(a) = b0, ..., bn/2−1, c0, ..., cn/2−1 where:

I All bi are less than or equal to all cj ,i.e. bi ≤ cj for all i , j ∈ {0, ..., n/2− 1}

I Both halfs are bitonicb0, ..., bn/2−1 is bitonic and c0, ..., cn/2−1 is bitonic

I The theorem says: Afterwards at least one half iscompletely sorted (all 0 or all 1) Why?

I Either the initial sequence a was all 0 or all 1, or

I Either the first half of the final sequence is all 0, or thesecond half is all 1

I It also says we can reapply Bn/2 to the final half sequences

Page 43: Sorting Networks - King's College LondonA sorting network is a special kind of sorting algorithm, where the sequence of comparisons is not data-dependent. This makes sorting networks

Theorem

For n even, let a = a0, ..., an−1 be a bitonic 0-1-sequence.

I Application of comparator network Bn to a yieldsBn(a) = b0, ..., bn/2−1, c0, ..., cn/2−1 where:

I All bi are less than or equal to all cj ,i.e. bi ≤ cj for all i , j ∈ {0, ..., n/2− 1}

I Both halfs are bitonicb0, ..., bn/2−1 is bitonic and c0, ..., cn/2−1 is bitonic

I The theorem says: Afterwards at least one half iscompletely sorted (all 0 or all 1) Why?

I Either the initial sequence a was all 0 or all 1, or

I Either the first half of the final sequence is all 0, or thesecond half is all 1

I It also says we can reapply Bn/2 to the final half sequences

Page 44: Sorting Networks - King's College LondonA sorting network is a special kind of sorting algorithm, where the sequence of comparisons is not data-dependent. This makes sorting networks

Theorem

For n even, let a = a0, ..., an−1 be a bitonic 0-1-sequence.

I Application of comparator network Bn to a yieldsBn(a) = b0, ..., bn/2−1, c0, ..., cn/2−1 where:

I All bi are less than or equal to all cj ,i.e. bi ≤ cj for all i , j ∈ {0, ..., n/2− 1}

I Both halfs are bitonicb0, ..., bn/2−1 is bitonic and c0, ..., cn/2−1 is bitonic

I The theorem says: Afterwards at least one half iscompletely sorted (all 0 or all 1) Why?

I Either the initial sequence a was all 0 or all 1, or

I Either the first half of the final sequence is all 0, or thesecond half is all 1

I It also says we can reapply Bn/2 to the final half sequences

Page 45: Sorting Networks - King's College LondonA sorting network is a special kind of sorting algorithm, where the sequence of comparisons is not data-dependent. This makes sorting networks

Theorem

For n even, let a = a0, ..., an−1 be a bitonic 0-1-sequence.

I Application of comparator network Bn to a yieldsBn(a) = b0, ..., bn/2−1, c0, ..., cn/2−1 where:

I All bi are less than or equal to all cj ,i.e. bi ≤ cj for all i , j ∈ {0, ..., n/2− 1}

I Both halfs are bitonicb0, ..., bn/2−1 is bitonic and c0, ..., cn/2−1 is bitonic

I The theorem says: Afterwards at least one half iscompletely sorted (all 0 or all 1) Why?

I Either the initial sequence a was all 0 or all 1, or

I Either the first half of the final sequence is all 0, or thesecond half is all 1

I It also says we can reapply Bn/2 to the final half sequences

Page 46: Sorting Networks - King's College LondonA sorting network is a special kind of sorting algorithm, where the sequence of comparisons is not data-dependent. This makes sorting networks

Theorem

For n even, let a = a0, ..., an−1 be a bitonic 0-1-sequence.

I Application of comparator network Bn to a yieldsBn(a) = b0, ..., bn/2−1, c0, ..., cn/2−1 where:

I All bi are less than or equal to all cj ,i.e. bi ≤ cj for all i , j ∈ {0, ..., n/2− 1}

I Both halfs are bitonicb0, ..., bn/2−1 is bitonic and c0, ..., cn/2−1 is bitonic

I The theorem says: Afterwards at least one half iscompletely sorted (all 0 or all 1) Why?

I Either the initial sequence a was all 0 or all 1, or

I Either the first half of the final sequence is all 0, or thesecond half is all 1

I It also says we can reapply Bn/2 to the final half sequences

Page 47: Sorting Networks - King's College LondonA sorting network is a special kind of sorting algorithm, where the sequence of comparisons is not data-dependent. This makes sorting networks

Theorem

For n even, let a = a0, ..., an−1 be a bitonic 0-1-sequence.

I Application of comparator network Bn to a yieldsBn(a) = b0, ..., bn/2−1, c0, ..., cn/2−1 where:

I All bi are less than or equal to all cj ,i.e. bi ≤ cj for all i , j ∈ {0, ..., n/2− 1}

I Both halfs are bitonicb0, ..., bn/2−1 is bitonic and c0, ..., cn/2−1 is bitonic

I The theorem says: Afterwards at least one half iscompletely sorted (all 0 or all 1) Why?

I Either the initial sequence a was all 0 or all 1, or

I Either the first half of the final sequence is all 0, or thesecond half is all 1

I It also says we can reapply Bn/2 to the final half sequences

Page 48: Sorting Networks - King's College LondonA sorting network is a special kind of sorting algorithm, where the sequence of comparisons is not data-dependent. This makes sorting networks

Theorem

For n even, let a = a0, ..., an−1 be a bitonic 0-1-sequence.

I Application of comparator network Bn to a yieldsBn(a) = b0, ..., bn/2−1, c0, ..., cn/2−1 where:

I All bi are less than or equal to all cj ,i.e. bi ≤ cj for all i , j ∈ {0, ..., n/2− 1}

I Both halfs are bitonicb0, ..., bn/2−1 is bitonic and c0, ..., cn/2−1 is bitonic

I The theorem says: Afterwards at least one half iscompletely sorted (all 0 or all 1) Why?

I Either the initial sequence a was all 0 or all 1, or

I Either the first half of the final sequence is all 0, or thesecond half is all 1

I It also says we can reapply Bn/2 to the final half sequences

Page 49: Sorting Networks - King's College LondonA sorting network is a special kind of sorting algorithm, where the sequence of comparisons is not data-dependent. This makes sorting networks

B16 acting on bitonic 0–1-sequences

0’s are drawn white and 1’s gray.

Page 50: Sorting Networks - King's College LondonA sorting network is a special kind of sorting algorithm, where the sequence of comparisons is not data-dependent. This makes sorting networks

How to use this?

I Bn is sometimes called a half cleaner because afterwardshalf the sequence is clean

I First we need to build a bitonic sequence of length n(Bitonic Build)

I Then we need to merge the bitonic sequence correctly byBn followed by two parallel Bn/2 etc (Bitonic Merge)

I In summary. Build a bitonic sequence and sort it using abitonic merging network

I In both cases we use Bn. There are several ways to putthem together

Page 51: Sorting Networks - King's College LondonA sorting network is a special kind of sorting algorithm, where the sequence of comparisons is not data-dependent. This makes sorting networks

How to use this?

I Bn is sometimes called a half cleaner because afterwardshalf the sequence is clean

I First we need to build a bitonic sequence of length n(Bitonic Build)

I Then we need to merge the bitonic sequence correctly byBn followed by two parallel Bn/2 etc (Bitonic Merge)

I In summary. Build a bitonic sequence and sort it using abitonic merging network

I In both cases we use Bn. There are several ways to putthem together

Page 52: Sorting Networks - King's College LondonA sorting network is a special kind of sorting algorithm, where the sequence of comparisons is not data-dependent. This makes sorting networks

How to use this?

I Bn is sometimes called a half cleaner because afterwardshalf the sequence is clean

I First we need to build a bitonic sequence of length n(Bitonic Build)

I Then we need to merge the bitonic sequence correctly byBn followed by two parallel Bn/2 etc (Bitonic Merge)

I In summary. Build a bitonic sequence and sort it using abitonic merging network

I In both cases we use Bn. There are several ways to putthem together

Page 53: Sorting Networks - King's College LondonA sorting network is a special kind of sorting algorithm, where the sequence of comparisons is not data-dependent. This makes sorting networks

How to use this?

I Bn is sometimes called a half cleaner because afterwardshalf the sequence is clean

I First we need to build a bitonic sequence of length n(Bitonic Build)

I Then we need to merge the bitonic sequence correctly byBn followed by two parallel Bn/2 etc (Bitonic Merge)

I In summary. Build a bitonic sequence and sort it using abitonic merging network

I In both cases we use Bn. There are several ways to putthem together

Page 54: Sorting Networks - King's College LondonA sorting network is a special kind of sorting algorithm, where the sequence of comparisons is not data-dependent. This makes sorting networks

How to use this?

I Bn is sometimes called a half cleaner because afterwardshalf the sequence is clean

I First we need to build a bitonic sequence of length n(Bitonic Build)

I Then we need to merge the bitonic sequence correctly byBn followed by two parallel Bn/2 etc (Bitonic Merge)

I In summary. Build a bitonic sequence and sort it using abitonic merging network

I In both cases we use Bn. There are several ways to putthem together

Page 55: Sorting Networks - King's College LondonA sorting network is a special kind of sorting algorithm, where the sequence of comparisons is not data-dependent. This makes sorting networks

Example: Action of Bn

27.3 A bitonic sorting network 715

0

0

0

0

1

0

1

1

0

0

0

0

1

0

1

1

0

0

1

1

1

0

0

0

0

0

0

0

0

1

1

1

(b)(a)

bitonic sorted

BITONIC-SORTER[n/2]

HALF-CLEANER[n]

BITONIC-SORTER[n/2]

Figure 27.9 The comparison network BITONIC-SORTER[n], shown here for n 8. (a) The re-cursive construction: HALF-CLEANER[n] followed by two copies of BITONIC-SORTER[n 2] thatoperate in parallel. (b) The network after unrolling the recursion. Each half-cleaner is shaded. Sam-ple zero-one values are shown on the wires.

The bitonic sorter

By recursively combining half-cleaners, as shown in Figure 27.9, we can builda bitonic sorter, which is a network that sorts bitonic sequences. The rst st ageof BITONIC-SORTER[n] consists of HALF-CLEANER[n], which, by Lemma 27.3,produces two bitonic sequences of half the size such that eve ry element in thetop half is at least as small as every element in the bottom half. Thus, we cancomplete the sort by using two copies of BITONIC-SORTER[n 2] to sort the twohalves recursively. In Figure 27.9(a), the recursion has be en shown explicitly, andin Figure 27.9(b), the recursion has been unrolled to show th e progressively smallerhalf-cleaners that make up the remainder of the bitonic sort er. The depth D n ofBITONIC-SORTER[n] is given by the recurrence

D n0 if n 1D n 2 1 if n 2k and k 1

whose solution is D n lg n.Thus, a zero-one bitonic sequence can be sorted by B ITONIC-SORTER, which

has a depth of lg n. It follows by the analog of the zero-one principle given asExercise 27.3-6 that any bitonic sequence of arbitrary numb ers can be sorted bythis network.

Exercises

27.3-1How many zero-one bitonic sequences of length n are there?

In this course Bitonic-Sorter [CLRS] is called Bitonic MergeFigure from another version of CLRS Chapter 27 given atmitpress.mit.edu/sites/default/files/Chapter%2027.pdf

Page 56: Sorting Networks - King's College LondonA sorting network is a special kind of sorting algorithm, where the sequence of comparisons is not data-dependent. This makes sorting networks

Bitonic sort n = 8

Page 57: Sorting Networks - King's College LondonA sorting network is a special kind of sorting algorithm, where the sequence of comparisons is not data-dependent. This makes sorting networks

Bitonic sort n = 2

This B2 is ⊕B2,

This one is B2

Page 58: Sorting Networks - King's College LondonA sorting network is a special kind of sorting algorithm, where the sequence of comparisons is not data-dependent. This makes sorting networks

Bitonic sort n = 4

The logicSequence a bitonic

b ≤ c bitonicd sorted within b, c

⊕B2 B4B2

B2 B2

Build Merge

Page 59: Sorting Networks - King's College LondonA sorting network is a special kind of sorting algorithm, where the sequence of comparisons is not data-dependent. This makes sorting networks

Bitonic sort n = 4

Write BM[2] = B2, and for n = 4, BM[4] is

BM[4] = B4B2

B2

⊕BM[2]BM[4]BM[2]

Build Merge

Page 60: Sorting Networks - King's College LondonA sorting network is a special kind of sorting algorithm, where the sequence of comparisons is not data-dependent. This makes sorting networks

Bitonic sort n = 8

⊕B2 ⊕B4⊕B2

B8

B4B2

B2 ⊕B2 B2

⊕B2 B4B2 B4

B2

B2 B2 B2

Build Merge

Page 61: Sorting Networks - King's College LondonA sorting network is a special kind of sorting algorithm, where the sequence of comparisons is not data-dependent. This makes sorting networks

Bitonic sort n = 8

⊕B2 ⊕B4⊕B2

B8

B4B2

B2 ⊕B2 B2

⊕B2 B4B2 B4

B2

B2 B2 B2

⊕BM[2] ⊕BM[4]BM[8]

BM[2]⊕BM[2] BM[4]BM[2]

Build Merge

Page 62: Sorting Networks - King's College LondonA sorting network is a special kind of sorting algorithm, where the sequence of comparisons is not data-dependent. This makes sorting networks
Page 63: Sorting Networks - King's College LondonA sorting network is a special kind of sorting algorithm, where the sequence of comparisons is not data-dependent. This makes sorting networks

Analysis

I BitonicSort[16] is sequentially formed from ⊕, variants{BM[2],BM[4],BM[8],BM[16]} with parallel sections(see top of previous figure)

I BitonicSort[n] makes a sorted sequence of length n fromtwo sorted sequences of length n/2, (one ascending, onedescending) using BM[n]

I BM[n] has log2 n comparator stages. e.g. the 3 = log(8)comparator stages to form sequence i from d and d ′

using BM[8], (see previous figures).

Page 64: Sorting Networks - King's College LondonA sorting network is a special kind of sorting algorithm, where the sequence of comparisons is not data-dependent. This makes sorting networks

I The number of comparator stagesT (BS [n]) = T (BM[n]) + T (BS [n/2]) whereBS[n]=BitonicSort[n]

I The number of comparator stages T (n) of the entiresorting network is given by: T (n) = log(n) + T (n/2)

The solution of this recurrence equation is

T (n) = log(n) + log(n)− 1 + log(n)− 2 + ... + 1

= log(n)(log(n) + 1)/2

Each stage of the sorting network consists of n/2 comparators.In total, there are Θ(nlog 2(n)) comparators.

Page 65: Sorting Networks - King's College LondonA sorting network is a special kind of sorting algorithm, where the sequence of comparisons is not data-dependent. This makes sorting networks

A final word

I The bitonic sort consists of the use bitonic merge BM[n]in various ways, both for the build and merge phases

I Bitonic merge BM[n] is a divide an conquer algorithmbased on Bn. IndeedBM[n] = (Bn, {BM[n/2],BM[n/2]}).

I The steps are build a bitonic sequence and then merge it.

Page 66: Sorting Networks - King's College LondonA sorting network is a special kind of sorting algorithm, where the sequence of comparisons is not data-dependent. This makes sorting networks

Acknowledgements

With the permission of the author (H. W. Lang) these slidesare based on material from the website http://www.iti.fh-flensburg.de/lang/algorithmen/sortieren/algoen.htm.We recommend you read http://www.iti.fh-flensburg.de/lang/algorithmen/sortieren/networks/indexen.htmOther figures fromhttps://en.wikipedia.org/wiki/Sorting network and[CLRS]