1 / 72

Sorting Algorithms

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. Sorting Algorithms. Example.

Download Presentation

Sorting Algorithms

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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 Sorting Algorithms Example Order of comparisons not fixed in advance. Not readily implementable in hardware.

  2. sorted Sorting Networks (binary values) inputs outputs 1 0 0 0 0 0 Sorting Network 1 0 0 1 0 1 1 1 1 1

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

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

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

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

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

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

  9. 1 0 0 0 0 1 0 0 width n 1 0 1 1 0 1 1 1 depth d Comparison Network

  10. 1 0 0 0 0 1 0 0 1 0 1 1 0 1 1 1 d stages Comparison Network n / 2 comparisons per stage

  11. 1 0 0 0 0 1 0 0 width n 1 0 1 1 0 1 1 1 depth d Comparison Network

  12. 1 0 0 0 0 1 0 0 1 0 1 1 0 1 1 1 d stages Comparison Network n / 2 comparisons per stage

  13. Sorting Network Any ideas?

  14. Sorting Network . . . n 1 . . . . . . n Sorting Network inputs outputs

  15. Insertion Sort Network inputs outputs depth2n 3

  16. Batcher Sorting Network Next Lecture

  17. Batcher Sorting Network, n = 8 n = 4 n = 4

  18. sorted sorted Lemma 1 Any subsequence of a sorted sequence is a sorted sequence. 0 0 0 0 0 1 1 1 1 1 1 1 1

  19. sorted Lemma 2 For a sorted sequence, the number of 0’s in the even subsequence is either equal to, or one greater than, the number of 0’s in the odd subsequence. 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 even odd

  20. Lemma 3 For two sorted sequences and : denotes the the number of 0’s in denotes the even subsequence of denotes the odd subsequence of

  21. 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 1 1 0 0 1 1 1 1 1 1 1 1 1 1 1 1 ¢ ¢ ¢ x x x O E Lemma 3

  22. Lemma 3 For two sorted sequences and : (by Lemma 2) (by Lemma 2)

  23. Merge[4] sorted sorted Merge[4] sorted Merge Network

  24. Merge[4] sorted sorted Merge[4] sorted sorted Merge Network (pf.) (by Lemma 1) (by Lemma 1)

  25. Merge[4] sorted By Lemma 3 and differ by at most 1 Merge[4] sorted Merge Network (pf.)

  26. Merge[4] By Lemma 3 and sorted differ by at most 1 Merge[4] Merge Network (pf.)

  27. Merge[4] By Lemma 3 and differ by at most 1 Merge[4] Merge Network (pf.) 0 0 0 0 1 0 1 0 0 0 0 1 0 1 1 1

  28. Sort[4] Merge[8] sorted Sort[4] Batcher Sorting Network

  29. Merge[4] Batcher Sorting Network, n = 4 Sort[2] Sort[2]

  30. Merge[8] Batcher Sorting Network, n = 8 Sort[4] Sort[4]

  31. 1 0 0 0 0 1 0 0 width n 1 0 1 1 0 1 1 1 depth d Comparison Network

  32. 1 0 0 0 0 1 0 0 1 0 1 1 0 1 1 1 d stages Comparison Network n / 2 comparisons per stage

  33. Sort[4] Merge[8] sorted Sort[4] Batcher Sorting Network

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

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

  36. 2 2 2 9 9 6 6 2 6 9 9 6 Sorting Arbitrary Numbers sorted

  37. 1 1 1 1 5 4 4 5 4 5 5 4 Sorting Arbitrary Numbers sorted

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

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

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

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

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

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

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

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

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

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

  48. Sorting Arbitrary Numbers

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

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

More Related