Professional Documents
Culture Documents
Since the comparison is executed on each repetition of the loop and the
assignment is not, we should consider the comparison to be the algorithm’s
basic operation.
Finding the value of the largest element in
a list of n numbers.
Number of Comparisons
Element uniqueness problem
check whether all the elements in a given array of n elements are
distinct.
Element uniqueness problem
Matrix Multiplication
Given two n × n matrices A and B, find the time efficiency of the definition-
based algorithm for computing their product C = AB. By definition, C is an n ×
n matrix whose elements are computed as the scalar (dot) products of the
rows of matrix A and the columns of matrix B
Matrix Multiplication
Finding the number of binary digits in the
binary representation of a positive decimal integer
The most frequently executed operation here is not inside the while
loop but rather the comparison n > 1 that determines whether the
loop’s body will be executed.
Since the value of n is about halved on each repetition of the loop, the
answer should be about log2 n.
Analysis
At Iteration 1, Length of array = n
At Iteration 2, Length of array = n⁄2
At Iteration 3, Length of array = (n⁄2)⁄2 = n⁄2^2
Therefore, after Iteration k, Length of array = n⁄2^k
After k divisions, the length of array becomes 1
Length of array = n⁄2^k = 1 => n = 2^k
To find the value of k, take base2 log on both sides
=> log2 (n) = log2 (2^k)
=> log2 (n) = k log2 (2)
=> k = log2 (n)
Practice
Practice
Practice