欢迎来到装配图网! | 帮助中心 装配图网zhuangpeitu.com!
装配图网
ImageVerifierCode 换一换
首页 装配图网 > 资源分类 > PPT文档下载
 

数据结构教学课件:Chapter7

  • 资源ID:190958238       资源大小:573KB        全文页数:42页
  • 资源格式: PPT        下载积分:30积分
快捷下载 游客一键下载
会员登录下载
微信登录下载
三方登录下载: 微信开放平台登录 支付宝登录   QQ登录   微博登录  
二维码
微信扫一扫登录
下载资源需要30积分
邮箱/手机:
温馨提示:
用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)
支付方式: 支付宝    微信支付   
验证码:   换一换

 
账号:
密码:
验证码:   换一换
  忘记密码?
    
友情提示
2、PDF文件下载后,可能会被浏览器默认打开,此种情况可以点击浏览器菜单,保存网页到桌面,就可以正常下载了。
3、本站不支持迅雷下载,请使用电脑自带的IE浏览器,或者360浏览器、谷歌浏览器下载即可。
4、本站资源下载后的文档和图纸-无水印,预览文档经过压缩,下载后原文更清晰。
5、试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。

数据结构教学课件:Chapter7

1Given a sequence of records R1,R2,Rn withkey values k1,k2,kn,respectively,arrange therecords into any orders such that recordsRs1,Rs2,Rsn have keys obeying the propertyks1 ks2 ksn.Each record contains a field called the key.Linear order:comparison.Measures of cost:Comparisons&SwapsA sorting algorithm is said to stable if it does not change the relative ordering of records with identical key values.23template void inssort(Elem A,int n)for(int i=1;i0)&(Comp:lt(Aj,Aj-1);j-)swap(A,j,j-1);Best Case:0 swaps,n-1 comparisonsWorst Case:n(n-1)/2 swaps and comparisons Average Case:n(n-1)/4 swaps and comparisons456template void bubsort(Elem A,int n)for(int i=0;ii;j-)if(Comp:lt(Aj,Aj-1)swap(A,j,j-1);Best Case:0 swaps,n(n-1)/2 comparisonsWorst Case:n(n-1)/2 swaps and comparisonsAverage Case:n(n-1)/4 swaps and n(n-1)/2 comparisons.78template void selsort(Elem A,int n)for(int i=0;ii;j-)/Find least if(Comp:lt(Aj,Alowindex)lowindex=j;/Put it in place swap(A,i,lowindex);Best Case:0 swaps(n-1 as written),n(n-1)/2 comparisons.Worst Case:n-1 swaps and n(n-1)/2 comparisonsAverage Case:O(n)swaps and n(n-1)/2 comparisons910InsertionBubbleSelectionComparisons:Best Case(n)(n2)(n2)Average Case(n2)(n2)(n2)Worst Case(n2)(n2)(n2)SwapsBest Case00(n)Average Case(n2)(n2)(n)Worst Case(n2)(n2)(n)11Do exercises:Trace by hand the situation of the array(int a=21,25,49,25*,16,08)using Insertion Sort algorithm,Bubble Sort algorithm and Selection sort algorithm respectively.Tell which algorithm is stable or not?1213/Modified version of Insertion Sorttemplate void inssort2(Elem A,int n,int incr)for(int i=incr;i=incr)&(Comp:lt(Aj,Aj-incr);j-=incr)swap(A,j,j-incr);template void shellsort(Elem A,int n)/Shellsort for(int i=n/2;i2;i/=2)/For each incr for(int j=0;ji;j+)/Sort sublists inssort2(&Aj,n-j,i);inssort2(A,n,1);14Do exercises:Trace by hand the situation of the array(int a=49,38,65,97,76,13,27,49*,55,04)using Shellsort(dk=5,3,1).Tell the algorithm is stable or not?15template void qsort(Elem A,int i,int j)if(j=i)return;/List too small int pivotindex=findpivot(A,i,j);swap(A,pivotindex,j);/Put pivot at end /k will be first position on right side int k=partition(A,i-1,j,Aj);swap(A,k,j);/Put pivot in place qsort(A,i,k-1);qsort(A,k+1,j);Divide and Conquer:divide list into values less than pivot and values greater than pivot.Initial call:qsort(array,0,n-1);16template int findpivot(Elem A,int i,int j)return(i+j)/2;template int partition(Elem A,int l,int r,Elem&pivot)do /Move the bounds in until they meet while(Comp:lt(A+l,pivot);while(r!=0)&Comp:gt(A-r,pivot);swap(A,l,r);/Swap out-of-place values while(l r);/Stop when they cross swap(A,l,r);/Reverse last swap return l;/Return first pos on right171819Best case:Always partition in half.(n log n)Worst case:Bad partition.(n2)Average case:T(n)=cn+1/n(T(k)+T(n-1-k)=(n log n)Optimizations for Quicksort:Better Pivot Better algorithm for small sublists Eliminate recursionk=0n-120Do exercises:Trace by hand the execution of Quicksort algorithm on the array:int a=44,77,55,99,66,33,22,88,77*.Tell the algorithm is stable or not?21List mergesort(List inlist)if(inlist.length()=1)return inlist;List l1=half of the items from inlist;List l2=other half of items from inlist;return merge(mergesort(l1),mergesort(l2);22template void mergesort(Elem A,Elem temp,int left,int right)int mid=(left+right)/2;if(left=right)return;mergesort(A,temp,left,mid);mergesort(A,temp,mid+1,right);for(int i=left;i=right;i+)/Copy tempi=Ai;int i1=left;int i2=mid+1;for(int curr=left;curr right)/Right exhausted Acurr=tempi1+;else if(Comp:lt(tempi1,tempi2)Acurr=tempi1+;else Acurr=tempi2+;23template void mergesort(Elem A,Elem temp,int left,int right)if(right-left)=THRESHOLD)inssort(&Aleft,right-left+1);return;int i,j,k,mid=(left+right)/2;if(left=right)return;mergesort(A,temp,left,mid);mergesort(A,temp,mid+1,right);for(i=mid;i=left;i-)tempi=Ai;for(j=1;j=right-mid;j+)tempright-j+1=Aj+mid;for(i=left,j=right,k=left;k=right;k+)if(tempi tempj)Ak=tempi+;else Ak=tempj-;24Mergesort cost:(n log n)in the best,average,and worst cases.Mergsort is also good for sorting linked lists.Mergesort requires twice the space.25Do exercises:Trace by hand the execution of Mergesort algorithm on the array:int a=21,25,49,25*,93,62,72,08,37,16,54.Tell the algorithm is stable or not?26School of Software Engineering,SCUT Longcun Jin(金龙存)(金龙存) Oct.18,201327template void heapsort(Elem A,int n)/Heapsort Elem mval;maxheap H(A,n,n);for(int i=0;in;i+)/Now sort H.removemax(mval);/Put max at endUse a max-heap,so that elements end up sorted within the array.Cost of heapsort:build the heap is(n)remove the n elements is(n log n)Cost of finding K largest elements:(n+k log n)282930Do exercises:Trace by hand the execution of Heapsort algorithm on the array:int a=21,25,49,25*,16,08.Tell the algorithm is stable or not?31A simple,efficient sort:for(i=0;in;i+)BAi=Ai;Ways to generalize:Make each bin the head of a list.Allow more keys than records.32template void binsort(Elem A,int n)List BMaxKeyValue;Elem item;for(i=0;in;i+)BAi.append(Ai);for(i=0;iMaxKeyValue;i+)for(Bi.setStart();Bi.getValue(item);Bi.next()output(item);Cost:(n+MaxKeyValue).3334template void radix(Elem A,Elem B,int n,int k,int r,int cnt)/cnti stores number of records in bini int j;for(int i=0,rtok=1;ik;i+,rtok*=r)for(j=0;jr;j+)cntj=0;/Count number of records for each bin for(j=0;jn;j+)cnt(Aj/rtok)%r+;/cntj will be last slot of bin j.for(j=1;j=0;j-)B-cnt(Aj/rtok)%r=Aj;for(j=0;jn;j+)Aj=Bj;3536Cost:(nk+rk)How do n,k,and r relate?If key range is small,then this can be(n).If there are n distinct keys,then the length of a key must be at least logrn.Thus,Radix Sort is(n logn)in general case37Do exercises:Trace by hand the execution of Radix Sort algorithm on the array:int a=32,13,27,32*,19,33.Tell the algorithm is stable or not?383940We would like to know a lower bound for all possible sorting algorithms.Sorting is O(n log n)(average,worst cases)because we know of algorithms with this upper bound.We will now prove(n log n)lower bound for sorting.4142 There are n!permutations.A sorting algorithm can be viewed as determining which permutation has been input.Each leaf node of the decision tree corresponds to one permutation.A tree with n nodes has(log n)levels,so the tree with n!leaves has(log n!)=(n log n)levels.

注意事项

本文(数据结构教学课件:Chapter7)为本站会员(努力****83)主动上传,装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知装配图网(点击联系客服),我们立即给予删除!

温馨提示:如果因为网速或其他原因下载失败请重新下载,重复下载不扣分。




关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服 - 联系我们

copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!