Chapter 8 Algorithms.

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.
©Brooks/Cole, 2003 Chapter 8 Algorithms. ©Brooks/Cole, 2003 The Origins of “Algorithm”
Pseudocode and Algorithms
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (3) Recurrence Relation 11/11 ~ 11/14/2008 Yang Song.
Section Section Summary Recursive Algorithms Proving Recursive Algorithms Correct Recursion and Iteration (not yet included in overheads) Merge.
Searching and Sorting Arrays
Sorting Chapter 12 Objectives Upon completion you will be able to:
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the basic concepts and uses of arrays ❏ To be able to define C.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Algorithms Computer Science: An Overview Tenth Edition by J. Glenn.
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.
Chapter 3: The Fundamentals: Algorithms, the Integers, and Matrices
Department of Computer Science Data Structures Using C++ 2E Chapter 6: Recursion Learn about recursive Definitions Algorithms Functions Explore the base.
Computer Science: A Structured Programming Approach Using C1 8-5 Sorting One of the most common applications in computer science is sorting—the process.
Problem Solving and Algorithms
Reynolds 2006 Complexity1 Complexity Analysis Algorithm: –A sequence of computations that operates on some set of inputs and produces a result in a finite.
©Brooks/Cole, 2003 Chapter 8 Algorithms. ©Brooks/Cole, 2003 Understand the concept of an algorithm. Define and use the three constructs for developing.
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.
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.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Algorithms Computer Science: An Overview Tenth Edition by J. Glenn.
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
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 Algorithms CS 202 Epp section ??? Aaron Bloomfield.
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.
Searching & Sorting Programming 2. Searching Searching is the process of determining if a target item is present in a list of items, and locating it A.
Algorithms Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin,
1 Programming Tools Flowcharts Pseudocode Hierarchy Chart Direction of Numbered NYC Streets Algorithm Class Average Algorithm.
ALGORITHMS.
Chapter 5 Algorithms (1) Introduction to CS 1 st Semester, 2012 Sanghyun Park.
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
8.1 8 Algorithms Foundations of Computer Science  Cengage Learning.
ICS201 Lecture 21 : Sorting King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
©Brooks/Cole, 2003 Chapter 8 Algorithms. ©Brooks/Cole, 2003 CONCEPTCONCEPT 8.1.
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.
Section Recursion  Recursion – defining an object (or function, algorithm, etc.) in terms of itself.  Recursion can be used to define sequences.
Chapter 9: Sorting and Searching Arrays
Program design Program Design Process has 2 phases:
Growth of Functions & Algorithms
Chapter 2 Data Representation.
ALGORITHMS AND FLOWCHARTS
Chapter 8 Arrays Objectives
Topics discussed in this section:
Sorting Data are arranged according to their values.
Algorithms Chapter 3 With Question/Answer Animations
Algorithm design and Analysis
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.
PROBLEM ANALYSIS.
Chapter 8 Arrays Objectives
Teori Bahasa dan Automata Lecture 13: Algorithm
Algorithms.
Applications of Arrays
Computer Science: An Overview Tenth Edition
Presentation transcript:

Chapter 8 Algorithms

OBJECTIVES After reading this chapter, the reader should be able to: 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. Understand the concept of modularity and subalgorithms. List and comprehend common algorithms.

8.1 CONCEPT

Informal definition of an algorithm used in a computer (Input data) (Output data) An algorithm is independent of the computer system.

Finding the largest integer among five integers The algorithm should be general and not dependent on # of integers.

Refinement

Generalization

8.2 THREE CONSTRUCTS

Three constructs

8.3 ALGORITHM REPRESENTATION

Flowcharts for three constructs Flowchart – a pictorial representation of an algorithm.

Pseudocode for three constructs Pseudocode– a Englishlike representation of an algorithm.

Write an algorithm in pseudocode that finds the average of two numbers Example 1 Write an algorithm in pseudocode that finds the average of two numbers Solution See Algorithm 8.1 on the next slide.

Algorithm 8.1: Average of two AverageOfTwo Input: Two numbers Add the two numbers Divide the result by 2 Return the result by step 2 End

Write an algorithm to change a numeric grade to a pass/no pass grade. 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.

Algorithm 8.2: Pass/no pass Grade Pass/NoPassGrade Input: One number 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 Return the grade End

Write an algorithm to change a numeric grade to a letter grade. Example 3 Write an algorithm to change a numeric grade to a letter grade. Solution See Algorithm 8.3 on the next slide.

Algorithm 8.3: Letter grade 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” Continues on the next slide

Letter grade (continued) Algorithm 8.3: Letter grade (continued) 3. 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” Continues on the next slide

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

See Algorithm 8.4 on the next slide. 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.

Algorithm 8.4: Find largest FindLargest Input: A list of positive integers Set largest to 0 while (more integers) 2.1 if (the integer is greater than Largest) then 2.1.1 Set largest to the value of the integer End if End while Return largest End

Write an algorithm to find the largest of 1000 numbers. Example 5 Write an algorithm to find the largest of 1000 numbers. Solution See Algorithm 8.5 on the next slide.

Algorithm 8.5: Find largest of 1000 numbers FindLargest Input: 1000 positive integers Set Largest to 0 Set Counter to 0 while (Counter less than 1000) 3.1 if (the integer is greater than Largest) then 3.1.1 Set Largest to the value of the integer End if 3.2 Increment Counter End while Return Largest End

8.4 MORE FORMAL DEFINITION

Algorithm An ordered set of unambiguous steps that produces a result and terminates in a finite time. A well-defined, ordered set of instructions Each step – clear and unambiguous Result – returned to the calling algorithm or some other effect Must terminate

8.5 SUBALGORITHMS

Subalgorithm The principles of structured programming, require that an algorithm be broken into small units – subalgorithms. (subprograms, subroutines, procedures, functions, methods, and modules) Each subalgorithm is in turn divided into smaller subalgorithms. The process continues until the subalgorithms become understood immediately.

Concept of a subalgorithm

Algorithm 8.6: Find largest FindLargest Input: A list of positive integers Set Largest to 0 while (more integers) 2.1 FindLarger End while Return Largest End

Subalgorithm: Find larger FindLarger Input: Largest and current integer if (the integer is greater than Largest) then 1.1 Set Largest to the value of the integer End if End

8.6 BASIC ALGORITHMS

Figure 8-10 Summation

Figure 8-11 Product

Sorting Data are arranged according to their values. One of the most common applications in computer science. Selection sort Bubble Sort Insertion Sort

Selection sort A list of n elements requires n-1 sort passes. Figure 8-12 Selection sort A list of n elements requires n-1 sort passes. In pass k :

Example of selection sort Figure 8-13: part I Example of selection sort

Example of selection sort Figure 8-13: part II Example of selection sort

Figure 8-14 Selection sort algorithm

Bubble sort A list of n elements requires up to n-1 passes. Figure 8-15 Bubble sort A list of n elements requires up to n-1 passes. The smallest element is bubbled from the unsorted sublist and moved to the sorted sublist.

Figure 8-16: part I Example of bubble sort

Figure 8-16: part II Example of bubble sort

Figure 8-17 Insertion sort A list of n elements requires n-1 passes.

Example of insertion sort Figure 8-18: part I Example of insertion sort

Example of insertion sort Figure 8-18: part II Example of insertion sort

Searching Find the location of a target among a list of objects. One of the most common applications in computer science. Sequential search used if the list being searched is not ordered. Binary search

Figure 8-19 Search concept

Example of a sequential sort Figure 8-20: Part I Example of a sequential sort

Example of a sequential sort Figure 8-20: Part II Example of a sequential sort

Example of a binary search Figure 8-21

8.7 RECURSION

Recursion Iteration Recursion – a process in which an algorithm calls itself.

Iterative definition of factorial Figure 8-22 Iterative definition of factorial

Recursive definition of factorial Figure 8-23 Recursive definition of factorial

Tracing recursive solution to factorial problem Figure 8-24 Tracing recursive solution to factorial problem

Algorithm 8.7: Iterative factorial Factorial Input: A positive integer num Set FactN to 1 (FactN = 1) Set i to 1 (i = 1) while (i is less than or equal to num) (i <= num) 3.1 Set FactN to FactN x i (FactN = FactN * i) 3.2 Increment i (i = i + 1) End while Return FactN End

Algorithm 8.8: Recursive factorial Factorial Input: A positive integer num if (num is equal to 0) (num == 0) then 1.1 return 1 else 1.2 return num x Factorial (num – 1) End if End