Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Algorithms Computer Science: An Overview Tenth Edition by J. Glenn.

Similar presentations


Presentation on theme: "Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Algorithms Computer Science: An Overview Tenth Edition by J. Glenn."— Presentation transcript:

1 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Algorithms Computer Science: An Overview Tenth Edition by J. Glenn Brookshear

2 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-2 Chapter 5: Algorithms 5.1 The Concept of an Algorithm 5.2 Algorithm Representation 5.3 Algorithm Discovery

3 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-3 Definition of Algorithm An algorithm is an ordered set of unambiguous, executable steps that defines a terminating process.

4 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-4 Figure 5.2 Folding a bird from a square piece of paper

5 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-5 Algorithm Representation Primitive: Well-defined set of building blocks from which algorithm representations can be constructed. Algorithms require well-defined primitives A collection of primitives constitutes a programming language.

6 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-6 Figure 5.3 Origami primitives

7 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-7 Pseudocode A notational system in which ideas can be expressed informally during the algorithm development process Pseudocode primitives: – Assignment –Conditional selection –Repeated execution –Procedure

8 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-8 Pseudocode Primitives Assignment name  expression (assign name the value of expression) Examples: total  a+b X  9

9 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-9 Pseudocode Primitives (continued) Conditional selection if condition then action if condition then action1 else action2 Example 1: if (year is a leap year) then (daily total  total divided by 366) else (daily total  total divided by 365)

10 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-10 Pseudocode Primitives (continued) Conditional selection Example 2: if ( not raining) then ( if (temperature=hot) then (go swimming) else (play golf) ) else (watch television)

11 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Pseudocode Primitives (continued) Repeated execution while condition do activity while ( tickets remain to be sold) do (sell a ticket) 5-11

12 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Example:Rewrite the following program segment using a while structure Count  1 repeat (print the value assigned to Count and Count  Count+1 ) until (Count=5) ---------------------------------------------------------------- Count  1 while (Count < 5) do ( print the value assigned to Count and Count  Count + 1) 5-12

13 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-13 Pseudocode Primitives (continued) Procedure (a pseudocode unit)

14 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Example: Greatest common divisor The Euclidean algorithm finds the greatest common divisor of two positive integers X and Y by the following process: As long as the value of neither X nor Y is zero, continue dividing the larger of the values by the smaller and assigning X and Y the values of the divisor and remainder, respectively 5-14

15 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Example: Greatest common divisor procedure Euclidean if (neither X nor Y is 0) then (divide the larger of X and Y by the smaller; Y ← the value of the remainder; X ← the value of the divisor; and apply Euclidean to X and Y ) 5-15

16 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-16 The Art of Problem Solving Polya’s 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.

17 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-17 Ages of Children Problem Person A is charged with the task of determining the ages of B’s three children –B tells A that the product of the children’s ages is 36. –A replies that another clue is required. –B tells A the sum of the children’s ages. –A replies that another clue is needed. –B tells A that the oldest child plays the piano. –A tells B the ages of the three children. How old are the three children?

18 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-18 Clue 1 : B tells A that the product of the children’s ages is 36

19 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-19 Clue 1 : B tells A that the product of the children’s ages is 36 Clue 2 : B tells A that the sum of the children’s ages (?)

20 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-20 Clue 1 : B tells A that the product of the children’s ages is 36 Clue 2 : B tells A that the sum of the children’s ages Clue 3 : B tells A that oldest child play piano

21 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-21 Getting a Foot in the Door Try working the problem backwards Stepwise refinement: Divide the problem into smaller problems (top-down methodology) A top-down approach an overview of the system is formulated, specifying but not detailing any first-level subsystems (black boxes)

22 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-22 Getting a Foot in the Door Solve an easier related problem –Relax some of the problem constraints –Solve pieces of the problem first (bottom-up methodology) In bottom up- approach, individual base elements of the system are first specified in great detail

23 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Example 1 The following is an addition problem in binary notation. Each letter represents a unique binary digit. Which letter represents 1 and which represents 0? 5-23

24 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Example 2 Design an algorithm for finding all the factors of a positive integer. For example, in the case of the integer 12, your algorithm should report the values 1,2,3,4,6 and 12. X  1 while (X ≤ N) do (if (X divides N) then report X X  X+1 ) 5-24


Download ppt "Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Algorithms Computer Science: An Overview Tenth Edition by J. Glenn."

Similar presentations


Ads by Google