ImageVerifierCode 换一换
格式:PPT , 页数:23 ,大小:196.50KB ,
资源ID:7281973      下载积分:10 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.docduoduo.com/d-7281973.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录   微博登录 

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(算法设计与分析6MergeSort.ppt)为本站会员(lxhqcj)主动上传,道客多多仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知道客多多(发送邮件至docduoduo@163.com或直接QQ联系客服),我们立即给予删除!

算法设计与分析6MergeSort.ppt

1、MergeSort,Algorithm : Design & Analysis 6,In the last class,General pattern of divide-and conquer Quicksort: the Strategy Quicksort: the Algorithm Analysis of Quicksort Improvements of the Algorithm,Mergesort,Mergesort Worst Case Analysis of Mergesort Lower Bounds for Sorting by Comparison of Keys W

2、orst Case Average Behavior,MergeSort: the Strategy,Easy division No comparison is done during the division Minimizing the size difference between the divided subproblems Merging two sorted subranges Using Merge,Merging Sorted Arrays,A0,Ak-1,B0,Bm-1,Merge: the Specification,Input: Array A with k elem

3、ents and B with m elements, each in nondecreasing order of their key. Output: C, an array containing n=k+m elements from A and B in nondecreasing order. C is passed in and the algorithm fills it.,Merge: the Recursive Version,merge(A,B,C)if (A is empty)rest of C = rest of Belse if (B is empty)rest of

4、 C = rest of Aelseif (first of A first of B)first of C =first of Amerge(rest of A, B, rest of C)elsefirst of C =first of Bmerge(A, rest of B, rest of C)return,Base cases,Worst Case Complexity of Merge,Observations: After each comparison, one element is inserted into Array C, at least. After entering

5、 Array C, an element will never be compared again After the last comparison, at least two elements have not yet been moved to Array C. So at most n-1 comparisons are done. Worst case is that the last comparison is conducted between Ak-1 and Bm-1 In worst case, n-1 comparisons are done, where n=k+m,O

6、ptimality of Merge,Any algorithm to merge two sorted arrays, each containing k=m=n/2 entries, by comparison of keys, does at least n-1 comparisons in the worst case. Choose keys so that: b0a0b1 a1.biaibi+1,.,bm-1ak-1 Then the algorithm must compare ai with bi for every i in 0,m-1, and must compare a

7、i with bi+1 for every i in 0, m-2, so, there are n-1 comparisons.,Valid for |k-m|1, as well.,Space Complexity of Merge,A algorithm is “in space”, if the extra space it has to use is in (1) Merge is not a algorithm “in space”, since it need enough extra space to store the merged sequence during the m

8、erging process.,Overlapping Arrays for Merge,0,k-1,m-1,0,Before the merge,k+m-1,A,B,Merge from the right,0,k-1,m-1,0,k+m-1,0,k-1,m-1,0,k+m-1,A/C,Finished,Merged,extra space,Partly finished,MergeSort,Input: Array E and indexes first, and last, such that the elements of Ei are defined for firstilast.

9、Output: Efirst,Elast is a sorted rearrangement of the same elements. Procedure void mergeSort(Element E, int first, int last)if (firstlast)int mid=(first+last)/2;mergeSort(E, first, mid);mergeSort(E, mid+1, last);merge(E, first, mid, last)return,Analysis of Mergesort,The recurrence equation for Merg

10、esort W(n)=W(n/2)+W(n/2)+n-1 W(1)=0 Where n=last-first+1, the size of range to be sorted The Master Theorem applies for the equation, so: W(n)(nlogn),k/2 may be k/2 or k/2,Base cases occur at depth lg(n+1)-1 and lg(n+1),Recursion Tree for Mergesort,T(n),n-1,T(n/2),n/2-1,T(n/8),n/8-1,T(n/4),n/4-1,n-1

11、,n-2,n-4,n-8,Level 0,Level 1,Level 2,Level 3,Note: nonrecursive costs on level k is n-2k for all level without basecase node,Non-complete Recursive Tree,B base-case nodes on the second lowest level,n-B base-case nodes No nonbase-case nodes at this depth,2D-1 nodes,Since each nonbase-case node has 2

12、children, there are (n-B)/2 nonbase-case nodes at depth D-1,Example: n=11,Number of Comparison of Mergesort,The maximum depth D of the recursive tree is lg(n+1). Let B base case nodes on depth D-1, and n-B on depth D, (Note: base case node has nonrecursive cost 0). (n-B)/2 nonbase case nodes at dept

13、h D-1, each has nonrecursive cost 1. So:nlg(n)-n+1 number of comparison nlg(n)-0.914n,Decision Tree for Sorting,A example for n=3Decision tree is a 2-tree.(Assuming no same keys) The action of Sort on a particular input corresponds to following on path in its decision tree from the root to a leaf as

14、sociated to the specific output,Internal node,External node,Characteristics of the Decision Tree,For a sequence of n distinct elements, there are n! different permutation, so, the decision tree has at least n! leaves, and exactly n! leaves can be reached from the root. So, for the purpose of lower b

15、ounds evaluation, we use trees with exactly n! leaves. The number of comparison done in the worst case is the height of the tree. The average number of comparison done is the average of the lengths of all paths from the root to a leaf.,Lower Bound for Worst Case,Theorem: Any algorithm to sort n item

16、s by comparisons of keys must do at least lgn!, or approximately nlgn-1.443n, key comparisons in the worst case. Note: Let L=n!, which is the number of leaves, then L2h, where h is the height of the tree, that is h lgL=lgn! For the asymptotic behavior:derived using:,Lower Bound for Average Behavior,

17、Since a decision tree with L leaves is a 2-tree, the average path length from the root to a leaf is .The trees that minimize epl are as balanced as possible. Recall that epl Llg(L). Theorem: The average number of comparison done by an algorithm to sort n items by comparison of keys is at least lg(n!

18、), which is about nlgn-1.443n.,to be proved,Reducing External Path Length,Assuming that h-k1, when calculating epl, h+h+k is replaced by (h-1)+2(k+1). The net change in epl is k-h+10, that is, the epl decreases. So, more balanced 2-tree has smaller epl.,X,Y,X,Y,level,k,level,h,-,1,level,h,level,k,+1

19、,Mergesort Has Optimal Average Performance,We have proved that the average number of comparisons done by an algorithm to sort n items by comparison of keys is at least about nlgn-1.443n The worst complexity of mergesort is in (nlgn) But, the average performance can not be worse the the worst case performance. So, mergesort is optimal as for its average performance.,Home Assignment,pp.212- 4.24 4.25 4.27 4.29 4.30 4.32,

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:道客多多官方知乎号:道客多多

经营许可证编号: 粤ICP备2021046453号世界地图

道客多多©版权所有2020-2025营业执照举报