©Brooks/Cole, 2003 Chapter 8 Algorithms. ©Brooks/Cole, 2003 CONCEPTCONCEPT 8.1.

Slides:



Advertisements
Similar presentations
College of Information Technology & Design
Advertisements

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Chapter 2 - Problem Solving
CS1010 Programming Methodology
8 Algorithms Foundations of Computer Science ã Cengage Learning.
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
Unit 171 Algorithms and Problem Solving  Introduction  Algorithm Design  Algorithm Properties  Algorithm Control Flow  Examples  Comparing Algorithms.
Searching and Sorting Arrays
CHAPTER 10 Recursion. 2 Recursive Thinking Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 9 Searching.
ALGORITHMS AND FLOW CHARTS 1 Adapted from the slides Prepared by Department of Preparatory year Prepared by: lec. Ghader Kurdi.
Adapted from slides by Marie desJardins
© 2004 Pearson Addison-Wesley. All rights reserved October 27, 2006 Recursion (part 2) ComS 207: Programming I (in Java) Iowa State University, FALL 2006.
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)
Problem Solving and Algorithms
©Brooks/Cole, 2003 Chapter 8 Algorithms. ©Brooks/Cole, 2003 Understand the concept of an algorithm. Define and use the three constructs for developing.
Chapter 9: Recursion1 CHAPTER 9 RECURSION. Recursion  Concept of recursion  A recursive: Benefit and Cost  Comparison : Iterative and recursive functions.
M180: Data Structures & Algorithms in Java
Chapter 12 Recursion, Complexity, and Searching and Sorting
Introduction Algorithms and Conventions The design and analysis of algorithms is the core subject matter of Computer Science. Given a problem, we want.
Chapter 15 Recursion INTRODUCTION Recursion is a program-solving technique that expresses the solution of a problem in terms of the solutions of.
CSE 102 Introduction to Computer Engineering What is an Algorithm?
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
IB Computer Science Unit 5 – Advanced Topics Recursion.
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.
Algorithms Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin,
ALGORITHMS.
Chapter 8 Algorithms.
Principles of Programming - NI Simple Recursion Recursion is where a function calls itself. Concept of recursive function: A recursive function is.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12 Recursion.
Chapter 5 Algorithms (1) Introduction to CS 1 st Semester, 2012 Sanghyun Park.
8.1 8 Algorithms Foundations of Computer Science  Cengage Learning.
Examples of Flow Charts Pseudocodes
Flowchart. a diagram of the sequence of movements or actions of people or things involved in a complex system or activity. a graphical representation.
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.
Flowcharts C++ Lab. Algorithm An informal definition of an algorithm is: a step-by-step method for solving a problem or doing a task. Input data A step-by-step.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
Program Program is a collection of instructions that will perform some task.
INTRO2CS Tirgul 8 1. Searching and Sorting  Tips for debugging  Binary search  Sorting algorithms:  Bogo sort  Bubble sort  Quick sort and maybe.
CS 116 Object Oriented Programming II Lecture 13 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Flow Charts And Pseudo Codes Grade 12. An algorithm is a complete step-by- step procedure for solving a problem or accomplishing a task.
Chapter 9: Sorting and Searching Arrays
Program design Program Design Process has 2 phases:
INTRODUCTION TO PROBLEM SOLVING
while Repetition Structure
ALGORITHMS AND FLOWCHARTS
Sorting Algorithms.
Sorting Data are arranged according to their values.
Algorithm design and Analysis
Recursion (part 2) October 26, 2007 ComS 207: Programming I (in Java)
Chapter 5: Algorithms Computer Science: An Overview Tenth Edition
Sorting Data are arranged according to their values.
Algorithm Discovery and Design
Standard Version of Starting Out with C++, 4th Edition
Introduction to Algorithms and Programming
Algorithm Analysis and Design
PROBLEM ANALYSIS.
Teori Bahasa dan Automata Lecture 13: Algorithm
Recursion (part 2) March 22, 2006 ComS 207: Programming I (in Java)
Presentation transcript:

©Brooks/Cole, 2003 Chapter 8 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 Example: Finding the largest integer We want to find the largest integer among a list positive integers. This task cannot be done in one step. This algorithm is called FindLargest

©Brooks/Cole, 2003 Figure 8-3 Defining actions in FindLargest algorithm 1. The action is not the same as the other steps 2. The wording is not the same The Algorithm needs to be refined

©Brooks/Cole, 2003 Figure 8-4 FindLargest Refinement Step 0 initializes the largest to zero The wording is the same

©Brooks/Cole, 2003 Figure 8-5 Generalization of FindLargest This is a generalized form of the FindLargest algorithm. N is the number of integers.

©Brooks/Cole, 2003 THREE CONSTRUCTS 8.2

©Brooks/Cole, 2003 Figure 8-6 Three constructs to a structured program It has been proven there is no need for any other construct. Those three constructs makes a program or an algorithm easy to understand, debug and change. If the result of the test is true follow a sequence of instructions, if false, follow different instructions The same sequence of instructions are repeated sequence of instructions

©Brooks/Cole, 2003 ALGORITHMREPRESENTATIONALGORITHMREPRESENTATION 8.3

Figure 8-7 Flowcharts for three constructs (Pictorial Representation)

©Brooks/Cole, 2003 Figure 8-8 Pseudocode for three constructs (Englishlike Representation)

©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 Algorithm Algorithm is an ordered set of unambiguous steps that produces a result and terminate in a finite time. Ordered set; must be ordered set of instructions. Unambiguous; each step must be clearly defined. Produces result; if not the algorithm is useless. Terminate in finite time; if it doesn’t, then it is not an algorithm.

©Brooks/Cole, 2003 SUBALGORITHMSSUBALGORITHMS 8.5

Figure 8-9 Concept of a subalgorithm The principle of structured programming requires breaking the algorithm into smaller units called subalgorithms. Each subalgorithm is then divided into smaller units until the subalgorithm become intrinsic (understood immediately)

©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 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

©Brooks/Cole, 2003 Structure Chart Is a tool that shows the relationship between different modules in an algorithm. It is mostly used at design level. See Appendix E

©Brooks/Cole, 2003 BASICALGORITHMSBASICALGORITHMS 8.6

Figure 8-10 Summation

©Brooks/Cole, 2003 Figure 8-11 Product Exercise; X N ?

©Brooks/Cole, 2003 Finding the Smallest Finding the smallest is similar to finding the largest with two differences. First, the decision is to find the smallest. Second, initialized with a very large number.

©Brooks/Cole, 2003 Sorting Is the process by which data are arranged according to their value. There are three sorting algorithms; 1.Selection sort 2.Bubble sort 3.Insertion sort

©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-14 Selection sort algorithm

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

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

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

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

©Brooks/Cole, 2003 Search Algorithm Searching is the process of finding a location of a target in a list of objects. There are two basic searches for lists: 1.Sequential Search. 2.Binary Search.

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

©Brooks/Cole, 2003 Figure 8-20: Part I 1. Sequential Search - Example

©Brooks/Cole, 2003 Figure 8-20: Part II Example of a sequential sort Used for small and unsorted lists Sequential sort is very slow

©Brooks/Cole, 2003 Figure Binary sort - Example List is sorted

©Brooks/Cole, 2003 RECURSIONRECURSION 8.1

Figure Iteration- example (factorial) 2. Recursion – example (factorial)

©Brooks/Cole, 2003 Figure 8-23 Iterative and Recursive Algorithms An Algorithm is iterative whenever the definition doesn’t involve the algorithm itself. An Algorithm is recursive whenever it appears in the definition itself.

©Brooks/Cole, 2003 Figure 8-24 Tracing recursive solution to factorial problem (Two way journey)

©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 Much easier, more elegant and simple for creator and reader.