Professional Documents
Culture Documents
• An algorithm is a sequence of
unambiguous instructions for solving a
Algorithms, Design and
problem, i.e., for obtaining a required
Analysis output for any legitimate input in a
Introduction. finite amount of time.
1
Selection sort Insertion sort
Pseudocode Pseudocode
• Mixture of English, math Very High-level • Mixture of English, math
expressions, and computer pseudocode: expressions, and computer Detailed pseudocode
code code
• Less detailed than a Algorithm arrayMax(A, n) • Less detailed than a Algorithm arrayMax(A, n)
program Input array A of n integers program Input array A of n integers
• Preferred notation for Output maximum element of A • Preferred notation for Output maximum element of A
describing algorithms currentMax ← A[0] describing algorithms currentMax ← A[0]
• Hides program design Step through each element in A, • Hides program design for i ← 1 to n − 1 do
issues updating currentMax when a issues if A[i] > currentMax then
• Can write at different levels bigger element is found • Can write at different levels currentMax ← A[i]
of detail. return currentMax of detail. return currentMax
2
Theoretical analysis of time
Pseudocode Details efficiency
Time efficiency is analyzed by determining the
• Control flow number of repetitions of the basic operation as a
• Method call
– if … then … [else …] var.method (arg [, arg…]) function of input size
– while … do … • Return value
– repeat … until … return expression
– for … do … • Expressions • Basic operation: the operation that contributes
– Indentation replaces braces ← Assignment most towards the running time of the algorithm
(like = in Java)
• Method declaration
= Equality testing
Algorithm method (arg [, arg …]) (like == in Java) input size
Input … n2 Superscripts and other
Output … mathematical formatting
allowed
T(n) ˜ copC(n)
running time execution time Number of times
for basic operation basic operation is
executed
3
Arithmetic Progression Prefix Averages (Linear)
7 • The following algorithm computes prefix averages in
• The running time of linear time by computing prefix sums (and averages)
prefixAverages1 is 6
O(1 + 2 + …+ n) Algorithm recPrefixSumAndAverage(X, A, n) T(n) operations
5 Input array X of n ≥ 1 integer.
• The sum of the first n integers
Empty array A; A is same size as X.
is n(n + 1) / 2 4
Output array A[0]…A[n-1] changed to hold prefix averages of X.
– There is a simple visual
proof of this fact 3 returns sum of X[0], X[1],…,X[n-1] #operations
if n=1 1
• Thus, algorithm 2 A[0] ← X[0] 3
prefixAverages1 runs in O(n2)
1 return A[0] 2
time tot ← recPrefixSumAndAverage (X,A,n-1) 3+T(n-1)
0 tot ← tot + X[n-1] 4
1 2 3 4 5 6 A[n-1] ← tot / n 4
return tot; 1
• Average case: A(n) – “average” over inputs of size • Formula indicating order of growth with
n unknown multiplicative constant
– Number of times the basic operation will be executed on e.g., C(n) ˜ cn2
typical input
4
Time efficiency of nonrecursive
algorithms Example: Sequential search
Steps in mathematical analysis of nonrecursive • Problem: Given a list of n elements and a
algorithms: search key K, find an element equal to K, if
any.
• Decide on parameter n indicating input siz
• Algorithm: Scan the list and compare its
• Identify algorithm ’s basic operatio successive elements with K until either a
• Determine worst, average, and best case for input of matching element is found (successful
size n
search) of the list is exhausted (unsuccessful
• Set up summation for C(n) reflecting algorithm ’s loop
structure
search)
• Simplify summation using standard formulas (see • Worst case
Appendix A)
• Best case