Beruflich Dokumente
Kultur Dokumente
AN EXAMPLE: INSERTION
SORT
Input: Array A of size n
Output: Sorted array in increasing order
InsertionSort(A, n) {
for i 2 to n {
key A[i]
j i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] A[j]
jj-1
}
A[j+1] key
}
}
INSERTION SORT
Statement
Effort
InsertionSort(A, n) {
for j = 2 to n {
c 1n
key = A[j]
c2(n-1)
i = j - 1; c3(n-1)
while (i > 0) and (A[i] > key) {
c4Tj
A[i+1] = A[i]
c5(Tj-(n-1))
i=i-1
c6(Tj-(n-1))
}
0
A[i+1] = key
c7(n-1)
}
0
}
Tj= t2 + t3 + + tn where tj is number of times you
shifting the element in right side to insert new number.
ANALYZING INSERTION
SORT
T(n)
can Tj be?
Best case
Worst case Average case-
ANALYZING INSERTION
SORT
T(n)
=c n+c (n1)
+
0
(n1)
+c (n1)
1
+c42jn(Tj)+c52jn(Tj1)
+c62jn(Tj1)
+c7(n1)
What
can Tj be?
Best case
The
ANALYZING INSERTION
SORT
T(n)
=c n+c (n1)
+
0
(n1)
+c (n1)
1
+c42jn(Tj)+c52jn(Tj1)
+c7(n1)
+c62jn(Tj1)
What
can Tj be?
Worst case
The
ASYMPTOTIC NOTATIONS
ASYMPTOTIC PERFORMANCE
ASYMPTOTIC PERFORMANCE
Asymptotic
ASYMPTOTIC NOTATION
Fortunately, there are asymptotic
notations which allow us to characterize
the
main
factors
affecting
an
algorithms running time without going
into detail
A good notation for large inputs.
The notations describe different rate-ofrelations between
theo(.)
defining
growth
Big-Oh O(.)
Little-Oh
function and the defined set of
functions.
Big-Omega (.)
Little-Omega (.)
Big-Theta (.)
for all n n0
BIG O NOTATION
Example
If f(n) = 2n2
g(n) is O(n3 )
Que: Show that f(n) is Big Oh g(n)
Ans: to prove this we have to show that 0
f(n) c g(n)
for all n n0
If we can find positive constants c and n0 then
we can say that f(n) is big oh g(n)
BIG O NOTATION
Example
1
If f(n) = 2n2 , g(n) is O(n3)
f(n) c g(n)
Or
For c=2 and no = 4 also, f(n) is big oh g(n)
BIG O NOTATION
Example
2
If f(n) = 8n+128, g(n) is O(n2)
To prove f(n) is big oh g(n) we have to prove
that
f(n) c g(n) for all n n0 and constant c
Lets take c=1
BIG-O NOTATION
When determining the order of a function
f(x), always try to pick the smallest g(x)
possible.
If f(x) is (g(x)) and g(x) is (f(x)), then f(x)
and g(x) are of the same order.
In practice, all big-O results are obtained for
functions that are positive for all values of
x.
-NOTATION
A
-NOTATION
Example:
-NOTATION
Example:
=0
so n0
- NOTATION DEFINITION
( g(n) ) denotes a family of functions.
``f(n) is Thetag(n),'' if we can
constants c1, c2, and n0 such that
0 c1 g(n) f(n) c2 g(n)
for all n n0
find
is said to asymptotic
tight bound.
For
reasonably
large
values of n, the function
f(n) is within the range of
constant multiples of
g(n)
f(n) is bounded below as
well as above.
RELATIONS BETWEEN
NOTAIONS
Example
NOTATION
7n2 54n +3
Thus c1 = 5, c2 = 9 and n0 = 28
7n3
54n2 + 10n + 3
Can g(n) be n2 ?
NO
c2n2 cannot be larger than n3
large n.
Thus you cannot find c1, c2 and n0
Thus f(n) (n2 )
for
LITTLE O-NOTATION
o(g(n) ={ f(n):
for any positive constant c > 0
there exists a constant n0 > 0 such
that
0 f(n) < cg(n) for all n n0 }
Limit f/g 0
LITTLE -NOTATION
(g(n) ={ f(n):
for any positive constant c > 0
there exists a constant n0 > 0 such
that
0 cg(n) < f(n) for all n n0 }
Limit g/f 0
O(1)
Logarithmic O(log n)
Linear O(n)
Quadratic O(n2)
Polynomial O(nk) k is a constant
Exponential
O(2n)
Exponential O(an) a is a constant and a > 1
THEOREM
R1:
>0
R2:
R3:
R4:
THEOREM
R5:
R6:
R7:
R8:
SOME HELPFUL
MATHEMATICS
1 b a 1
i a
2
i 1
n
i 2 12 22 n 2
i 1
n(n 1)(2n 1)
6
SOME HELPFUL
MATHEMATICS
Cubic Series: For n 0,
2
2
n
(
n
1
)
3
3
3
3
i
4
i 1
n
x 1
k 0
n
1
x
1 x
k 0
k
SOME HELPFUL
MATHEMATICS
Cubic Series: For n 0,
2
2
n
(
n
1
)
3
3
3
3
i
4
i 1
n
x 1
k 0
n
1
x
1 x
k 0
k