Algorithm
Algorithm
Algorithm
OBJECTIVES
After reading this chapter, the reader should
be able to:
Understand the concept of an algorithm.
CONCEPT
Figure 8-1
Informal definition of an algorithm
used in a computer
Figure 8-2
Finding the largest integer
among five integers
Figure 8-3
Defining actions in FindLargest algorithm
Figure 8-4
FindLargest refined
Figure 8-5
Generalization of FindLargest
8.2
THREE CONSTRUCTS
Figure 8-6
Three constructs
8.3
ALGORITHM
REPRESENTATION
Figure 8-7
Flowcharts for three constructs
Figure 8-8
Pseudocode for three constructs
Example 1
Solution
See Algorithm 8.1 on the next slide.
Algorithm 8.1: Average of two
AverageOfTwo
Input: Two numbers
1. Add the two numbers
2. Divide the result by 2
3. Return the result by step 2
End
Example 2
Solution
See Algorithm 8.2 on the next slide.
Algorithm 8.2: Pass/no pass Grade
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
Example 3
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”
End if
Continues on the next slide
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”
End if
Solution
See Algorithm 8.5 on the next slide.
Algorithm 8.5: Find largest of 1000 numbers
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
8.4
MORE FORMA DEFINITION
•Ordered set
•Unambiguous steps
•Effectiveness
•Termination
8.5
SUBALGORITHMS
Figure 8-9
Concept of a subalgorithm
Algorithm 8.6: Find largest
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
Subalgorithm: Find larger
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
8.6
BASIC
ALGORITHMS
Figure 8-10
Summation
Figure 8-11
Product
Figure 8-12
Selection sort
Figure 8-13: part I
Example of selection sort
Figure 8-13: part II
Example of selection sort
Figure 8-14
Selection sort
algorithm
Figure 8-15
Bubble sort
Figure 8-16: part I
Example of bubble sort
Figure 8-16: part II
Example of bubble sort
Figure 8-17
Insertion sort
Figure 8-18: part I
Example of insertion sort
Figure 8-18: part II
Example of insertion sort
Figure 8-19
Search concept
Figure 8-20: Part I
Example of a sequential sort
Figure 8-20: Part II
Example of a sequential sort
Figure 8-21
Example of a binary sort
8.1
RECURSION
Figure 8-22
Iterative definition of factorial
Figure 8-23