数据结构.ppt
《数据结构.ppt》由会员分享,可在线阅读,更多相关《数据结构.ppt(15页珍藏版)》请在课桌文档上搜索。
1、CHAPTER 2ALGORITHM ANALYSIS,【Definition】An algorithm is a finite set of instructions that,if followed,accomplishes a particular task.In addition,all algorithms must satisfy the following criteria:(1)Input There are zero or more quantities that are externally supplied.(2)Output At least one quantity
2、is produced.(3)Definiteness Each instruction is clear and unambiguous.(4)Finiteness If we trace out the instructions of an algorithm,then for all cases,the algorithm terminates after finite number of steps.(5)Effectiveness Every instruction must be basic enough to be carried out,in principle,by a pe
3、rson using only pencil and paper.It is not enough that each operation be definite as in(3);it also must be feasible.,1/15,Note:A program is written in some programming language,and does not have to be finite(e.g.an operation system).An algorithm can be described by human languages,flow charts,some p
4、rogramming languages,or pseudo-code.,Example Selection Sort:Sort a set of n 1 integers in increasing order.,From those integers that are currently unsorted,find the smallest and place it next in the sorted list.,Where and how are they stored?,Where?,for(i=0;i n;i+)Examine listi to listn1 and suppose
5、 that the smallest integer is at listmin;Interchange listi and listmin;,Sort=Find the smallest integer+Interchange it with listi.,Algorithm in pseudo-code,2/15,1 What to Analyze,Machine&compiler-dependent run times.Time&space complexities:machine&compiler-independent.,Assumptions:instructions are ex
6、ecuted sequentially each instruction is simple,and takes exactly one time unit integer size is fixed and we have infinite memory,Typically the following two functions are analyzed:Tavg(N)&Tworst(N)-the average and worst case time complexities,respectively,as functions of input size N.,If there is mo
7、re than one input,these functions may have more than one argument.,3/15,1 What to Analyze,Example Matrix addition,/*rows+1*/,/*rows(cols+1)*/,/*rows cols*/,T(rows,cols)=2 rows cols+2rows+1,Q:What shall we do if rows cols?,A:Exchange rows and cols.,4/15,ExampleIterative function for summing a list of
8、 numbers,/*count=1*/,/*count+*/,/*count+for last execution of for*/,/*count+*/,/*count+*/,Tsum(n)=2n+3,ExampleRecursive function for summing a list of numbers,/*count+*/,/*count+*/,/*count+*/,Trsum(n)=2n+2But it takes more time to compute each step.,1 What to Analyze,5/15,1 What to Analyze,Is it rea
9、lly necessary to count the exact number of steps?,Uhhh.I dont think so.,Why not?,Because it drives me crazy!,So its too complicated sometimes.But does it worth the effort?,Take the iterative and recursive programs for summing a list for example-if you think 2n+2 is less than 2n+3,try a large n and y
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 数据结构
链接地址:https://www.desk33.com/p-229775.html