Chapter 4 - Algorithms. This chapter’s objectives are to: Understand general properties of algorithms, get familiar with pseudocode and flowcharts, learn about iterations and recursion, learn about working with lists, learn basic facts about OOP.
Trang 1and Data Structures
Maria Litvin ● Gary Litvin
2nd AP edition with GridWorld
Trang 2Objectives:
Trang 3Define Algorithm
abstract step-by-step recipe that describes
how to perform a certain task or solve a
Trang 5Repeat the following
three steps while k n:
Trang 7pos = pos0 and dir = dir0?
Yes No
Yes
No
pos pos0 dir dir0
Step forward
Input:
pos0, dir0
Stop Wall in front?
Trang 8Variables
be written and later erased and replaced with another value
sum sum + sq
sum
Trang 9Properties of Algorithms
or recursion to repeat the same steps multiple times
“size” of task or any input values
on a particular computer language or platform (although it may depend on the general
computing model)
Trang 10Repeat the following
three steps while k n:
the algorithm repeats the same instructions many times, but with different values of the variables
(The “running time” depends on n,
of course)
General: works for
any n
Trang 11Python C/C++
Javapublic class MyMath{
public static int addSquares(int n) {
int sum = 0;
for (int k = 1; k <= n; k++) sum += k * k;
return sum;
}}
int addSquares(int n){
int k, sum = 0;
for (k = 1; k <= n; k++) sum += k * k;
return sum;
}
Trang 12Iterations
instructions multiple times
Trang 13while (k <= n) {
sum += k * k; // add k * k to sum k++; // increment k by 1 }
Trang 14for (<initial setup>; <as long as this condition holds>;
<adjust variable(s) at the end of each iteration>) {
Trang 15Recursion
for a particular task in terms of applying the same procedure to a similar but smaller task.
simple that no recursion is needed.
a base case.
Trang 16Recursion: an Example
Procedure: Climb steps
Base case: if no steps
to climb stop Recursive case: more steps to climb
1 Step up one step
2 Climb steps
Trang 17Recursive Methods
Trang 18Recursion: How Does it Work
iterations, but hidden from the programmer
addSquares (4)addSquares (3)
addSquares (2)
addSquares (1) addSquares (0)
3
2
0 1
0 1
5
14
Base case
Trang 19Recursion (cont’d)
Recursion is especially useful for dealing with nested structures or branching processes
Trang 20count count + the number of bytes in X
else (if X is a folder)
count count + totalBytes(X)
}
return count
}
Base case(This is pseudocode, not Java!)
Trang 21Euclid’s Algorithm
two positive integers
Trang 22No
Trang 23Euclid’s Algorithm (cont’d)
• With iterations • With recursion
public static int gcf (int a, int b)
Trang 24Working with Lists
numbered
quickly
Amy 5
Ben 3
Cal 2
Dan
0
Eve 6
In Java, the elements
are counted from 0
Fay 1
Guy 4
Trang 25List Traversal
Start at the first element
While more elements remain
process the next element
for (int i = 0; i < list.length; i++)
Java’s “for each” loop
(a.k.a enhanced for
loop)
Trang 26Sequential Search
n, where n is the length of the list
Amy 5
Ben 3
Cal 2
Dan
0
Eve 6
Fay 1
Guy 4
Trang 27Binary Search
in ascending (or descending) order
the middle element of the remaining
search range
Trang 28Binary Search (cont’d)
Fay 5
Dan 3
Cal 2
Amy
0
Guy 6
Ben 1
Eve 4
Eve?
Fay 5
Dan 3
Cal 2
Amy
0
Guy 6
Ben 1
Eve 4
Fay 5
Dan 3
Cal 2
Amy
0
Guy 6
Ben 1
Eve 4 Eve?
Eve!
Trang 29• For a list of 1,000,000 elements takes, on average, only 20 comparisons
Trang 30Review:
• Why algorithms often use iterations?
• How is pseudocode different from Java code?
• Name three basic shapes used in flowcharts.
• Explain how variables are used in iterations.
• Which Java statements can be used to express iterations?
Trang 31Review (cont’d):
• What is called a base case in recursion?
• Suppose we define “word” as a sequence of letters Turn this into a recursive definition.
• When does Binary Search apply?
• How many comparisons, on average, are needed to find one of the values in a list of 1000 elements using Sequential Search? Binary Search?