Professional Documents
Culture Documents
AoA Part2 Notes
AoA Part2 Notes
• Fixed part is usually ignored and only the variable part is considered.
• To analyze algorithms:
– First, we start to count the number of significant operations in a particular
solution to assess its efficiency.
– Then, we will express the efficiency of algorithms using growth functions.
A sequence of operations:
è Total Cost = c1 + c2
Algorithm arrayMax(A, n)
Input array A of n integers
Output maximum element of A
currentMax ¬ A[0]
for i ¬ 1 to n - 1 do
if A[i] > currentMax then
currentMax ¬ A[i]
return currentMax
For I = 1 Assigning 1
I<n Comparing two values N
True False
If current_max < A[ I ] Indexing 1 1
comparing 1 1
To summarize:
The no. of primitive operations executed by the algorithm atleast is
T(n)=2+1+n+2(n-1)+1
The no. of primitive operations executed by the algorithm atmost is
T(n)=2+1+n+4(n-1)+1
• Loops: The running time of a loop is at most the running time of the statements inside
of that loop times the number of iterations.
• Nested Loops: Running time of a nested loop containing a statement in the inner
most loop is the running time of statement multiplied by the product of the sized of all
loops.
• Consecutive Statements: Just add the running times of those consecutive statements.
• If/Else: Never more than the running time of the test plus the larger of running times
of S1 and S2.
Primitive Operations
Basic computations performed by an algorithm
Identifiable in pseudocode
Largely independent from the programming language
Assumed to take a constant amount of time in the RAM model
A CPU
An potentially unbounded bank of memory cells, each of which can hold an arbitrary
number or character
Memory cells are numbered and accessing any cell in memory takes unit time.
Examples:
Evaluating an expression
Assigning a value to a variable
Indexing into an array
Calling a method
Returning from a method
Measuring the performance of an Algorithm in relation with the input size n is called order of
growth/ Growth rate.
Log N Logarithmic
Log2N Log-squared
N Linear
N log N
N2 Quadratic
N3 Cubic
2N Exponential