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

加入VIP,免费下载
 

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

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

下载须知

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

版权提示 | 免责声明

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

算法设计与分析5QuickSort.ppt

1、Quicksort,Algorithm : Design & Analysis 5,In the last class,Comparison-based sorting Insertion sort Analysis of insertion sorting algorithm Lower bound of local comparison based sorting algorithm Shellsort,Quicksort,General pattern of divide-and conquer Quicksort: the Strategy Quicksort: the Algorit

2、hm Analysis of Quicksort Improvements of the Algorithm,“What a wonderful way to start a career in Computing, by discovering a new sorting algorithm.“,- C.A.R. Hoare,Quicksort: the Strategy,Dividing the array to be sorted into two parts: “small” and “large”, which will be sorted recursively.,first,la

3、st,for any element in this segment, the key is less than pivot.,for any element in this segment, the key is not less than pivot.,QuickSort: the algorithm,Input: Array E and indexes first, and last, such that elements Ei are defined for firstilast. Output: Efirst,Elast is a sorted rearrangement of th

4、e same elements. The procedure:void quickSort(Element E, int first, int last)if (firstlast)Element pivotElement=Efirst;Key pivot=pivotElement.key;int splitPoint=partition(E, pivot, first, last);EsplitPoint=pivotElement;quickSort(E, first, splitPoint-1);quickSort(E, splitPoint+1, last);return,The spl

5、itting point is chosen arbitrariry, as the first element in the array segment here.,Partition: the Strategy,Partition: the Process,Always keep a vacancy before completion.,First met key that is less than pivot,First met key that is larger than pivot,Moving as far as possible!,Vacant left after movin

6、g,highVac,lowVac,Partition: the Algorithm,Input: Array E, pivot, the key around which to partition, and indexes first, and last, such that elements Ei are defined for first+1ilast and Efirst is vacant. It is assumed that firstlast. Output: Returning splitPoint, the elements origingally in first+1,la

7、st are rearranged into two subranges, such that the keys of Efirst, , EsplitPoint-1 are less than pivot, and the keys of EsplitPoint+1, , Elast are not less than pivot, and firstsplitPointlast, and EsplitPoint is vacant.,Partition: the Procedure,int partition(Element E, Key pivot, int first, int las

8、t)int low, high; 1. low=first; high=last; 2. while (lowhigh) 3. int highVac=extendLargeRegion(E,pivot,low,high); 4. int lowVac = extendSmallRegion(E,pivot,low+1,highVac); 5. low=lowVac; high=highVac-1; 6 return low; /This is the splitPoint,highVac has been filled now.,Extending Regions,Specification

9、 for Precondition: lowVachigh Postcondition: If there are elements in ElowVac+1,.,Ehigh whose key is less than pivot, then the rightmost of them is moved to ElowVac, and its original index is returned. If there is no such element, lowVac is returned.,extendLargeRegion(Element E, Key pivot, int lowVa

10、c, int high),Example of Quicksort,45 14 62 51 75 96 33 84 20,45 as pivot,20 14 62 51 75 96 33 84,20 14 51 75 96 33 84 62,high,highVac,low,lowVac,20 14 51 75 96 33 84 62,low,high =highVac-1,20 14 33 51 75 96 84 62,highVac,20 14 33 75 96 51 84 62,highVac,lowVac,To be processed in the next loop,Divide

11、and Conquer: General Pattern,solve(I)n=size(I);if (nsmallSize)solution=directlySolve(I)elsedivide I into I1, Ik;for each i1,kSi=solve(Ii);solution=combine(S1 , ,Sk);return solution,T(n)=D(n)+ T(size(Ii)+C(n)for nsmallSize,T(n)=B(n) for nsmallSize,Workhorse,“Hard division, easy combination” “Easy div

12、ision, hard combination”Usually, the “real work” is in one part.,Worst Case: a Paradox,For a range of k positions, k-1 keys are compared with the pivot(one is vacant). If the pivot is the smallest, than the “large” segment has all the remaining k-1 elements, and the “small” segment is empty. If the

13、elements in the array to be sorted has already in ascending order(the Goal), then the number of comparison that Partition has to do is:,Average Analysis,Assumption: all permutation of the keys are equally likely. A(n) is the average number of key comparison done for range of size n. In the first cyc

14、le of Partition, n-1 comparisons are done If split point is Ei(each i has probability 1/n), Partition is to be executed recursively on the subrange 0,i and i+1,n-1,The Recurrence Equation,with i0,1,2,n-1, each value with the probability 1/nSo, the average number of key comparison A(n) is:and A(1)=A(

15、0)=0,The number of key comparison in the first cycle(finding the splitPoint) is n-1,Why the assumed probability is still hold for each subrange?,No two keys within a subrange have been compared each other!,Simplified Recurrence Equation,Note: So: Two approaches to solve the equation Guess and prove

16、by induction Solve directly,Guess the Solution,A special case as clue for guess Assuming that Partition divide the problem range into 2 subranges of about the same size. So, the number of comparison Q(n) satisfy: Q(n) n+2Q(n/2) Applying Master Theorem, case 2: Q(n)(nlogn)Note: here, b=c=2, so E=lg(b

17、)/lg(c)=1, and, f(n)=n=nE,Inductive Proof: A(n)O(nlnn),Theorem: A(n)cnlnn for some constant c, with A(n) defined by the recurrence equation above. Proof: By induction on n, the number of elements to be sorted. Base case(n=1) is trivial. Inductive assumption: A(i)cilni for 1in,For Your Reference,Harm

18、onic Series,Inductive Proof: A(n)(nlnn),Theorem: A(n)cnlnn for some co c, with large n Inductive reasoning:,Inductive assumption,Directly Derived Recurrence Equation,Combining the 2 equations in some way, we can remove all A(i) for i=1,2,n-2,Solving the Equation,We have equation:,Let it be B(n),Note

19、: lnn 0.693 lgn,Space Complexity,Good news: Partition is in-place Bad news: In the worst case, the depth of recursion will be n-1 So, the largest size of the recursion stack will be in (n),Recursion on Small Partition,void quickSort(Element E, int first, int last)int first1, last1, first2, last2;fir

20、st2=first; last2=last;while (last2-first21)pivotElement=Efirst; pivot=pivotElement.key;int splitPoint=partition(E,pivot,first2,last2)Esplitpoint=pivotElement;If (splitPoint(first2+last2)/2)first1=first2; last1=splitPoint-1; first2=splitPoint+1; last2=last2elsefirst1=splitPoint+1; last1=last2; first2

21、=first2; last2=splitPoint-1;quickSortTRO(E, first1, last1);return,Recursion:small partition,iteration: larger partition,Eliminating Recursion,void quickSort(Element E,int first, last)int stack; int top;while truewhile (last-first1)pivotElement=Efirst;pivot=pivotElement.key;int splitPoint=partition(E

22、,pivot,first,last);if (splitPoint(first+last)/2)stacktop+=splitPoint+1; stacktop+=last;first=first; last=splitPoint-1;elsestacktop+=first; stacktop+=splitPoint-1;first=splitPoint+1; last=last;if top=0 return;first=stacktop; last=stacktop-1; top=top-2;,processing in inner loop determines the stack depth,Dealing with Duplicated Keys,Three-way comparison,splitpoint,Elements with keys equal to the pivot,Home Assignment,pp.209- 4.12 4.17 4.18 4.21 4.22,

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


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

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

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