数据结构教学课件:Chapter7

上传人:努力****83 文档编号:190958238 上传时间:2023-03-01 格式:PPT 页数:42 大小:573KB
收藏 版权申诉 举报 下载
数据结构教学课件:Chapter7_第1页
第1页 / 共42页
数据结构教学课件:Chapter7_第2页
第2页 / 共42页
数据结构教学课件:Chapter7_第3页
第3页 / 共42页
资源描述:

《数据结构教学课件:Chapter7》由会员分享,可在线阅读,更多相关《数据结构教学课件:Chapter7(42页珍藏版)》请在装配图网上搜索。

1、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 algorit

2、hm 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 com

3、parisons456template 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(C

4、omp: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)Wor

5、st 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

6、/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,

7、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

8、/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)retur

9、n(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 right171819

10、Best 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 arra

11、y: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,in

12、t 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=t

13、empi2+;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

14、+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

15、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 m

16、ax-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

17、 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.

18、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 bi

19、n 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 executio

20、n 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.

展开阅读全文
温馨提示:
1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
2: 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
3.本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服 - 联系我们

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

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


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