版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、1 The Role of Algorithms in Computing,1 what are algorithms? 2 why is the study of algorithms worthwhile? 3 what is the role of algorithms?,An algorithm is said to be correct if, for every input instance, it halts with the correct output,The problem of sorting,Input: sequence of numbers. Output: per
2、mutation such that a 1 a 2 a n . Example: 解释什么是实例? Input: 8 2 4 9 3 6 Output: 2 3 4 6 8 9,Time Is Important,“Time is important for everybody, especially for the computer scientist who studies algorithms.” J. S. Yu,What kinds of problems are solved by algorithms? Levels of Hardness 1. There is no met
3、hod to solve the problem in finitely many steps. 2. Theoretically, there is an algorithm, but the running time increases too much with the size of input, even to the universe age(NPC problems). 3. Among the good algorithms, there is still a hierarchy of running time.,多项式算法:把渐近复杂性与规模N的幂同阶的这类算法称为多项式算法
4、。 指数型算法:把渐近复杂性与规模N的指数同阶的这类算法称为指数型算法。 这两种算法在效率上有质的区别的内在原因是算法渐近复杂性的阶的区别。可见,算法的渐近复杂性的阶对于算法的效率有着决定性的意义。 多项式算法是有效的算法。很多问题都有多项式算法。但也有一些问题还未找到多项式算法,只找到指数型算法。,Homework,1.1-3, 1.1-4,1.2 Algorithms as a technology,Suppose computers were infinitely fast and computer memory was free. Would you have any reason t
5、o study algorithms? Compare the running time of insert sort and merge sort.,Why study algorithms andperformance?, Algorithms help us to understand scalability. Performance often draws the line between what is feasible and what is impossible. Algorithmic mathematics provides a language for talking ab
6、out program behavior., Performance is the currency of computing. The lessons of program performance generalize to other computing resources. Speed is fun!,2 Getting Started,Analysis of Algorithms Insertion Sort Merge Sort,The problem of sorting,Input: sequence of numbers. Output: permutation such th
7、at a 1 a 2 a n . Example: Input: 8 2 4 9 3 6 Output: 2 3 4 6 8 9,An Example: Insertion Sort,InsertionSort(A, n) for i = 2 to n key = Aij = i - 1;while (j 0) and (Aj key) Aj+1 = Ajj = j - 1Aj+1 = key ,An Example: Insertion Sort,InsertionSort(A, n) for i = 2 to n key = Aij = i - 1;while (j 0) and (Aj
8、key) Aj+1 = Ajj = j - 1Aj+1 = key ,30,10,40,20,1,2,3,4,i = j = key = Aj = Aj+1 = ,An Example: Insertion Sort,InsertionSort(A, n) for i = 2 to n key = Aij = i - 1;while (j 0) and (Aj key) Aj+1 = Ajj = j - 1Aj+1 = key ,30,10,40,20,1,2,3,4,i = 2j = 1key = 10Aj = 30 Aj+1 = 10,An Example: Insertion Sort,
9、InsertionSort(A, n) for i = 2 to n key = Aij = i - 1;while (j 0) and (Aj key) Aj+1 = Ajj = j - 1Aj+1 = key ,30,30,40,20,1,2,3,4,i = 2j = 1key = 10Aj = 30 Aj+1 = 30,An Example: Insertion Sort,InsertionSort(A, n) for i = 2 to n key = Aij = i - 1;while (j 0) and (Aj key) Aj+1 = Ajj = j - 1Aj+1 = key ,3
10、0,30,40,20,1,2,3,4,i = 2j = 1key = 10Aj = 30 Aj+1 = 30,An Example: Insertion Sort,InsertionSort(A, n) for i = 2 to n key = Aij = i - 1;while (j 0) and (Aj key) Aj+1 = Ajj = j - 1Aj+1 = key ,30,30,40,20,1,2,3,4,i = 2j = 0key = 10Aj = Aj+1 = 30,An Example: Insertion Sort,InsertionSort(A, n) for i = 2
11、to n key = Aij = i - 1;while (j 0) and (Aj key) Aj+1 = Ajj = j - 1Aj+1 = key ,30,30,40,20,1,2,3,4,i = 2j = 0key = 10Aj = Aj+1 = 30,An Example: Insertion Sort,InsertionSort(A, n) for i = 2 to n key = Aij = i - 1;while (j 0) and (Aj key) Aj+1 = Ajj = j - 1Aj+1 = key ,10,30,40,20,1,2,3,4,i = 2j = 0key
12、= 10Aj = Aj+1 = 10,An Example: Insertion Sort,InsertionSort(A, n) for i = 2 to n key = Aij = i - 1;while (j 0) and (Aj key) Aj+1 = Ajj = j - 1Aj+1 = key ,10,30,40,20,1,2,3,4,i = 3j = 0key = 10Aj = Aj+1 = 10,An Example: Insertion Sort,InsertionSort(A, n) for i = 2 to n key = Aij = i - 1;while (j 0) a
13、nd (Aj key) Aj+1 = Ajj = j - 1Aj+1 = key ,10,30,40,20,1,2,3,4,i = 3j = 0key = 40Aj = Aj+1 = 10,An Example: Insertion Sort,InsertionSort(A, n) for i = 2 to n key = Aij = i - 1;while (j 0) and (Aj key) Aj+1 = Ajj = j - 1Aj+1 = key ,10,30,40,20,1,2,3,4,i = 3j = 0key = 40Aj = Aj+1 = 10,An Example: Inser
14、tion Sort,InsertionSort(A, n) for i = 2 to n key = Aij = i - 1;while (j 0) and (Aj key) Aj+1 = Ajj = j - 1Aj+1 = key ,10,30,40,20,1,2,3,4,i = 3j = 2key = 40Aj = 30 Aj+1 = 40,An Example: Insertion Sort,InsertionSort(A, n) for i = 2 to n key = Aij = i - 1;while (j 0) and (Aj key) Aj+1 = Ajj = j - 1Aj+
15、1 = key ,10,30,40,20,1,2,3,4,i = 3j = 2key = 40Aj = 30 Aj+1 = 40,An Example: Insertion Sort,InsertionSort(A, n) for i = 2 to n key = Aij = i - 1;while (j 0) and (Aj key) Aj+1 = Ajj = j - 1Aj+1 = key ,10,30,40,20,1,2,3,4,i = 3j = 2key = 40Aj = 30 Aj+1 = 40,An Example: Insertion Sort,InsertionSort(A,
16、n) for i = 2 to n key = Aij = i - 1;while (j 0) and (Aj key) Aj+1 = Ajj = j - 1Aj+1 = key ,10,30,40,20,1,2,3,4,i = 4j = 2key = 40Aj = 30 Aj+1 = 40,An Example: Insertion Sort,InsertionSort(A, n) for i = 2 to n key = Aij = i - 1;while (j 0) and (Aj key) Aj+1 = Ajj = j - 1Aj+1 = key ,10,30,40,20,1,2,3,
17、4,i = 4j = 2key = 20Aj = 30 Aj+1 = 40,An Example: Insertion Sort,InsertionSort(A, n) for i = 2 to n key = Aij = i - 1;while (j 0) and (Aj key) Aj+1 = Ajj = j - 1Aj+1 = key ,10,30,40,20,1,2,3,4,i = 4j = 2key = 20Aj = 30 Aj+1 = 40,An Example: Insertion Sort,InsertionSort(A, n) for i = 2 to n key = Aij
18、 = i - 1;while (j 0) and (Aj key) Aj+1 = Ajj = j - 1Aj+1 = key ,10,30,40,20,1,2,3,4,i = 4j = 3key = 20Aj = 40 Aj+1 = 20,An Example: Insertion Sort,InsertionSort(A, n) for i = 2 to n key = Aij = i - 1;while (j 0) and (Aj key) Aj+1 = Ajj = j - 1Aj+1 = key ,10,30,40,20,1,2,3,4,i = 4j = 3key = 20Aj = 40
19、 Aj+1 = 20,An Example: Insertion Sort,InsertionSort(A, n) for i = 2 to n key = Aij = i - 1;while (j 0) and (Aj key) Aj+1 = Ajj = j - 1Aj+1 = key ,10,30,40,40,1,2,3,4,i = 4j = 3key = 20Aj = 40 Aj+1 = 40,An Example: Insertion Sort,InsertionSort(A, n) for i = 2 to n key = Aij = i - 1;while (j 0) and (A
20、j key) Aj+1 = Ajj = j - 1Aj+1 = key ,10,30,40,40,1,2,3,4,i = 4j = 3key = 20Aj = 40 Aj+1 = 40,An Example: Insertion Sort,InsertionSort(A, n) for i = 2 to n key = Aij = i - 1;while (j 0) and (Aj key) Aj+1 = Ajj = j - 1Aj+1 = key ,10,30,40,40,1,2,3,4,i = 4j = 3key = 20Aj = 40 Aj+1 = 40,An Example: Inse
21、rtion Sort,InsertionSort(A, n) for i = 2 to n key = Aij = i - 1;while (j 0) and (Aj key) Aj+1 = Ajj = j - 1Aj+1 = key ,10,30,40,40,1,2,3,4,i = 4j = 2key = 20Aj = 30 Aj+1 = 40,An Example: Insertion Sort,InsertionSort(A, n) for i = 2 to n key = Aij = i - 1;while (j 0) and (Aj key) Aj+1 = Ajj = j - 1Aj
22、+1 = key ,10,30,40,40,1,2,3,4,i = 4j = 2key = 20Aj = 30 Aj+1 = 40,An Example: Insertion Sort,InsertionSort(A, n) for i = 2 to n key = Aij = i - 1;while (j 0) and (Aj key) Aj+1 = Ajj = j - 1Aj+1 = key ,10,30,30,40,1,2,3,4,i = 4j = 2key = 20Aj = 30 Aj+1 = 30,An Example: Insertion Sort,InsertionSort(A,
23、 n) for i = 2 to n key = Aij = i - 1;while (j 0) and (Aj key) Aj+1 = Ajj = j - 1Aj+1 = key ,10,30,30,40,1,2,3,4,i = 4j = 2key = 20Aj = 30 Aj+1 = 30,An Example: Insertion Sort,InsertionSort(A, n) for i = 2 to n key = Aij = i - 1;while (j 0) and (Aj key) Aj+1 = Ajj = j - 1Aj+1 = key ,10,30,30,40,1,2,3
24、,4,i = 4j = 1key = 20Aj = 10 Aj+1 = 30,An Example: Insertion Sort,InsertionSort(A, n) for i = 2 to n key = Aij = i - 1;while (j 0) and (Aj key) Aj+1 = Ajj = j - 1Aj+1 = key ,10,30,30,40,1,2,3,4,i = 4j = 1key = 20Aj = 10 Aj+1 = 30,An Example: Insertion Sort,InsertionSort(A, n) for i = 2 to n key = Ai
25、j = i - 1;while (j 0) and (Aj key) Aj+1 = Ajj = j - 1Aj+1 = key ,10,20,30,40,1,2,3,4,i = 4j = 1key = 20Aj = 10 Aj+1 = 20,An Example: Insertion Sort,InsertionSort(A, n) for i = 2 to n key = Aij = i - 1;while (j 0) and (Aj key) Aj+1 = Ajj = j - 1Aj+1 = key ,10,20,30,40,1,2,3,4,i = 4j = 1key = 20Aj = 1
26、0 Aj+1 = 20,Done!,Animating Insertion Sort,Try it out with random, ascending, and descending inputs Homework:2.1-1, 2.1-2, 2.1-3,2.2 Analyzing algorithm, 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
27、 short sequences are easier to sort than long ones. Generally, we seek upper bounds on the running time, because everybody likes a guarantee.,时间复杂性和空间复杂性,P25-26详细讲解,Kinds of analyses,Worst-case: (usually) T(n) = maximum time of algorithm on any input of size n. Average-case: (sometimes) T(n) = expec
28、ted time of algorithm over all inputs of size n. Need assumption of statistical distribution of inputs.,P26,Best-case: (bogus) Cheat with a slow algorithm that works fast on some input.,Machine-independent time,What is insertion sorts worst-case time? It depends on the speed of our computer: relative speed (on the same machine), absolute speed (on different machines). BIG IDEA: Ignore machine-dependent constants. Look at growth of T(n) as n .,Inserti
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 幼儿园开学第一课观后感心得7篇2021
- 2025-2026学年二代目教学设计图片
- 10.5(1)《角平分线》教学设计 鲁教版(五四制)数学七年级下册
- 1.2 国家的政权组织形式 教学设计-2025-2026学年高中政治统编版选择性必修一当代国际政治与经济
- 2025-2026学年电脑窗口认识教学设计
- 2025-2026学年橱柜设计下单教学流程
- 河南省TOP二十名校2025-2026学年高二上学期10月调研考试物理试卷(B卷)
- 高端项目发布会
- 1.1 人口分布 教学设计 2025-2026学年人教版(2019)高中地理必修第二册
- 队列训练:培养纪律意识小学主题班会课件
- 2026年浙江嘉兴南湖区社区工作者招聘考试-含答案解析
- 2026河南开封工程职业学院招聘57人备考题库及答案详解一套
- 2026年陕西榆林能源集团社会招聘(279人)笔试参考题库及答案详解
- 2026江苏盐城东台市公证处招聘公证员助理2人笔试备考试题及答案详解
- 2026春苏教版五年级下册数学期末综合练习卷含参考答案 (三套)
- 2025年江苏镇江市初二学业水平地生会考考试试题及答案
- 2026版《国有企业领导人员廉洁从业规定》全文+新旧对比+高频考点+习题答案详解
- GB/T 47529-2026自助仓储运营管理规范
- 简易电子琴设计
- 2025年广东省珠海市八年级地理生物会考真题试卷+解析及答案
- 2025年经开投集团招聘真题及答案解析
评论
0/150
提交评论