计算机科学概论课件,计算机科学概论 课件chap 05

1、5-1 Chapter 5: Algorithms 5-2 Chapter 5: Algorithms 算法算法 5.1 The Concept of an Algorithm 5.2 Algorithm Representation 5.3 Algorithm Discovery 5.4 Iterative Structures 5.5 Recursive Structures 5.6 Efficiency and Correctness 5-3 Definition of Algorithm 算法的定义算法的定义 An algorithm is an ordered set of unambiguous, executable steps that defines a terminating process. 算法是定义一个算法是定义一个可终止进程可终止进程的一组的一组有有 序的序的、无歧义的无歧义的、可执行可执行的的步骤的集步骤的集 合合 5.1 The Concept of an Algorithm 算法的概念算法的概念 Algorithm算法算法vs.Program程序程序 。

2、vs.Process进程进程 algorithm :an ordered set of unambiguous, executable steps that defines a terminating process. program :a formal representation of an algorithm designed for computer application process :the activity of executing a program 5-4 鸡兔同笼鸡兔同笼问题问题。 大约在大约在15001500年前,年前,孙子算经孙子算经中记载了这个有中记载了这个有 趣的问题趣的问题: “今有雉兔同笼,上有三十五头,今有雉兔同笼,上有三十五头, 下有九十四足,问雉兔各几何?下有九十四足,问雉兔各几何?” 5-5 5.3 Algorithm Discovery算法的发现算法的发现 The development of a program consists of two activities: discovering the underlying algorithm。

3、 (most challenging) representing that algorithm as a program. Polyas Problem Solving Steps 1. Understand the problem. 2. Devise a plan for solving the problem. 3. Carry out the plan. 4. Evaluate the solution for accuracy and its potential as a tool for solving other problems. The basic principles for problem-solving 1. Understand the problem. 2. Get an idea as to how an algorithmic procedure might solve the problem. 3. Formulate the algorithm and represent it as a program. 4. Evaluate the progra。

4、m for accuracy and for its potential as a tool for solving other problems. Top-down methodology 自顶向下方法自顶向下方法 Problem - subproblem Stepwise refinement 逐步求精逐步求精 Bottom-up methodology 自底向上方法自底向上方法 From the specific to the general 5-6 5-7 5.2 Algorithm Representation 算法的表示算法的表示 Primitives 原语原语 Requires well-defined primitives A collection of primitives constitutes a programming language(程序设计语言程序设计语言). Flowcharts 流程图流程图 by which algorithms are represented by geometric shapes connected by arrows Pseud。

5、ocode 伪代码伪代码 Pseudocode - is a notational system in which ideas can be expressed informally during the algorithm development process. 5-8 Pseudocode 伪代码伪代码 Assignment statement 赋值语句赋值语句 name expression Conditional selection 条件语句条件语句 if (condition) then (activity) else (activity) Repeated execution 重复执行重复执行 while (condition) do (activity) Procedure 过程过程 procedure name (generic names) 5-9 Readability 可读性可读性 Indentation 缩进缩进 Comment 注释注释 Naming items 命名命名 Underline to connect word estimated_arrival。

6、_time Pascal casing 样式样式 EstimatedArrivalTime Camel casing estimatedArrivalTime Begin a pseudocode with procedure name. Request a pseudocode by its name if () then (execute the procedure Greetings) 5-10 Figure 5.4 The procedure Greetings in pseudocode 5-11 5.4 Iterative Structures 迭代结构迭代结构 Pretest loop: 前测试循环前测试循环 while (condition) do (loop body) Posttest loop: 后测试循环后测试循环 repeat (loop body) until(condition) For 循环循环 for (condition) (loop body) 5-12 Figure 5.7 Components of repetitive control The。

7、 control of a loop consists of three activities: 初始化初始化 条件测试条件测试 修改修改 5-13 The while loop The repeat loop 5-14 Figure 5.6 The sequential search algorithm in pseudocode 5-15 Figure 5.10 Sorting the list Fred, Alex, Diana, Byron, and Carol alphabetically 5-16 Figure 5.11 The insertion sort algorithm expressed in pseudocode 5-17 5.5 Recursive Structures 递归结构递归结构 The execution of a procedure leads to another execution of the procedure. Multiple activations of the procedure are formed, all but one of。

8、 which are waiting for other activations to complete. The equivalence of iterative and recursive structure 迭代与递归结构等价迭代与递归结构等价 5-18 Figure 5.12 Applying our strategy to search a list for the entry John 5-19 Figure 5.13 A first draft of the binary search technique 5-20 Figure 5.14 The binary search algorithm in pseudocode 5-21 5.6 Efficiency & Correctness 有效性和正确性有效性和正确性 Efficiency You can develop a variety of algorithms to solve the same problem. However, the choice between efficient and inefficie。

9、nt algorithms can make the difference between a practical solution to a problem and an impractical one. Time and storage complexity of the algorithm. Measured as number of instructions executed Big theta notation: Used to represent efficiency classes Example: Insertion sort is in (n2) binary search is in (lg n) Best, worst, and average case analysis 5-22 Figure 5.18 Applying the insertion sort in a worst-case situation 5-23 Figure 5.19 Graph of the worst-case analysis of the insertion sort algorithm 5-24 Figure 5.20 Graph of the worst-case analysis of the binary search algorithm 5-25 Software Verification 软件验证软件验证 Proof of correctness Testing 作业作业 P.207 1 P.215 3 P.232 1,2,4 5-26 小结小结 重点重点 5.4 While-d。

《计算机科学概论 课件chap 05》由会员aa****6分享,可在线阅读,更多相关《计算机科学概论 课件chap 05》请在金锄头文库上搜索。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值