merge sort
play

Merge Sort 7 2 9 4 2 4 7 9 7 2 2 7 9 4 4 9 7 7 2 2 9 - PowerPoint PPT Presentation

Merge Sort 7 2 9 4 2 4 7 9 7 2 2 7 9 4 4 9 7 7 2 2 9 9 4 4 Chapter 4 1 Outline and Reading Divide-and-conquer paradigm, MergeSort (4.1) Sets (4.2);Generic Merging and set operations (4.2.1)


  1. Merge Sort 7 2  9 4 → 2 4 7 9 7  2 → 2 7 9  4 → 4 9 7 → 7 2 → 2 9 → 9 4 → 4 Chapter 4 1

  2. Outline and Reading Divide-and-conquer paradigm, MergeSort (§4.1) Sets (§4.2);Generic Merging and set operations (§4.2.1)  Note: Sections 4.2.2 and 4.2.3 are Optional Quick-sort (§4.3) Analysis of quick-sort ((§4.3.1) A Lower Bound on Comparison-based Sorting (§4.4) QuickSort and Radix Sort (§4.5) In-place quick-sort (§4.8) Comparison of Sorting Algorithm (§4.6) Selection (§4.7) Chapter 4 2

  3. Divide-and-Conquer Divide-and conquer is a Merge-sort is a sorting general algorithm design algorithm based on the paradigm: divide-and-conquer paradigm Divide: divide the input data  S in two disjoint subsets S 1 Like heap-sort and S 2 It uses a comparator  Recur: solve the  It has O ( n log n ) running  subproblems associated time with S 1 and S 2 Unlike heap-sort Conquer: combine the  It does not use an solutions for S 1 and S 2 into a  auxiliary priority queue solution for S It accesses data in a  The base case for the sequential manner recursion are subproblems of (suitable to sort data on a size 0 or 1 disk) Chapter 4 3

  4. Merge-Sort Algorithm mergeSort ( S, C ) Merge-sort on an input sequence S with n Input sequence S with n elements, comparator C elements consists of Output sequence S sorted three steps: according to C  Divide: partition S into if S.size () > 1 two sequences S 1 and S 2 ( S 1 , S 2 ) ← partition ( S , n /2) of about n / 2 elements each mergeSort ( S 1 , C )  Recur: recursively sort S 1 mergeSort ( S 2 , C ) and S 2 S ← merge ( S 1 , S 2 )  Conquer: merge S 1 and S 2 into a unique sorted sequence Chapter 4 4

  5. Merging Two Sorted Sequences Algorithm merge ( A, B ) The conquer step of merge-sort consists Input sequences A and B with n / 2 elements each of merging two Output sorted sequence of A ∪ B sorted sequences A and B into a sorted S ← empty sequence sequence S while ¬ A.isEmpty () ∧ ¬ B.isEmpty () containing the union of the elements of A if A.first () .element () < B.first () .element () and B S.insertLast ( A.remove ( A.first ())) Merging two sorted else sequences, each S.insertLast ( B.remove ( B.first ())) with n / 2 elements while ¬ A.isEmpty () and implemented by S.insertLast ( A.remove ( A.first ())) means of a doubly while ¬ B.isEmpty () linked list, takes S.insertLast ( B.remove ( B.first ())) O ( n ) time return S Chapter 4 5

  6. Merge-Sort Tree An execution of merge-sort is depicted by a binary tree  each node represents a recursive call of merge-sort and stores  unsorted sequence before the execution and its partition  sorted sequence at the end of the execution  the root is the initial call  the leaves are calls on subsequences of size 0 or 1 7 2  9 4 → 2 4 7 9 7  2 → 2 7 9  4 → 4 9 7 → 7 2 → 2 9 → 9 4 → 4 Chapter 4 6

  7. Execution Example Partition 7 2 9 4  3 8 6 1 → 1 2 3 4 6 7 8 9 7 2 9 4 → 2 4 7 9 3 8 6 1 → 1 3 8 6 7 2 → 2 7 9 4 → 4 9 3 8 → 3 8 6 1 → 1 6 7 → 7 2 → 2 9 → 9 4 → 4 3 → 3 8 → 8 6 → 6 1 → 1 Chapter 4 7

  8. Execution Example (cont.) Recursive call, partition 7 2 9 4  3 8 6 1 → 1 2 3 4 6 7 8 9 7 2  9 4 → 2 4 7 9 3 8 6 1 → 1 3 8 6 7 2 → 2 7 9 4 → 4 9 3 8 → 3 8 6 1 → 1 6 7 → 7 2 → 2 9 → 9 4 → 4 3 → 3 8 → 8 6 → 6 1 → 1 Chapter 4 8

  9. Execution Example (cont.) Recursive call, partition 7 2 9 4  3 8 6 1 → 1 2 3 4 6 7 8 9 7 2  9 4 → 2 4 7 9 3 8 6 1 → 1 3 8 6 7  2 → 2 7 9 4 → 4 9 3 8 → 3 8 6 1 → 1 6 7 → 7 2 → 2 9 → 9 4 → 4 3 → 3 8 → 8 6 → 6 1 → 1 Chapter 4 9

  10. Execution Example (cont.) Recursive call, base case 7 2 9 4  3 8 6 1 → 1 2 3 4 6 7 8 9 7 2  9 4 → 2 4 7 9 3 8 6 1 → 1 3 8 6 7  2 → 2 7 9 4 → 4 9 3 8 → 3 8 6 1 → 1 6 7 → 7 2 → 2 9 → 9 4 → 4 3 → 3 8 → 8 6 → 6 1 → 1 Chapter 4 10

  11. Execution Example (cont.) Recursive call, base case 7 2 9 4  3 8 6 1 → 1 2 3 4 6 7 8 9 7 2  9 4 → 2 4 7 9 3 8 6 1 → 1 3 8 6 7  2 → 2 7 9 4 → 4 9 3 8 → 3 8 6 1 → 1 6 7 → 7 2 → 2 9 → 9 4 → 4 3 → 3 8 → 8 6 → 6 1 → 1 Chapter 4 11

  12. Execution Example (cont.) Merge 7 2 9 4  3 8 6 1 → 1 2 3 4 6 7 8 9 7 2  9 4 → 2 4 7 9 3 8 6 1 → 1 3 8 6 7  2 → 2 7 9 4 → 4 9 3 8 → 3 8 6 1 → 1 6 7 → 7 2 → 2 9 → 9 4 → 4 3 → 3 8 → 8 6 → 6 1 → 1 Chapter 4 12

  13. Execution Example (cont.) Recursive call, …, base case, merge 7 2 9 4  3 8 6 1 → 1 2 3 4 6 7 8 9 7 2  9 4 → 2 4 7 9 3 8 6 1 → 1 3 8 6 7  2 → 2 7 9 4 → 4 9 3 8 → 3 8 6 1 → 1 6 7 → 7 2 → 2 9 → 9 4 → 4 3 → 3 8 → 8 6 → 6 1 → 1 Chapter 4 13

  14. Execution Example (cont.) Merge 7 2 9 4  3 8 6 1 → 1 2 3 4 6 7 8 9 7 2  9 4 → 2 4 7 9 3 8 6 1 → 1 3 8 6 7  2 → 2 7 9 4 → 4 9 3 8 → 3 8 6 1 → 1 6 7 → 7 2 → 2 9 → 9 4 → 4 3 → 3 8 → 8 6 → 6 1 → 1 Chapter 4 14

  15. Execution Example (cont.) Recursive call, …, merge, merge 7 2 9 4  3 8 6 1 → 1 2 3 4 6 7 8 9 7 2  9 4 → 2 4 7 9 3 8 6 1 → 1 3 6 8 7  2 → 2 7 9 4 → 4 9 3 8 → 3 8 6 1 → 1 6 7 → 7 2 → 2 9 → 9 4 → 4 3 → 3 8 → 8 6 → 6 1 → 1 Chapter 4 15

  16. Execution Example (cont.) Merge 7 2 9 4  3 8 6 1 → 1 2 3 4 6 7 8 9 7 2  9 4 → 2 4 7 9 3 8 6 1 → 1 3 6 8 7  2 → 2 7 9 4 → 4 9 3 8 → 3 8 6 1 → 1 6 7 → 7 2 → 2 9 → 9 4 → 4 3 → 3 8 → 8 6 → 6 1 → 1 Chapter 4 16

  17. Analysis of Merge-Sort The height h of the merge-sort tree is O (log n ) at each recursive call we divide in half the sequence,  The overall amount or work done at the nodes of depth i is O ( n ) we partition and merge 2 i sequences of size n / 2 i  we make 2 i + 1 recursive calls  Thus, the total running time of merge-sort is O ( n log n ) depth # seqs size 0 1 n n / 2 1 2 n / 2 i 2 i i … … … Chapter 4 17

  18. Sets Chapter 4 18

  19. Set Operations We represent a set by the Set union: sorted sequence of its aIsLess ( a, S )  elements S.insertFirst ( a ) By specializing the auxliliary bIsLess ( b, S )  methods he generic merge S.insertLast ( b ) algorithm can be used to bothAreEqual ( a, b, S )  perform basic set operations: S. insertLast ( a ) union  Set intersection: intersection  aIsLess ( a, S )  subtraction  { do nothing } The running time of an bIsLess ( b, S )  operation on sets A and B { do nothing } should be at most O ( n A + n B ) bothAreEqual ( a, b, S )  S. insertLast ( a ) Chapter 4 19

  20. Storing a Set in a List We can implement a set with a list Elements are stored sorted according to some canonical ordering The space used is O ( n ) Nodes storing set elements in order ∅ List Set elements Chapter 4 20

  21. Generic Merging Algorithm genericMerge ( A, B ) Generalized merge S ← empty sequence of two sorted lists while ¬ A.isEmpty () ∧ ¬ B.isEmpty () A and B a ← A.first () .element (); b ← B.first () .element () Template method if a < b genericMerge aIsLess ( a, S ); A.remove ( A.first ()) else if b < a Auxiliary methods bIsLess ( b, S ); B.remove ( B.first ()) aIsLess  else { b = a } bIsLess  bothAreEqual ( a, b, S ) bothAreEqual  A.remove ( A.first ()); B.remove ( B.first ()) Runs in O ( n A + n B ) while ¬ A.isEmpty () aIsLess ( a, S ); A.remove ( A.first ()) time provided the while ¬ B.isEmpty () auxiliary methods bIsLess ( b, S ); B.remove ( B.first ()) run in O (1) time return S Chapter 4 21

  22. Using Generic Merge for Set Operations Any of the set operations can be implemented using a generic merge For example:  For intersection: only copy elements that are duplicated in both list  For union: copy every element from both lists except for the duplicates All methods run in linear time. Chapter 4 22

  23. Quick-Sort 7 4 9 6 2 → 2 4 6 7 9 4 2 → 2 4 7 9 → 7 9 2 → 2 9 → 9 Chapter 4 23

  24. Quick-Sort Quick-sort is a randomized sorting algorithm based x on the divide-and-conquer paradigm:  Divide: pick a random element x (called pivot) and x partition S into  L elements less than x L E G  E elements equal x  G elements greater than x  Recur: sort L and G x  Conquer: join L , E and G Chapter 4 24

  25. Partition Algorithm partition ( S, p ) We partition an input Input sequence S , position p of pivot sequence as follows: Output subsequences L, E, G of the We remove, in turn, each  elements of S less than, equal to, element y from S and or greater than the pivot, resp. We insert y into L , E or G ,  L, E, G ← empty sequences depending on the result of x ← S.remove ( p ) the comparison with the while ¬ S.isEmpty () pivot x y ← S.remove ( S.first ()) Each insertion and removal if y < x is at the beginning or at the L.insertLast ( y ) end of a sequence, and else if y = x hence takes O (1) time E.insertLast ( y ) Thus, the partition step of else { y > x } quick-sort takes O ( n ) time G.insertLast ( y ) return L, E, G Chapter 4 25

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend