logic & algorithms · •algorithm: an ordered set of unambiguous steps that produces a result...

62
©Brooks/Cole, 2003 OVERVIEW Logic & Algorithms Foundations of Computer Science Behrouz A. Forouzan, Brooks/ColeThomson Learning, Pacific Grove, USA, 2003.

Upload: others

Post on 24-Mar-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Logic & Algorithms · •Algorithm: an ordered set of unambiguous steps that produces a result and terminates in a finite time. ©Brooks/Cole, 2003 SUBALGORITHMS 8.5 ©Brooks/Cole,

©Brooks/Cole, 2003

OVERVIEW

Logic & Algorithms

Foundations of

Computer Science

Behrouz A. Forouzan,

Brooks/Cole—Thomson Learning,

Pacific Grove, USA, 2003.

Page 2: Logic & Algorithms · •Algorithm: an ordered set of unambiguous steps that produces a result and terminates in a finite time. ©Brooks/Cole, 2003 SUBALGORITHMS 8.5 ©Brooks/Cole,

©Brooks/Cole, 2003

Understand the concept of Logic and 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:

OBJECTIVES

Understand the concept of modularity and

subalgorithms.

List and comprehend common algorithms.

Page 3: Logic & Algorithms · •Algorithm: an ordered set of unambiguous steps that produces a result and terminates in a finite time. ©Brooks/Cole, 2003 SUBALGORITHMS 8.5 ©Brooks/Cole,

©Brooks/Cole, 2003

CONCEPT8.1

Page 4: Logic & Algorithms · •Algorithm: an ordered set of unambiguous steps that produces a result and terminates in a finite time. ©Brooks/Cole, 2003 SUBALGORITHMS 8.5 ©Brooks/Cole,

©Brooks/Cole, 2003

Figure 8-1

Informal definition of an algorithm

used in a computer

Informal definition

Page 5: Logic & Algorithms · •Algorithm: an ordered set of unambiguous steps that produces a result and terminates in a finite time. ©Brooks/Cole, 2003 SUBALGORITHMS 8.5 ©Brooks/Cole,

©Brooks/Cole, 2003

Figure 8-2

Finding the largest integer

among five integers

Page 6: Logic & Algorithms · •Algorithm: an ordered set of unambiguous steps that produces a result and terminates in a finite time. ©Brooks/Cole, 2003 SUBALGORITHMS 8.5 ©Brooks/Cole,

©Brooks/Cole, 2003

Figure 8-3

Defining actions in FindLargest algorithm

Page 7: Logic & Algorithms · •Algorithm: an ordered set of unambiguous steps that produces a result and terminates in a finite time. ©Brooks/Cole, 2003 SUBALGORITHMS 8.5 ©Brooks/Cole,

©Brooks/Cole, 2003

Figure 8-4

FindLargest refined

Page 8: Logic & Algorithms · •Algorithm: an ordered set of unambiguous steps that produces a result and terminates in a finite time. ©Brooks/Cole, 2003 SUBALGORITHMS 8.5 ©Brooks/Cole,

©Brooks/Cole, 2003

Figure 8-5

Generalization of FindLargest

Page 9: Logic & Algorithms · •Algorithm: an ordered set of unambiguous steps that produces a result and terminates in a finite time. ©Brooks/Cole, 2003 SUBALGORITHMS 8.5 ©Brooks/Cole,

©Brooks/Cole, 2003

THREE CONSTRUCTS

8.2

Page 10: Logic & Algorithms · •Algorithm: an ordered set of unambiguous steps that produces a result and terminates in a finite time. ©Brooks/Cole, 2003 SUBALGORITHMS 8.5 ©Brooks/Cole,

©Brooks/Cole, 2003

Figure 8-6

Three constructs

Page 11: Logic & Algorithms · •Algorithm: an ordered set of unambiguous steps that produces a result and terminates in a finite time. ©Brooks/Cole, 2003 SUBALGORITHMS 8.5 ©Brooks/Cole,

©Brooks/Cole, 2003

ALGORITHM

REPRESENTATION

8.3

Page 12: Logic & Algorithms · •Algorithm: an ordered set of unambiguous steps that produces a result and terminates in a finite time. ©Brooks/Cole, 2003 SUBALGORITHMS 8.5 ©Brooks/Cole,

©Brooks/Cole, 2003

Algorithm representation• Flowchart

• A flowchart is a pictorial representation of an algorithm.

• Figure 8.7, Appendix C.

• Pseudocode • Pseudocode is an Englishlike representation of an algorithm.

• Figure 8.8, Appendix D.

Page 13: Logic & Algorithms · •Algorithm: an ordered set of unambiguous steps that produces a result and terminates in a finite time. ©Brooks/Cole, 2003 SUBALGORITHMS 8.5 ©Brooks/Cole,

©Brooks/Cole, 2003

Figure 8-7

Flowcharts for three constructs

WhileCondition

Page 14: Logic & Algorithms · •Algorithm: an ordered set of unambiguous steps that produces a result and terminates in a finite time. ©Brooks/Cole, 2003 SUBALGORITHMS 8.5 ©Brooks/Cole,

©Brooks/Cole, 2003

Figure 8-8

Pseudocode for three constructs

Page 15: Logic & Algorithms · •Algorithm: an ordered set of unambiguous steps that produces a result and terminates in a finite time. ©Brooks/Cole, 2003 SUBALGORITHMS 8.5 ©Brooks/Cole,

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

Page 16: Logic & Algorithms · •Algorithm: an ordered set of unambiguous steps that produces a result and terminates in a finite time. ©Brooks/Cole, 2003 SUBALGORITHMS 8.5 ©Brooks/Cole,

©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

Page 17: Logic & Algorithms · •Algorithm: an ordered set of unambiguous steps that produces a result and terminates in a finite time. ©Brooks/Cole, 2003 SUBALGORITHMS 8.5 ©Brooks/Cole,

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

Page 18: Logic & Algorithms · •Algorithm: an ordered set of unambiguous steps that produces a result and terminates in a finite time. ©Brooks/Cole, 2003 SUBALGORITHMS 8.5 ©Brooks/Cole,

©Brooks/Cole, 2003

Pass/NoPassGrade

Input: One number

1. if (the number is greater than or equal to 60)

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

Page 19: Logic & Algorithms · •Algorithm: an ordered set of unambiguous steps that produces a result and terminates in a finite time. ©Brooks/Cole, 2003 SUBALGORITHMS 8.5 ©Brooks/Cole,

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

Page 20: Logic & Algorithms · •Algorithm: an ordered set of unambiguous steps that produces a result and terminates in a finite time. ©Brooks/Cole, 2003 SUBALGORITHMS 8.5 ©Brooks/Cole,

©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

Page 21: Logic & Algorithms · •Algorithm: an ordered set of unambiguous steps that produces a result and terminates in a finite time. ©Brooks/Cole, 2003 SUBALGORITHMS 8.5 ©Brooks/Cole,

©Brooks/Cole, 2003

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”

End if

Algorithm 8.3: Letter grade (continued)

Continues on the next slide

Page 22: Logic & Algorithms · •Algorithm: an ordered set of unambiguous steps that produces a result and terminates in a finite time. ©Brooks/Cole, 2003 SUBALGORITHMS 8.5 ©Brooks/Cole,

©Brooks/Cole, 2003

5. if (the number is less than 59, inclusive)

then

5.1 Set the grade to “F”

End if

6. Return the grade

End

Algorithm 8.3: Letter grade (continued)

Page 23: Logic & Algorithms · •Algorithm: an ordered set of unambiguous steps that produces a result and terminates in a finite time. ©Brooks/Cole, 2003 SUBALGORITHMS 8.5 ©Brooks/Cole,

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

Page 24: Logic & Algorithms · •Algorithm: an ordered set of unambiguous steps that produces a result and terminates in a finite time. ©Brooks/Cole, 2003 SUBALGORITHMS 8.5 ©Brooks/Cole,

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

2.1.1 Set largest to the value of the integer

End if

End while

3. Return Largest

End

Algorithm 8.4: Find largest

Page 25: Logic & Algorithms · •Algorithm: an ordered set of unambiguous steps that produces a result and terminates in a finite time. ©Brooks/Cole, 2003 SUBALGORITHMS 8.5 ©Brooks/Cole,

©Brooks/Cole, 2003

Example 5

Write an algorithm to find the largest of

1000 numbers.

Solution

See Algorithm 8.5 on the next slide.

Page 26: Logic & Algorithms · •Algorithm: an ordered set of unambiguous steps that produces a result and terminates in a finite time. ©Brooks/Cole, 2003 SUBALGORITHMS 8.5 ©Brooks/Cole,

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

3.1.1 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

Page 27: Logic & Algorithms · •Algorithm: an ordered set of unambiguous steps that produces a result and terminates in a finite time. ©Brooks/Cole, 2003 SUBALGORITHMS 8.5 ©Brooks/Cole,

©Brooks/Cole, 2003

MORE FORMAL

DEFINITION

8.4

Page 28: Logic & Algorithms · •Algorithm: an ordered set of unambiguous steps that produces a result and terminates in a finite time. ©Brooks/Cole, 2003 SUBALGORITHMS 8.5 ©Brooks/Cole,

©Brooks/Cole, 2003

Algorithm

• Algorithm: an ordered set of unambiguous steps that produces a result and terminates in a finite time.

Page 29: Logic & Algorithms · •Algorithm: an ordered set of unambiguous steps that produces a result and terminates in a finite time. ©Brooks/Cole, 2003 SUBALGORITHMS 8.5 ©Brooks/Cole,

©Brooks/Cole, 2003

SUBALGORITHMS

8.5

Page 30: Logic & Algorithms · •Algorithm: an ordered set of unambiguous steps that produces a result and terminates in a finite time. ©Brooks/Cole, 2003 SUBALGORITHMS 8.5 ©Brooks/Cole,

©Brooks/Cole, 2003

Figure 8-9

Concept of a subalgorithm

Page 31: Logic & Algorithms · •Algorithm: an ordered set of unambiguous steps that produces a result and terminates in a finite time. ©Brooks/Cole, 2003 SUBALGORITHMS 8.5 ©Brooks/Cole,

©Brooks/Cole, 2003

Subalgorithms

•The advantages of subalgorithms:• It is more undersatandable.• A subalgorithm can be called many times in different

parts of the main algorithm without rewritten.

Page 32: Logic & Algorithms · •Algorithm: an ordered set of unambiguous steps that produces a result and terminates in a finite time. ©Brooks/Cole, 2003 SUBALGORITHMS 8.5 ©Brooks/Cole,

©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

Page 33: Logic & Algorithms · •Algorithm: an ordered set of unambiguous steps that produces a result and terminates in a finite time. ©Brooks/Cole, 2003 SUBALGORITHMS 8.5 ©Brooks/Cole,

©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

Page 34: Logic & Algorithms · •Algorithm: an ordered set of unambiguous steps that produces a result and terminates in a finite time. ©Brooks/Cole, 2003 SUBALGORITHMS 8.5 ©Brooks/Cole,

©Brooks/Cole, 2003

Structure chart

• Structure chart:• A structure chart is a high-level design tool that shows the relationship

between different modules in an algorithm.

• Appendix E.

Page 35: Logic & Algorithms · •Algorithm: an ordered set of unambiguous steps that produces a result and terminates in a finite time. ©Brooks/Cole, 2003 SUBALGORITHMS 8.5 ©Brooks/Cole,

©Brooks/Cole, 2003

BASIC

ALGORITHMS

8.6

Page 36: Logic & Algorithms · •Algorithm: an ordered set of unambiguous steps that produces a result and terminates in a finite time. ©Brooks/Cole, 2003 SUBALGORITHMS 8.5 ©Brooks/Cole,

©Brooks/Cole, 2003

Basic algorithms

• Basic algorithms:• Summation

• Product

• Sorting

• Searching

Page 37: Logic & Algorithms · •Algorithm: an ordered set of unambiguous steps that produces a result and terminates in a finite time. ©Brooks/Cole, 2003 SUBALGORITHMS 8.5 ©Brooks/Cole,

©Brooks/Cole, 2003

Figure 8-10

Summation

More Numbers

Page 38: Logic & Algorithms · •Algorithm: an ordered set of unambiguous steps that produces a result and terminates in a finite time. ©Brooks/Cole, 2003 SUBALGORITHMS 8.5 ©Brooks/Cole,

©Brooks/Cole, 2003

Figure 8-11

Product

More Numbers

Page 39: Logic & Algorithms · •Algorithm: an ordered set of unambiguous steps that produces a result and terminates in a finite time. ©Brooks/Cole, 2003 SUBALGORITHMS 8.5 ©Brooks/Cole,

©Brooks/Cole, 2003

Smallest and largest

• Find the smallest number• Use a decision construct to find the smaller of two numbers.

• Put this construct in a loop.

• Initialize with a very large number instead of a very small one.

Page 40: Logic & Algorithms · •Algorithm: an ordered set of unambiguous steps that produces a result and terminates in a finite time. ©Brooks/Cole, 2003 SUBALGORITHMS 8.5 ©Brooks/Cole,

©Brooks/Cole, 2003

Sorting

• Sorting:• The process by which data are arranged according to their values.

• Selection sort

• Bubble sort

• Insertion sort

Page 41: Logic & Algorithms · •Algorithm: an ordered set of unambiguous steps that produces a result and terminates in a finite time. ©Brooks/Cole, 2003 SUBALGORITHMS 8.5 ©Brooks/Cole,

©Brooks/Cole, 2003

Figure 8-12

Selection sort

Page 42: Logic & Algorithms · •Algorithm: an ordered set of unambiguous steps that produces a result and terminates in a finite time. ©Brooks/Cole, 2003 SUBALGORITHMS 8.5 ©Brooks/Cole,

©Brooks/Cole, 2003

Figure 8-13: part I

Example of selection sort

Page 43: Logic & Algorithms · •Algorithm: an ordered set of unambiguous steps that produces a result and terminates in a finite time. ©Brooks/Cole, 2003 SUBALGORITHMS 8.5 ©Brooks/Cole,

©Brooks/Cole, 2003

Figure 8-13: part II

Example of selection sort

Page 44: Logic & Algorithms · •Algorithm: an ordered set of unambiguous steps that produces a result and terminates in a finite time. ©Brooks/Cole, 2003 SUBALGORITHMS 8.5 ©Brooks/Cole,

©Brooks/Cole, 2003

Figure 8-14

Selection sort

algorithmWhile there are more elements in the unsorted

list

Page 45: Logic & Algorithms · •Algorithm: an ordered set of unambiguous steps that produces a result and terminates in a finite time. ©Brooks/Cole, 2003 SUBALGORITHMS 8.5 ©Brooks/Cole,

©Brooks/Cole, 2003

Figure 8-15

Bubble sort

Page 46: Logic & Algorithms · •Algorithm: an ordered set of unambiguous steps that produces a result and terminates in a finite time. ©Brooks/Cole, 2003 SUBALGORITHMS 8.5 ©Brooks/Cole,

©Brooks/Cole, 2003

Figure 8-16: part I

Example of bubble sort

Page 47: Logic & Algorithms · •Algorithm: an ordered set of unambiguous steps that produces a result and terminates in a finite time. ©Brooks/Cole, 2003 SUBALGORITHMS 8.5 ©Brooks/Cole,

©Brooks/Cole, 2003

Figure 8-16: part II

Example of bubble sort

Page 48: Logic & Algorithms · •Algorithm: an ordered set of unambiguous steps that produces a result and terminates in a finite time. ©Brooks/Cole, 2003 SUBALGORITHMS 8.5 ©Brooks/Cole,

©Brooks/Cole, 2003

Figure 8-17

Insertion sort

Page 49: Logic & Algorithms · •Algorithm: an ordered set of unambiguous steps that produces a result and terminates in a finite time. ©Brooks/Cole, 2003 SUBALGORITHMS 8.5 ©Brooks/Cole,

©Brooks/Cole, 2003

Figure 8-18: part I

Example of insertion sort

Page 50: Logic & Algorithms · •Algorithm: an ordered set of unambiguous steps that produces a result and terminates in a finite time. ©Brooks/Cole, 2003 SUBALGORITHMS 8.5 ©Brooks/Cole,

©Brooks/Cole, 2003

Figure 8-18: part II

Example of insertion sort

Page 51: Logic & Algorithms · •Algorithm: an ordered set of unambiguous steps that produces a result and terminates in a finite time. ©Brooks/Cole, 2003 SUBALGORITHMS 8.5 ©Brooks/Cole,

©Brooks/Cole, 2003

Searching

• Searching• The process of finding the location of a target among a list of objects.

• Sequential search

• Binary search

Page 52: Logic & Algorithms · •Algorithm: an ordered set of unambiguous steps that produces a result and terminates in a finite time. ©Brooks/Cole, 2003 SUBALGORITHMS 8.5 ©Brooks/Cole,

©Brooks/Cole, 2003

Figure 8-19

Search concept

Page 53: Logic & Algorithms · •Algorithm: an ordered set of unambiguous steps that produces a result and terminates in a finite time. ©Brooks/Cole, 2003 SUBALGORITHMS 8.5 ©Brooks/Cole,

©Brooks/Cole, 2003

Figure 8-20: Part I

Example of a sequential search

Page 54: Logic & Algorithms · •Algorithm: an ordered set of unambiguous steps that produces a result and terminates in a finite time. ©Brooks/Cole, 2003 SUBALGORITHMS 8.5 ©Brooks/Cole,

©Brooks/Cole, 2003

Figure 8-20: Part II

Example of a sequential search

Page 55: Logic & Algorithms · •Algorithm: an ordered set of unambiguous steps that produces a result and terminates in a finite time. ©Brooks/Cole, 2003 SUBALGORITHMS 8.5 ©Brooks/Cole,

©Brooks/Cole, 2003

Figure 8-21

Example of a binary search

Page 56: Logic & Algorithms · •Algorithm: an ordered set of unambiguous steps that produces a result and terminates in a finite time. ©Brooks/Cole, 2003 SUBALGORITHMS 8.5 ©Brooks/Cole,

©Brooks/Cole, 2003

RECURSION

8.7

Page 57: Logic & Algorithms · •Algorithm: an ordered set of unambiguous steps that produces a result and terminates in a finite time. ©Brooks/Cole, 2003 SUBALGORITHMS 8.5 ©Brooks/Cole,

©Brooks/Cole, 2003

Figure 8-22

Recursion

• Recursion:• A process in which an algorithm calls itself.

Page 58: Logic & Algorithms · •Algorithm: an ordered set of unambiguous steps that produces a result and terminates in a finite time. ©Brooks/Cole, 2003 SUBALGORITHMS 8.5 ©Brooks/Cole,

Figure 8-23

Recursive definition of factorial

Iterative definition of factorial

Page 59: Logic & Algorithms · •Algorithm: an ordered set of unambiguous steps that produces a result and terminates in a finite time. ©Brooks/Cole, 2003 SUBALGORITHMS 8.5 ©Brooks/Cole,

©Brooks/Cole, 2003

Figure 8-24

Tracing recursive solution to factorial problem

Page 60: Logic & Algorithms · •Algorithm: an ordered set of unambiguous steps that produces a result and terminates in a finite time. ©Brooks/Cole, 2003 SUBALGORITHMS 8.5 ©Brooks/Cole,

©Brooks/Cole, 2003

Factorial

Input: A positive integer num

1. Set FactN to 1

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

Page 61: Logic & Algorithms · •Algorithm: an ordered set of unambiguous steps that produces a result and terminates in a finite time. ©Brooks/Cole, 2003 SUBALGORITHMS 8.5 ©Brooks/Cole,

©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

Page 62: Logic & Algorithms · •Algorithm: an ordered set of unambiguous steps that produces a result and terminates in a finite time. ©Brooks/Cole, 2003 SUBALGORITHMS 8.5 ©Brooks/Cole,

©Brooks/Cole, 2003

Key terms

• Algorithm

• Binary search

• Bubble sort

• Flowchart

• Function

• Input data

• Insertion sort

• Iteration

• Module

• Output data

• Procedure

• Pseudocode

• Recursion

• Searching

• Selection sort

• Sequential search

• Sort pass

• Sorting

• Structure chart

• Subalgorithm

• Subprogram

• Subroutine

• Summation