Invitation To Computer Science, Java Version, Third Edition
Invitation To Computer Science, Java Version, Third Edition
Objectives
In this chapter, you will learn about
Representing algorithms
Introduction
This chapter uses four problems to discuss algorithms and algorithmic problem solving
Multiplying two numbers Searching lists Finding maxima and minima Matching patterns
Representing Algorithms
Natural language
Language spoken and written in everyday life Examples: English, Spanish, Arabic, and so on Problems with using natural language for algorithms
Verbose Imprecise Relies on context and experiences to give precise meaning to a word or phrase
4
Step 1 set the value of carry to 0 Step 2 set the value of i to 0 Step 3 while the value of i is less than or equal to m 1, repeat the instructions in steps 4 6 Step 4 add the two digits a(i) and b(i) to the current value of carry to get c(i) Step 5 if c(i) is greater than 10 or equal to 10 then reset c(i) to c(i) 10 and reset the value of carry to 1; otherwise set the new value of carry to 0 Step 6 Add 1 to i Step 7 set c(m) to the value of carry Step 8 print out the final answer c(m) c(m-1)c(0) Step 9 stop
Invitation to Computer Science, Java Version, Third Edition 6
During the initial phases of design, we are forced to deal with detailed language issues
The Beginning of the Addition Algorithm of Figure 1.2 Expressed in a High-Level Programming Language
Invitation to Computer Science, Java Version, Third Edition 8
Pseudocode
English language constructs modeled to look like statements available in most programming languages Steps presented in a structured manner (numbered, indented, and so on) No fixed syntax for most operations is required
Pseudocode (continued)
Less ambiguous and more readable than natural language Emphasis is on process, not notation Well-understood forms allow logical reasoning about algorithm behavior Can be easily translated into a programming language
10
Flowchart
A graphical representation of an algorithm
flowchart examples
Invitation to Computer Science, Java Version, Third Edition 11
Sequential
Conditional
Iterative
12
Sequential Operations
Computation operations
Example:
Set the value of variable to arithmetic expression
Variable
Input operations
To receive data values from the outside world Example
Output operations
To send results to the outside world for display Example
Sequential algorithm
Also called straight-line algorithm Executes its instructions in a straight line from top to bottom and then stops
Control operations
Conditional operations Iterative operations
16
Conditional operations
Ask questions and choose alternative actions based on the answers Example
17
Iterative operations
Perform looping behavior, repeating actions until a continuation condition becomes false Loop
18
19
Components of a loop
Continuation condition Loop body
Infinite loop
The continuation condition never becomes false An error
21
Pretest loop
Continuation condition tested at the beginning of each pass through the loop It is possible for the loop body to never be executed While loop
23
Posttest loop
Continuation condition tested at the end of loop body Loop body must be executed at least once Do/While loop (Repeat-Until loop)
24
2 Additional Constructs
CASE
CASE expression OF condition 1: sequence 1 condition 2: sequence 2 condition n: sequence n OTHERS: default sequence ENDCASE
FOR
FOR iteration bounds sequence ENDFOR
25
CASE
CASE shape OF SQUARE : area = length * length RECTANGLE : area = length * breadth TRIANGLE : area = length * breadth / 2 ENDCASE
26
Task
Implement an algorithm to multiply two numbers, a and b, using repeated addition
Algorithm outline
Create a loop that executes exactly b times, with each execution of the loop adding the value of a to a running total
29
Task
Find a particular persons name from an unordered list of telephone subscribers
Algorithm outline
Start with the first entry and check its name, then repeat the process for all entries
31
Algorithm discovery
Finding a solution to a given problem
Only works for collections of exactly one size Duplicates the same operations over and over
32
Handles special cases (such as a name not found in the collection) Uses the variable Found to exit the iteration as soon as a match is found
Invitation to Computer Science, Java Version, Third Edition 33
The selection of an algorithm to solve a problem is greatly influenced by the way the data for that problem is organized
35
Task
Find the largest value from a list of values
Algorithm outline
Keep track of the largest value seen so far (initialized to be the first in the list) Compare each value to the largest seen so far, and keep the larger as the new largest
36
Once an algorithm has been developed, it may itself be used in the construction of other, more complex algorithms Library
A collection of useful algorithms An important tool in algorithm design and development
37
38
Task
Find if and where a pattern string occurs within a longer piece of text
Algorithm outline
Try each possible location of pattern string in turn At each location, compare pattern characters against string characters
40
Abstraction
Separating high-level view from low-level details Key concept in computer science Makes difficult problems intellectually manageable Allows piece-by-piece development of algorithms
41
Top-down design
When solving a complex problem
Create high-level operations in the first draft of an algorithm After drafting the outline of the algorithm, return to the high-level operations and elaborate each one Repeat until all operations are primitives
42
Pattern-matching algorithm
Contains a loop within a loop
External loop iterates through possible locations of matches to pattern Internal loop iterates through corresponding characters of pattern and string to evaluate match
43
Summary
Summary
Pseudocode is readable, unambiguous, and able to be analyzed Algorithm design is a creative process; uses multiple drafts and top-down design to develop the best solution Abstraction is a key tool for good design
46