chapter 5: algorithms

24
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Algorithms Computer Science: An Overview Tenth Edition by J. Glenn Brookshear Modified by Marie desJardins for UMBC’s CMSC 100, Fall2008

Upload: colin-neal

Post on 03-Jan-2016

32 views

Category:

Documents


2 download

DESCRIPTION

Chapter 5: Algorithms. Computer Science: An Overview Tenth Edition by J. Glenn Brookshear Modified by Marie desJardins for UMBC’s CMSC 100, Fall2008. Chapter 5: Algorithms. 5.1 The Concept of an Algorithm 5.2 Algorithm Representation 5.3 Algorithm Discovery 5.4 Iterative Structures - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Chapter 5: Algorithms

Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Chapter 5:Algorithms

Computer Science: An OverviewTenth Edition

by J. Glenn Brookshear

Modified by Marie desJardins for UMBC’s CMSC 100, Fall2008

Page 2: Chapter 5: Algorithms

5-2Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

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 (skipping for now)

• 5.6 Efficiency and Correctness (coming back to this later)

Page 3: Chapter 5: Algorithms

5-3Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Definition of Algorithm

An algorithm is an ordered set of unambiguous, executable steps that defines a terminating process.

Page 4: Chapter 5: Algorithms

5-4Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Algorithm Representation

• Requires well-defined primitives

• A collection of primitives constitutes a programming language.

Page 5: Chapter 5: Algorithms

5-5Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Figure 5.2 Folding a bird from a square piece of paper

Page 6: Chapter 5: Algorithms

5-6Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Figure 5.3 Origami primitives

Page 7: Chapter 5: Algorithms

5-7Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Pseudocode Primitives

•Assignment

name expression

•Conditional selection

if condition then action

Page 8: Chapter 5: Algorithms

5-8Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Pseudocode Primitives (continued)

•Repeated executionwhile condition do activity

•Procedureprocedure name (generic names)

Page 9: Chapter 5: Algorithms

5-9Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Figure 5.4 The procedure Greetings in pseudocode

Page 10: Chapter 5: Algorithms

5-10Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

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.

Page 11: Chapter 5: Algorithms

5-11Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Getting a Foot in the Door

• Try working the problem backwards• Solve an easier related problem

– Relax some of the problem constraints– Solve pieces of the problem first (bottom up

methodology)• Stepwise refinement: Divide the problem into

smaller problems (top-down methodology)

Page 12: Chapter 5: Algorithms

5-12Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

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?

Page 13: Chapter 5: Algorithms

5-13Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Figure 5.5

Page 14: Chapter 5: Algorithms

5-14Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Iterative Structures

• Pretest loop:

while (condition) do

(loop body)• Posttest loop:

repeat (loop body)

until(condition)

Page 15: Chapter 5: Algorithms

5-15Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Figure 5.6 The sequential search algorithm in pseudocode

Page 16: Chapter 5: Algorithms

5-16Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Figure 5.7 Components of repetitive control

Page 17: Chapter 5: Algorithms

5-17Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Figure 5.8 The while loop structure

Page 18: Chapter 5: Algorithms

5-18Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Figure 5.9 The repeat loop structure

Page 19: Chapter 5: Algorithms

5-19Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Figure 5.10 Sorting the list Fred, Alex, Diana, Byron, and Carol alphabetically

Page 20: Chapter 5: Algorithms

5-20Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Figure 5.11 The insertion sort algorithm expressed in pseudocode

Page 21: Chapter 5: Algorithms

5-22Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Complexity Classes

Page 22: Chapter 5: Algorithms

5-23Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Figure 5.18 Applying the insertion sort in a worst-case situation

Page 23: Chapter 5: Algorithms

5-24Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Figure 5.19 Graph of the worst-case analysis of the insertion sort algorithm

Page 24: Chapter 5: Algorithms

5-25Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Figure 5.20 Graph of the worst-case analysis of the binary search algorithm