Presentation is loading. Please wait.

Presentation is loading. Please wait.

© 2004 Goodrich, Tamassia Merge Sort1 7 2  9 4  2 4 7 9 7  2  2 79  4  4 9 7  72  29  94  4.

Similar presentations


Presentation on theme: "© 2004 Goodrich, Tamassia Merge Sort1 7 2  9 4  2 4 7 9 7  2  2 79  4  4 9 7  72  29  94  4."— Presentation transcript:

1 © 2004 Goodrich, Tamassia Merge Sort1 7 2  9 4  2 4 7 9 7  2  2 79  4  4 9 7  72  29  94  4

2 Merge Sort2 Divide-and-Conquer (§ 10.1.1) Divide-and-conquer ( 分治演 算法 ) is a general algorithm design paradigm: Divide: divide the input data S in two disjoint subsets S 1 and S 2 Conquer: solve the subproblems associated with S 1 and S 2 Combine: combine the solutions for S 1 and S 2 into a solution for S The base case for the recursion are subproblems of size 0 or 1 Merge-sort is a sorting algorithm based on the divide-and-conquer paradigm Like heap-sort It uses a comparator It has O(n log n) running time Unlike heap-sort It does not use an auxiliary priority queue It accesses data in a sequential manner (suitable to sort data on a disk) External sort 化整為零 各個擊破

3 Merge Sort Merge sort A divide and conquer algorithm Invented by John von Neumann in 1945 Merge Sort3 約翰 · 馮 · 紐曼( John von Neumann , 1903 年 12 月 28 日- 1957 年 2 月 8 日), 出生於匈牙利的美國籍猶太人數學家, 現代電腦創始人之一。他在電腦科學、 經濟、物理學中的量子力學及幾乎所 有數學領域都作過重大貢獻,被譽為 「電腦之父」。 ( 圖及說明摘自維基百 科 )維基百 科

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

5 Merge Sort5 Merging Two Sorted Sequences Merge sort consists of merging two sorted sequences A and B into a sorted sequence S containing the union of the elements of A and B Merging two sorted sequences, each with n  2 elements and implemented by means of a doubly linked list, takes O(n) time Algorithm merge(A, B) Input sequences A and B with n  2 elements each Output sorted sequence of A  B S  empty sequence while  A.empty()   B.empty() if A.front() < B.front() S.addBack(A.front()); A.eraseFront(); else S.addBack(B.front()); B.eraseFront(); while  A.empty() S.addBack(A.front()); A.eraseFront(); while  B.empty() S.addBack(B.front()); B.eraseFront(); return S

6 6 To Merge 2 Sorted Sequences Properties Need extra space to store the sorted results  Not an in-place sort Total time = O(m+n) 1152426 2132738 11524262132738 1 11524262132738 1 2 11524262132738 1 213

7 Merge Sort7 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 79  4  4 9 7  72  29  94  4

8 Merge Sort8 Execution Example Partition 7 2 9 4  2 4 7 93 8 6 1  1 3 8 67 2  2 79 4  4 93 8  3 86 1  1 67  72  29  94  43  38  86  61  1 7 2 9 4  3 8 6 1  1 2 3 4 6 7 8 9

9 Merge Sort9 Execution Example (cont.) Recursive call, partition 7 2  9 4  2 4 7 9 3 8 6 1  1 3 8 6 7 2  2 79 4  4 93 8  3 86 1  1 67  72  29  94  43  38  86  61  1 7 2 9 4  3 8 6 1  1 2 3 4 6 7 8 9

10 Merge Sort10 Execution Example (cont.) Recursive call, partition 7 2  9 4  2 4 7 93 8 6 1  1 3 8 6 7  2  2 7 9 4  4 93 8  3 86 1  1 6 7  72  29  94  43  38  86  61  1 7 2 9 4  3 8 6 1  1 2 3 4 6 7 8 9

11 Merge Sort11 Execution Example (cont.) Recursive call, base case 7 2  9 4  2 4 7 93 8 6 1  1 3 8 6 7  2  2 79 4  4 93 8  3 86 1  1 6 7  77  7 2  29  94  43  38  86  61  1 7 2 9 4  3 8 6 1  1 2 3 4 6 7 8 9

12 Merge Sort12 Execution Example (cont.) Recursive call, base case 7 2  9 4  2 4 7 93 8 6 1  1 3 8 6 7  2  2 79 4  4 93 8  3 86 1  1 6 7  77  72  22  29  94  43  38  86  61  1 7 2 9 4  3 8 6 1  1 2 3 4 6 7 8 9

13 Merge Sort13 Execution Example (cont.) Merge 7 2  9 4  2 4 7 93 8 6 1  1 3 8 6 7  2  2 7 9 4  4 93 8  3 86 1  1 6 7  77  72  22  29  94  43  38  86  61  1 7 2 9 4  3 8 6 1  1 2 3 4 6 7 8 9

14 Merge Sort14 Execution Example (cont.) Recursive call, …, base case, merge 7 2  9 4  2 4 7 93 8 6 1  1 3 8 6 7  2  2 7 9 4  4 9 3 8  3 86 1  1 6 7  77  72  22  23  38  86  61  1 7 2 9 4  3 8 6 1  1 2 3 4 6 7 8 9 9  94  4

15 Merge Sort15 Execution Example (cont.) Merge 7 2  9 4  2 4 7 9 3 8 6 1  1 3 8 6 7  2  2 79 4  4 93 8  3 86 1  1 6 7  77  72  22  29  94  43  38  86  61  1 7 2 9 4  3 8 6 1  1 2 3 4 6 7 8 9

16 Merge Sort16 Execution Example (cont.) Recursive call, …, merge, merge 7 2  9 4  2 4 7 9 3 8 6 1  1 3 6 8 7  2  2 79 4  4 93 8  3 86 1  1 6 7  77  72  22  29  94  43  33  38  88  86  66  61  11  1 7 2 9 4  3 8 6 1  1 2 3 4 6 7 8 9

17 Merge Sort17 Execution Example (cont.) Merge 7 2  9 4  2 4 7 93 8 6 1  1 3 6 8 7  2  2 79 4  4 93 8  3 86 1  1 6 7  77  72  22  29  94  43  33  38  88  86  66  61  11  1 7 2 9 4  3 8 6 1  1 2 3 4 6 7 8 9

18 18 Merge Sort: Example 1242615 1322738 12426151322738 12426152738132 12426151322738 12415262738213 11524262132738 12131524262738

19 Why Merge Sort Is Stable? 19 50 and 50’ have the same relative position

20 Resources on Merge Sort Numerous resources on merge sort Wiki  Animation by sorting a vector Animation by sorting a vector  Animation by dots Animation by dots Youtube  Detailed explanation with pseudo code Detailed explanation with pseudo code Merge Sort20

21 Merge Sort21 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#seqssize 01n 12 n2n2 i2i2i n2in2i ………

22 Merge Sort22 Summary of Sorting Algorithms AlgorithmTimeNotes selection-sort O(n2)O(n2)  slow  in-place  for small data sets (< 1K) insertion-sort O(n2)O(n2)  slow  in-place  for small data sets (< 1K) heap-sort O(n log n)  fast  in-place  for large data sets (1K — 1M) merge-sort O(n log n)  fast  sequential data access  for huge data sets (> 1M)


Download ppt "© 2004 Goodrich, Tamassia Merge Sort1 7 2  9 4  2 4 7 9 7  2  2 79  4  4 9 7  72  29  94  4."

Similar presentations


Ads by Google