©Brooks/Cole, 2003 Chapter 8 Algorithms. ©Brooks/Cole, 2003 Understand the concept of an algorithm. Define and use the three constructs for developing.

Slides:



Advertisements
Similar presentations
College of Information Technology & Design
Advertisements

CS1010 Programming Methodology
8 Algorithms Foundations of Computer Science ã Cengage Learning.
CSE115/ENGR160 Discrete Mathematics 02/24/11 Ming-Hsuan Yang UC Merced 1.
Scott Grissom, copyright 2004 Chapter 5 Slide 1 Analysis of Algorithms (Ch 5) Chapter 5 focuses on: algorithm analysis searching algorithms sorting algorithms.
©Brooks/Cole, 2003 Chapter 8 Algorithms. ©Brooks/Cole, 2003 The Origins of “Algorithm”
Pseudocode and Algorithms
©Brooks/Cole, 2001 Chapter 3 Structure of a C Program.
Developing logic (Examples on algorithm and flowchart)
©Brooks/Cole, 2001 Chapter 4 Functions. ©Brooks/Cole, 2001 Figure 4-1.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the basic concepts and uses of arrays ❏ To be able to define C.
Adapted from slides by Marie desJardins
An ordered sequence of unambiguous and well-defined instructions that performs some task and halts in finite time Let's examine the four parts of this.
Algorithmic Problem Solving CMSC 201 Adapted from slides by Marie desJardins (Spring 2015 Prof Chang version)
Computer Science: A Structured Programming Approach Using C1 8-5 Sorting One of the most common applications in computer science is sorting—the process.
Reynolds 2006 Complexity1 Complexity Analysis Algorithm: –A sequence of computations that operates on some set of inputs and produces a result in a finite.
Computer Science: A Structured Programming Approach Using C1 6-9 Recursion In general, programmers use two approaches to writing repetitive algorithms.
Chapter 3 Sec 3.3 With Question/Answer Animations 1.
Chapter 15 Recursion INTRODUCTION Recursion is a program-solving technique that expresses the solution of a problem in terms of the solutions of.
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
Chapter 8 Algorithms. Understand the concept of an algorithm. Define and use the three constructs for developing algorithms: sequence, decision, and repetition.
ALGORITHM CHAPTER 8. Chapter Outlines and Objectives  Define an algorithm and relate it to problem solving.  Define three construct and describe their.
1 The Programming Layer 2 Outline Concepts –Definition –How to develop an algorithm –Essential features Three Constructs Algorithm Representation –Pseudocode.
Chapter 2: General Problem Solving Concepts
Lecture 4: Calculating by Iterating. The while Repetition Statement Repetition structure Programmer specifies an action to be repeated while some condition.
IB Computer Science Unit 5 – Advanced Topics Recursion.
Chapter Algorithms 3.2 The Growth of Functions 3.3 Complexity of Algorithms 3.4 The Integers and Division 3.5 Primes and Greatest Common Divisors.
1 Section 2.1 Algorithms. 2 Algorithm A finite set of precise instructions for performing a computation or for solving a problem.
Algorithms & FlowchartsLecture 10. Algorithm’s CONCEPT.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Algorithms Computer Science: An Overview Tenth Edition by J. Glenn.
Algorithms Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin,
Chapter 8 Algorithms.
Chapter 5 Algorithms (1) Introduction to CS 1 st Semester, 2012 Sanghyun Park.
8.1 8 Algorithms Foundations of Computer Science  Cengage Learning.
©Brooks/Cole, 2003 Chapter 8 Algorithms. ©Brooks/Cole, 2003 CONCEPTCONCEPT 8.1.
Examples of Flow Charts Pseudocodes
1 Algorithms. Algorithms Introduction Recipe for baking a cake…. 2 sticks butter 2 cups flour 1 cup sugar 4 eggs 1 cup milk 1 tsp baking powder Cocoa.
ALGORITHMS AND FLOWCHARTS. Why Algorithm is needed? 2 Computer Program ? Set of instructions to perform some specific task Is Program itself a Software.
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
Chapter 2- Visual Basic Schneider1 Chapter 2 Problem Solving.
CS 116 Object Oriented Programming II Lecture 13 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Program design Program Design Process has 2 phases:
Growth of Functions & Algorithms
Chapter 2 Data Representation.
while Repetition Structure
ALGORITHMS AND FLOWCHARTS
Chapter 4: Control Structures
Chapter 8 Arrays Objectives
Topics discussed in this section:
Sorting Data are arranged according to their values.
Chapter 5: Algorithms Computer Science: An Overview Tenth Edition
Chapter 8 Arrays Objectives
Sorting Data are arranged according to their values.
Introduction to Algorithms and Programming
Algorithm Analysis and Design
Algorithms Key Revision Points.
Binary Search Trees Chapter 7 Objectives
Recursion: The Mirrors
PROBLEM ANALYSIS.
Chapter 8 Arrays Objectives
Teori Bahasa dan Automata Lecture 13: Algorithm
Algorithms.
Applications of Arrays
REPETITION Why Repetition?
Computer Science: An Overview Tenth Edition
Presentation transcript:

©Brooks/Cole, 2003 Chapter 8 Algorithms

©Brooks/Cole, 2003 Understand the concept of an algorithm. Define and use the three constructs for developing algorithms: sequence, decision, and repetition. Understand and use three tools to represent algorithms: flowchart, pseudocode, and structure chart. After reading this chapter, the reader should be able to: O BJECTIVES Understand the concept of modularity and subalgorithms. List and comprehend common algorithms.

©Brooks/Cole, 2003 CONCEPTCONCEPT 8.1

Figure 8-1 Informal definition of an algorithm used in a computer

©Brooks/Cole, 2003 Figure 8-2 Finding the largest integer among five integers

©Brooks/Cole, 2003 Figure 8-3 Defining actions in FindLargest algorithm

©Brooks/Cole, 2003 Figure 8-4 FindLargest refined

©Brooks/Cole, 2003 Figure 8-5 Generalization of FindLargest

©Brooks/Cole, 2003 THREE CONSTRUCTS 8.2

©Brooks/Cole, 2003 Figure 8-6 Three constructs

©Brooks/Cole, 2003 ALGORITHMREPRESENTATIONALGORITHMREPRESENTATION 8.3

Figure 8-7 Flowcharts for three constructs

©Brooks/Cole, 2003 Figure 8-8 Pseudocode for three constructs

©Brooks/Cole, 2003 Example 1 Write an algorithm in pseudocode that finds the average of two numbers Solution See Algorithm 8.1 on the next slide.

©Brooks/Cole, 2003 AverageOfTwo Input: Two numbers 1.Add the two numbers 2.Divide the result by 2 3.Return the result by step 2 End Algorithm 8.1: Average of two

©Brooks/Cole, 2003 Example 2 Write an algorithm to change a numeric grade to a pass/no pass grade. Solution See Algorithm 8.2 on the next slide.

©Brooks/Cole, 2003 Pass/NoPassGrade Input: One number 1.if (the number is greater than or equal to 70) then 1.1 Set the grade to “pass” else 1.2 Set the grade to “nopass” End if 2.Return the grade End Algorithm 8.2: Pass/no pass Grade

©Brooks/Cole, 2003 Example 3 Write an algorithm to change a numeric grade to a letter grade. Solution See Algorithm 8.3 on the next slide.

©Brooks/Cole, 2003 LetterGrade Input: One number 1. if (the number is between 90 and 100, inclusive) then 1.1 Set the grade to “A” End if 2.if (the number is between 80 and 89, inclusive) then 2.1 Set the grade to “B” End if Algorithm 8.3: Letter grade Continues on the next slide

©Brooks/Cole, if (the number is between 70 and 79, inclusive) then 3.1 Set the grade to “C” End if 4.if (the number is between 60 and 69, inclusive) then 4.1 Set the grade to “D” End if Algorithm 8.3: Letter grade (continued) Continues on the next slide

©Brooks/Cole, If (the number is less than 60) then 5.1 Set the grade to “F” End if 6.Return the grade End Algorithm 8.3: Letter grade (continued)

©Brooks/Cole, 2003 Example 4 Write an algorithm to find the largest of a set of numbers. You do not know the number of numbers. Solution See Algorithm 8.4 on the next slide.

©Brooks/Cole, 2003 FindLargest Input: A list of positive integers 1.Set Largest to 0 2.while (more integers) 2.1 if (the integer is greater than Largest) then Set largest to the value of the integer End if End while 3.Return Largest End Algorithm 8.4: Find largest

©Brooks/Cole, 2003 Example 5 Write an algorithm to find the largest of 1000 numbers. Solution See Algorithm 8.5 on the next slide.

©Brooks/Cole, 2003 FindLargest Input: 1000 positive integers 1.Set Largest to 0 2.Set Counter to 0 3.while (Counter less than 1000) 3.1 if (the integer is greater than Largest) then Set Largest to the value of the integer End if 3.2 Increment Counter End while 4.Return Largest End Algorithm 8.5: Find largest of 1000 numbers

©Brooks/Cole, 2003 MORE FORMAL DEFINITION DEFINITION 8.4

©Brooks/Cole, 2003 SUBALGORITHMSSUBALGORITHMS 8.5

Figure 8-9 Concept of a subalgorithm

©Brooks/Cole, 2003 FindLargest Input: A list of positive integers 1.Set Largest to 0 2.while (more integers) 2.1 FindLarger End while 3.Return Largest End Algorithm 8.6: Find largest

©Brooks/Cole, 2003 FindLarger Input: Largest and current integer 1.if (the integer is greater than Largest) then 1.1 Set Largest to the value of the integer End if End Subalgorithm: Find larger

©Brooks/Cole, 2003 BASICALGORITHMSBASICALGORITHMS 8.6

Figure 8-10 Summation

©Brooks/Cole, 2003 Figure 8-11 Product

©Brooks/Cole, 2003 Figure 8-12 Selection sort

©Brooks/Cole, 2003 Figure 8-13: part I Example of selection sort

©Brooks/Cole, 2003 Figure 8-13: part II Example of selection sort

©Brooks/Cole, 2003 Figure 8-14 Selection sort algorithm

©Brooks/Cole, 2003 Figure 8-15 Bubble sort

©Brooks/Cole, 2003 Figure 8-16: part I Example of bubble sort

©Brooks/Cole, 2003 Figure 8-16: part II Example of bubble sort

©Brooks/Cole, 2003 Figure 8-17 Insertion sort

©Brooks/Cole, 2003 Figure 8-18: part I Example of insertion sort

©Brooks/Cole, 2003 Figure 8-18: part II Example of insertion sort

©Brooks/Cole, 2003 Figure 8-19 Search concept

©Brooks/Cole, 2003 Figure 8-20: Part I Example of a sequential sort

©Brooks/Cole, 2003 Figure 8-20: Part II Example of a sequential sort

©Brooks/Cole, 2003 Figure 8-21 Example of a binary sort

©Brooks/Cole, 2003 RECURSIONRECURSION 8.1

Figure 8-22 Iterative definition of factorial

©Brooks/Cole, 2003 Figure 8-23 Recursive definition of factorial

©Brooks/Cole, 2003 Figure 8-24 Tracing recursive solution to factorial problem

©Brooks/Cole, 2003 Factorial Input: A positive integer num 1.Set FactN to 0 2.Set i to 1 3.while (i is less than or equal to num) 3.1 Set FactN to FactN x I 3.2 Increment i End while 4.Return FactN End Algorithm 8.7: Iterative factorial

©Brooks/Cole, 2003 Factorial Input: A positive integer num 1.if (num is equal to 0) then 1.1 return 1 else 1.2 return num x Factorial (num – 1) End if End Algorithm 8.8: Recursive factorial