Professional Documents
Culture Documents
Algorithm
ALGORITHM DEIGN AND ANALYSIS
Execution times? Not a good measure as execution times are specific to a particular computer.
Number of statements executed? Not a good measure, since the number of statements varies
with the programming language as well as the style of the individual programmer.
Ideal solution? Let us assume that we express the running time of a given algorithm as a function
of the input size n (i.e., f(n)) and compare these different functions corresponding to running
times. This kind of comparison is independent of machine time, programming style, etc.
Algorithm 1: f(n) = n
Algorithm 2: f(n) = n2
As an example, in the case below, n4, 2n2, 100n and 500 are the individual costs of
some function and approximate to n4 since n4 is the highest rate of growth.
n+1
f(n) = 2n + 1
O(n)
n+1
n*(n+1)
n*(n)
f(n) = 2n2 + 2n + 1
O(n2)
0 0x 0
1 0 1
2 0 2
1
3 0 3
1
2
.
.
n n Total no. of executions
Here, this algorithm is same to previous algorithm, however, in this 1 + 2 + 3 + 4 + 5 +…+ n = n*(n+1)/2
algorithm the inner loop executes correspond to outer loop(i) value. f(n) = n2 + n / 2
O(n2)
2 1+2 = 3 p = k * ( k+1) / 2
i
Assume i >= n
1
1*2=2 Since i = 2k
2 * 2 = 22
22 * 2 = 23 Therefore,
. 2k > = n
. 2k = n
. k = log2n
2k O(log2n)
i
Assume i < 1
n
Since i = n/2k
n/2
Therefore,
n/22 n / 2k < 1
n/23
n /2k = 1
.
.
. n = 2k
k = log2n
n/2k
O(log2n)
i*i<n
i2 = n
i= 𝒏
n * log2n
n * log2n
f(n) = 2 n log n + n
O(n)
O(n)
O(n)
O(log2n)
O(log3n)
O(log2n)