已阅读5页,还剩61页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
Introduction to Algorithms 计算机算法导论,20072008年第一学期,Quiz(10 minutes),Question Suppose we are comparing implementations of insertion sort and merge sort on the same machine. For inputs of size n, insertion sort runs in 8n2 steps, while merge sort runs in 64nlgn steps. For which values of n does insertion sort beat merge sort? What is the smallest value of n such that an algorithm whose running time is 100n2 runs faster than an algorithm whose running time is 2n on the same machine?,2、Getting Started,Question Introducing the insertion sort algorithm to solve the solving problem Analyzing its running time Designing the algorithm by the divide-and-conquer approach,2.1、Insertion sort,Problem,Input: sequence of numbers. Output: permutation such that a1 a2 an . Example: Input: 8 2 4 9 3 6 Output: 2 3 4 6 8 9,Pseudocode,Difference between pseudocode and real code,Pseudocode is more clear and concise Sometimes, in English Pseudocode is not typically concerned with issue of software engineering,Pseudocode conventions,Indentation indicates block structure The looping constructs while, for and repeat and the conditional constructs if, then and else have interpretations similar to those in Pascal The “ ” indicates a comment A multiple assignment Varibles are local to the given procedure A1j Field objects indicates compound data, A variable representing an array or object is treated as a pointer, and NIL pointer Parameters are passed to a procedure by value The boolean operators is short circuiting,Pseudcode,2.1、Insertion sort,1.1、Algorithms,Correct and incorrect algorthms The specification of an algorthm In english A computer program A hareware design,Loop invariants,Initialization: It is true prior to the first iteration of the loop Maintenance: If it is true before an iteration, it remains true before the next iteration Termination: When the loop terminates, the invariant can helps show that the algorithm is correct,The correctness of insertion sort,Initialization: The loop invariant holds before the first loop iteration, j=2. Maintenance: Each iteration maintains the loop invariant. Termination: When the loop terminates, the entire array is sorted, which means that the algorithm is correct,2.2、Analyzing algorithms,Analyzing algorithms,Analyzing algorithms means predicting the resources that the algorithm requires. Such as, Computational time Memory Communication bandwidth Computer hardware,RAM(random-access machine) model,Instructions: Arithmetic (add, subtract, multiply, divide,) Data movement (load, store, copy,) Control (conditional and unconditional brand, return,) A grey area, such as exponentiation Data types: Integer Floating point,Running time,The running time depends on the input: an already sorted sequence is easier to sort. Parameterize the running time by the size of the input, since short sequences are easier to sort than long ones. Generally, we seek upper bounds on the running time, because everybody likes a guarantee.,Pseudocode,Kinds of analyses,Worst-case: (usually) T(n) = maximum time of algorithm on any input of size n. Average-case: (sometimes) T(n) = expected time of algorithm over all inputs of size n. Need assumption of statistical distribution of inputs. Best-case: (bogus) Cheat with a slow algorithm that works fast on some input.,Kinds of analyses,Worst-case: (usually) Best-case: (bogus),Worst-case and average-case analysis,2.3 Designing algorithms,The divide-and-conquer approach,The divide-and-conquer approach,Example,Merge,Machine-independent time,What is insertion sorts worst-case time? It depends on the speed of ou
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年氢燃料电池测试设备校准规范
- 依兰六年级科学测试卷
- 2026年环境影响评价工程师考试(环境影响评价案例分析)真题试卷
- 在秦皇岛做销售:双碳传媒如何重构“国家级项目”的资源链接逻辑
- 周末巧安排(第1课时)教案-2026-2027学年道德与法治二年级上册统编版
- 护理人文关怀实践
- 2026年度工业电机维修服务合同
- 护理查房课件获奖技巧
- 新生儿喂养与护理技巧大全
- 有没有适合护理科研立项参考的课件或文献资料
- 短视频编辑合作协议书
- 昌吉回族自治州奇台县公共基础辅警考试笔试题库及答案
- 护理记录对特殊患者(如过敏)的记录疏漏案例
- 2026年广东省深圳市34校联考中考二模化学试卷(含答案)
- 复式条形统计图
- 污水管网施工高温天气作业安全方案
- 统编版高中政治选择性必修三《逻辑与思维》综合题刷题练习题(含答案)
- (二模)南通市2026届高三第一次调研测试历史试卷(含答案)
- 第11课 少年当自强(课件) 小学道德与法治二年级下册
- (二检)2026年宝鸡市高三高考模拟检测(二)历史试卷
- 餐饮业面试流程及常见问题
评论
0/150
提交评论