Presentation is loading. Please wait.

Presentation is loading. Please wait.

Comparison Networks Sorting Sorting binary values

Similar presentations


Presentation on theme: "Comparison Networks Sorting Sorting binary values"— Presentation transcript:

1 Comparison Networks Sorting Sorting binary values
Sorting arbitrary numbers Implementing symmetric functions

2 Sorting Algorithms Example
Mergesort(array[1,…,n] of Integers): begin Mergesort(array[1,…,n/2]); Mergesort(array[n/2+1,…,n]); Merge(array[1,…,n/2], array[n/2+1,…,n]); end comparisons required to merge two arrays of size m/2 comparisons to sort n elements Order of comparisons not fixed in advance. Not readily implementable in hardware.

3 Sorting Networks Sorting Network C D B A A B C D
Order of comparisons fixed in advance. Readily implementable in hardware.

4 Sorting Networks (binary values)
inputs outputs 1 sorted Sorting Network 1 1 1 1 1 1 1

5 Comparator (2-sorter) inputs outputs x min(x, y) C y max(x, y)

6 Comparator (2-sorter) AON Implementation x min(x, y) max(x, y) y
inputs outputs x min(x, y) max(x, y) y

7 Comparator (2-sorter) inputs outputs x min(x, y) y max(x, y)

8 Comparison Network 1 1 1 1 width n depth d

9 Comparison Network 1 1 1 1 n / 2 comparisons per stage d stages

10 Sorting Network Any ideas?

11 Sorting Network inputs outputs Sorting Network . n . n 1

12 Insertion Sort Network
inputs outputs depth 2n 3

13 Batcher Sorting Network
Next Lecture

14 Sorting Arbitrary Numbers
inputs outputs x min(x, y) y max(x, y) x, y can be values from any linearly ordered set, e.g., integers, reals, etc.

15 Integer Comparator X, Y: integers represented as m-bit binary strings.
Comparison function: C(X,Y) = 1 if X > Y, 0 otherwise. Idea: use C(X,Y) to select the min and the max of X and Y.

16 Integer Comparator X C(X, Y) min(X, Y) Y C(X, Y) X C(X, Y) max(X, Y) Y

17 Sorting Arbitrary Numbers
9 6 2 2 9 6 6 9 2 2 6 9 sorted

18 Sorting Arbitrary Numbers
1 4 5 1 5 4 4 5 1 1 4 5 sorted

19 Sorting Arbitrary Numbers
3 7 3 7 7 3 3 7 not sorted How can we verify if a network sorts all possible input sequences?

20 Sorting Arbitrary Numbers
inputs outputs Try all possible 0/1 sequences.

21 Sorting Arbitrary Numbers
inputs outputs 000 000 Try all possible 0/1 sequences.

22 Sorting Arbitrary Numbers
inputs outputs 1 1 1 1 000 000 001 001 Try all possible 0/1 sequences.

23 Sorting Arbitrary Numbers
inputs outputs 1 1 1 1 000 000 001 001 010 001 Try all possible 0/1 sequences.

24 Sorting Arbitrary Numbers
inputs outputs 1 1 1 1 000 000 001 001 010 001 011 011 Try all possible 0/1 sequences.

25 Sorting Arbitrary Numbers
inputs outputs 1 1 1 1 000 000 001 001 010 001 011 011 100 001 Try all possible 0/1 sequences.

26 Sorting Arbitrary Numbers
inputs outputs 1 1 1 1 000 000 001 001 010 001 011 011 100 001 101 011 Try all possible 0/1 sequences.

27 Sorting Arbitrary Numbers
inputs outputs 1 1 1 1 000 000 001 001 010 001 011 011 100 001 101 011 110 101 not sorted! Try all possible 0/1 sequences.

28 Sorting Arbitrary Numbers
inputs outputs 1 1 1 1 000 000 001 001 010 001 011 011 100 001 101 011 110 101 not sorted! 111 111 Try all possible 0/1 sequences.

29 Sorting Arbitrary Numbers

30 Sorting Arbitrary Numbers
inputs outputs Try all possible 0/1 sequences.

31 Sorting Arbitrary Numbers
inputs outputs 000 000 Try all possible 0/1 sequences.

32 Sorting Arbitrary Numbers
inputs outputs 1 1 1 1 000 000 001 001 Try all possible 0/1 sequences.

33 Sorting Arbitrary Numbers
inputs outputs 1 1 1 1 000 000 001 001 010 001 Try all possible 0/1 sequences.

34 Sorting Arbitrary Numbers
inputs outputs 1 1 1 1 000 000 001 001 010 001 011 011 Try all possible 0/1 sequences.

35 Sorting Arbitrary Numbers
inputs outputs 1 1 1 1 000 000 001 001 010 001 011 011 100 001 Try all possible 0/1 sequences.

36 Sorting Arbitrary Numbers
inputs outputs 1 1 1 1 000 000 001 001 010 001 011 011 100 001 101 011 Try all possible 0/1 sequences.

37 Sorting Arbitrary Numbers
inputs outputs 1 1 1 1 000 000 001 001 010 001 011 011 100 001 101 011 110 011 Try all possible 0/1 sequences.

38 Sorting Arbitrary Numbers
inputs outputs 1 1 1 1 000 000 001 001 010 001 011 011 100 001 101 011 110 011 111 111 Try all possible 0/1 sequences.

39 Sorting Arbitrary Numbers
inputs outputs 000 000 001 001 010 001 011 011 100 001 101 011 110 011 111 111 all sorted! Try all possible 0/1 sequences.

40 Zero-One Principle If a comparison network sorts all possible sequences of 0’s and 1’s correctly, then it sorts all sequences of arbitrary numbers correctly.

41 Lemma Given For a monotonically increasing function f,

42 Lemma Given For a monotonically increasing function f,

43 Proof: Lemma

44 Proof: Lemma

45 Proof: Lemma f is monotonically increasing:

46 Proof: Lemma f is monotonically increasing:

47 Proof: Lemma f is monotonically increasing:

48 Generalization Given

49 Generalization For a monotonically increasing function f,
(by induction)

50 Proof: Zero-One Principle
Suppose b) there exists a sequence that it doesn’t sort, i.e., such that but is placed before in the output. a) the network sorts all sequences of 0’s and 1’s, Define f (x) = 0 if 1 otherwise

51 Proof: Zero-One Principle
Sorting Network .

52 Proof: Zero-One Principle
. Sorting Network . . . .

53 Proof: Zero-One Principle
. Sorting Network . . 1 contradiction! . .

54 Implementing XOR XOR(X) = 1 if odd 0 otherwise Sorting Network 1 1 1 1

55 Implementing XOR XOR(X) = 1 if odd 0 otherwise Sorting Network

56 Symmetric Functions for some subset of f (X) = 1 if 0 otherwise
Sorting Network


Download ppt "Comparison Networks Sorting Sorting binary values"

Similar presentations


Ads by Google