Академический Документы
Профессиональный Документы
Культура Документы
Introduction
Brooks/Cole,
2001
Pseudocode
Part English, part structured code
Used to describe an algorithm
In this text, algorithms consist of:
Data Structure
Combination of either a data type or another
data structure, into a set with defined
relationships
Examples:
Array
Homogeneous sequence of data elements
Position association among elements
Record
Heterogeneous combination of data
No association among elements
Brooks/Cole,
2001
Abstraction
We know what a data type can do
Details of how this is done, is hidden
Hiding details of how operations are
performed, allows a programmer to focus on
what needs to be done
Brooks/Cole,
2001
Figure 1-1
Brooks/Cole,
2001
Brooks/Cole,
2001
Figure 1-2
Brooks/Cole,
2001
Brooks/Cole,
2001
Figure 1-3
Brooks/Cole,
2001
Algorithm Efficiency
Efficiency is a function of the number of elements to
be processed (n):
f(n) = efficiency
Standard measures:
Linear
Logarithmic
Linear log
Quadratic
Polynomial
Exponential
Factorial
f(n) = n
f(n) = log2n
f(n) = nlog2n
f(n) = n2
f(n) = nk (k is largest term exponent)
f(n) = cn (c is a constant)
f(n) = n!
Brooks/Cole,
2001
Figure 1-4
Brooks/Cole,
2001
Brooks/Cole,
2001
Figure 1-5
Brooks/Cole,
2001
Algorithm 1-3
algorithm addMatrix (val matrix1 <matrix>, val matrix2 <matrix>, val size
<integer>, ref matrix3 <matrix>)
1.
r=0
2.
loop (r < size)
1. c = 0
2. loop (c < size)
1. matrix3[r, c] = matrix1[r, c] + matrix2[r, c]
2. c = c + 1
3. end loop
4. r = r + 1
3.
end loop
4.
return
end addMatrix
Brooks/Cole,
2001
Figure 1-6
Brooks/Cole,
2001
Algorithm 1-4
algorithm multiplyMatrix(val matrix1 <matrix>, val matrix2 <matrix>, val size <integer>, ref
matrix3 <matrix>)
1.
r=0
2.
loop (r < size)
1.
c=0
2.
loop (c < size)
1.
matrix3[r, c] = 0
2.
m=0
3.
loop (m < size)
1. matrix3[r, c] = matrix3[r,c] + matrix1[r,m] x matrix2[m,c]
2. m = m + 1
4.
end loop
5.
c=c+1
3.
end loop
4.
r=r+1
3.
end loop
4.
return
end multiplyMatrix
Brooks/Cole,
2001
Algorithm Analysis
Algorithm 1-4 is composed of three nested
loops
Big-O efficiency of O(size3), or O(n3)
Brooks/Cole,
2001