• Tidak ada hasil yang ditemukan

PPT PowerPoint Presentation

N/A
N/A
Protected

Academic year: 2025

Membagikan "PPT PowerPoint Presentation"

Copied!
53
0
0

Teks penuh

(1)

Chapter 8

Algorithms

(2)

©Brooks/Cole, 2003

Understand the concept of an algorithm.

Understand the concept of an algorithm.

Define and use the three constructs for developing Define and use the three constructs for developing

algorithms: sequence, decision, and repetition.

algorithms: sequence, decision, and repetition.

Understand and use three tools to represent algorithms:

Understand and use three tools to represent algorithms:

flowchart, pseudocode and structure chart.

flowchart, pseudocode and structure chart.

After reading this chapter, the reader should After reading this chapter, the reader should

be able to:

be able to:

O O BJECTIVES BJECTIVES

Understand the concept of modularity and subalgorithms.

Understand the concept of modularity and subalgorithms.

List and comprehend common algorithms.

List and comprehend common algorithms.

(3)

CONCEPT

CONCEPT CONCEPT

CONCEPT

8.1 8.1

(4)

©Brooks/Cole, 2003 Figure 8-1

Informal definition of an algorithm

used in a computer

(5)

Figure 8-2

Finding the largest integer

among five integers

(6)

©Brooks/Cole, 2003 Figure 8-3

Defining actions in FindLargest algorithm

(7)

Figure 8-4

FindLargest refined

(8)

©Brooks/Cole, 2003 Figure 8-5

Generalization of FindLargest

(9)

THREE CONSTRUCTS

THREE CONSTRUCTS THREE CONSTRUCTS

THREE CONSTRUCTS

8.2 8.2

(10)

©Brooks/Cole, 2003 Figure 8-6

Three constructs of Algorithm

(11)

ALGORITHM ALGORITHM

REPRESENTATION REPRESENTATION

ALGORITHM ALGORITHM

REPRESENTATION

REPRESENTATION

8.3 8.3

(12)

©Brooks/Cole, 2003 Figure 8-7

Flowcharts for three constructs

(13)

Figure 8-8

Pseudocode for three constructs

(14)

©Brooks/Cole, 2003

Example 1 Example 1

Write an algorithm in pseudocode that finds the average of two numbers

Solution Solution

See Algorithm 8.1 on the next slide.

(15)

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:

Algorithm 8.1: Average of two Average of two

(16)

©Brooks/Cole, 2003

Example 2 Example 2

Write an algorithm to change a numeric grade to a pass/no pass grade.

Solution Solution

See Algorithm 8.2 on the next slide.

(17)

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:

Algorithm 8.2: Pass/no pass Grade Pass/no pass Grade

(18)

©Brooks/Cole, 2003

Example 3 Example 3

Write an algorithm to change a numeric grade to a letter grade.

Solution Solution

See Algorithm 8.3 on the next slide.

(19)

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:

Algorithm 8.3: Letter grade Letter grade

(20)

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

Algorithm 8.3: Letter grade (continued) Letter grade (continued)

Continues on the next slide

(21)

5. 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:

Algorithm 8.3: Letter grade (continued) Letter grade (continued)

(22)

©Brooks/Cole, 2003

Example 4 Example 4

Write an algorithm to find the largest of a set of numbers. You do not know the number of numbers.

Solution Solution

See Algorithm 8.4 on the next slide.

(23)

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:

Algorithm 8.4: Find largest Find largest

(24)

©Brooks/Cole, 2003

Example 5 Example 5

Write an algorithm to find the largest of 1000 numbers.

Solution Solution

See Algorithm 8.5 on the next slide.

(25)

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

Algorithm 8.5:

Algorithm 8.5:

Find largest of 1000 numbersFind largest of 1000 numbers
(26)

©Brooks/Cole, 2003

MORE FORMAL MORE FORMAL

DEFINITION DEFINITION MORE FORMAL MORE FORMAL

DEFINITION

DEFINITION

8.4 8.4

(27)

SUBALGORITHMS

SUBALGORITHMS SUBALGORITHMS

SUBALGORITHMS

8.5 8.5

(28)

©Brooks/Cole, 2003 Figure 8-9

Concept of a subalgorithm

(29)

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:

Algorithm 8.6: Find largest Find largest

(30)

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

Subalgorithm: Find larger Find larger

(31)

BASIC BASIC

ALGORITHMS ALGORITHMS

BASIC BASIC

ALGORITHMS

ALGORITHMS

8.6 8.6

(32)

©Brooks/Cole, 2003 Figure 8-10

Summation

(33)

Figure 8-11

Product

(34)

©Brooks/Cole, 2003 Figure 8-12

Selection sort

(35)

Figure 8-13: part I

Example of selection sort

(36)

©Brooks/Cole, 2003 Figure 8-13: part II

Example of selection sort

(37)

Figure 8-14

Selection sort

algorithm

(38)

©Brooks/Cole, 2003 Figure 8-15

Bubble sort

(39)

Figure 8-16: part I

Example of bubble sort

(40)

©Brooks/Cole, 2003 Figure 8-16: part II

Example of bubble sort

(41)

Figure 8-17

Insertion sort

(42)

©Brooks/Cole, 2003 Figure 8-18: part I

Example of insertion sort

(43)

Figure 8-18: part II

Example of insertion sort

(44)

©Brooks/Cole, 2003 Figure 8-19

Search concept

(45)

Figure 8-20: Part I

Example of a sequential Search

(46)

©Brooks/Cole, 2003 Figure 8-20: Part II

Example of a sequential Search

(47)

Figure 8-21

Example of a binary Search

(48)

©Brooks/Cole, 2003

RECURSION

RECURSION RECURSION

RECURSION

8.1 8.1

(49)

Figure 8-22

Iterative definition of factorial

It is the process in which an algorithm calls itself

(50)

©Brooks/Cole, 2003 Figure 8-23

Recursive definition of factorial

It is when the process dose not involve calling it self

(51)

Figure 8-24

Tracing recursive solution to factorial problem

(52)

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

Algorithm 8.7:

Iterative factorialIterative factorial
(53)

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:

Algorithm 8.8:

Recursive factorialRecursive factorial

Referensi

Dokumen terkait

Figure 1 The Technical Research of Sunspot Numbers Prediction Using GRU Algorithm The first step of predicting sunspot numbers is collecting sunspot number data, next the data

Part B : READING Question 1-3: Look, read and write the correct words next to their descriptions.. There is one

Gambar : Pendekatan Filsafat ADA NILAI Ontologi: Crististian Wolff 1.Ada Garis Demarkasi 2.Ontologi berurusan dengan semesta empiris dan metafisika berurusan dengan semesta di

109 年度學生撰寫申請書輔導重點、輔導綜合考評 及學校初審作業實務 •行業類別填寫參考資料 1/2 二、申請書內容 2/5 代號 行業類別 說明 A 農、林、漁、牧業 從事農作物栽培、畜牧、農事及畜牧服務、造林、伐木及採集、漁撈及水產養殖等 之行業。 B 礦業及土石採取業 從事石油、天然氣、砂、石及黏土等礦物及土石之探勘、採取、初步處理(如碎解

方法 : 通过恒等变形或遂项求导或遂项求积把原级 数化为可求和的级数 等比级数... 内容小结 幂级数的性质 1两个幂级数在公共收敛区间内可进行加、减与

Suggested Activity Example no model numbers are given, as emphasis is on the hierarchical functions of the network devices shown: Slide 1: Graphic 1 Student or Group Notes as to

• Rule 8: Since 1 • Since এর আতগর clause টট simple present বয present perfect হতল পতরর main clause টটতত verb এর simple past tense হয়। • Example: • Incomplete: It is ten years

Quantum Numbers Describe the properties of the atomic orbitals and the electrons 1-principle quantum number 2-Secondary quantum number 3-Magnetic quantum number 4-Spin quantum number