2. ▪ The efficient algorithm
▪ Average, Best and worst case analysis
▪ Amortized analysis
▪ Asymptotic Notations
▪ Analyzing control statement
▪ Loop invariant and the correctness of the algorithm
▪ Sorting Algorithms and analysis:
▪ Bubble sort, Selection sort, Insertion sort, Shell sort Heap sort
▪ Sorting in linear time :
▪ Bucket sort, Radix sort and Counting sort
Prepared by: Prof. Khushali B Kathiriya 2
5. ▪ Importance of Efficiency analysis
1. By computing the time complexity we come to know whether algorithm is slow or
fast.
2. By computing the space complexity we come to know whether algorithm requires
more or less space.
Prepared by: Prof. Khushali B Kathiriya 5
7. ▪ Example-1
▪ The FC is a count that denotes how many times particular statement is executed.
Prepared by: Prof. Khushali B Kathiriya 7
Void fun()
{
int a;
a=10;
printf(“%d”, a);
}
Void fun()
{
int a;
a=10; ……………………………………… 1 time
printf(“%d”, a); …………………….... 1 time
}
8. ▪ Example-2
Prepared by: Prof. Khushali B Kathiriya 8
void Display()
{
int a, b, c;
a = 10;
b = 20;
c = a + b;
printf (“%d”,c);
}
CODE Frequency Count
a = 10 1
b = 20 1
c = a + b 1
printf (“%d”, c ) 1
Total 4
9. ▪ Example-3
Prepared by: Prof. Khushali B Kathiriya 9
Void fun()
{
int a;
a=0;
for(i=0; i<=n; i++)
{
a = a +i;
}
printf(“%d”, a);
}
Void fun()
{
int a;
a=0; …………………………………………………. 1 time
for(i=0; i<=n; i++) ………………………………….. N +1 time
{
a = a +i; ………………………………………….. N time
}
printf(“%d”, a); ………………………………………1 time
}
13. ▪ We can have three cases to analyze an algorithm:
1. The Worst Case
2. Average Case
3. Best Case
Prepared by: Prof. Khushali B Kathiriya 13
14. ▪ If Algorithm takes minimum time to solve the problem for given set of input, it is
called best case time complexity.
▪ In the best case analysis, we calculate lower bound on running time of an algorithm.
We must know the case that causes minimum number of operations to be executed.
▪ We can denote the best case time complexity as
▪ 𝑪𝒃𝒆𝒔𝒕 = 1 Or T(n) = Θ(1)
Prepared by: Prof. Khushali B Kathiriya 14
15. ▪ If Algorithm takes maximum time to solve the problem for given set of input, it is
called worst case time complexity.
▪ In the worst case analysis, we calculate upper bound on running time of an
algorithm. We must know the case that causes maximum number of operations to be
executed.
▪ Therefore, the worst case time complexity of linear search would be T(n)= Θ(n).
Prepared by: Prof. Khushali B Kathiriya 15
16. ▪ Input sequence which is neighed best nor worst is called average case.
▪ It gives general behaviour of an algorithm.
▪ CAVG(n) = Probability of successful search + Probability of unsuccessful search
▪ CAVG(n) = 1.
P
n
+ 2.
P
n
+ … +. i
P
n
+ n. 1 − P
=
P
n
[1 + 2 + .. i … n ] + n(1-P)
=
P
n
.
n (n+1)
2
+ n (1-P)
=
P (n+1)
2
+ n (1-P)
Prepared by: Prof. Khushali B Kathiriya 16
17. ▪ CAVG(n) =
P (n+1)
2
+ n (1-P)
▪ Suppose if P=0,
▪ CAVG(n) =
P (n+1)
2
+ n (1-P)
=
0 (n+1)
2
+ n (1-0)
= n
▪ Suppose if P=1,
▪ CAVG(n) =
P (n+1)
2
+ n (1-P)
=
1 (n+1)
2
+ n (1-1)
=
(n+1)
2
Prepared by: Prof. Khushali B Kathiriya 17
19. ▪ To choose the best algo., we need to check efficiency of each algorithm. The
efficiency can be measured by computing time complexity of each algorithm.
▪ Using Asymptotic Notations we can give time complexity as “fastest”, “slowest”, or
“ average” possible time.
Prepared by: Prof. Khushali B Kathiriya 19
Asymptotic
Notations
Big oh Notation
(O)
Omega Notation
(Ω)
Theta Notation
(Θ)
20. O
▪ It represents the upper bound of the runtime of an algorithm.
▪ Big O Notation's role is to calculate the longest time an algorithm can take for its
execution,
▪ i.e., it is used for calculating the worst-case time complexity of an algorithm.
▪ Let f(n) and g(n) be 2 non-negative functions.
▪ We want: f(n) = O(g(n))
▪ So we can write i.e., f(n) ≤ c * g(n)
▪ Where, c>0, n>=k, k>=0.
▪ Then f(n) is big oh of g(n) or f(n) ∈ O(g(n)).
Prepared by: Prof. Khushali B Kathiriya 20
21. O
▪ f(n) ≤ c * g(n)
Prepared by: Prof. Khushali B Kathiriya 21
22. O
▪ Example-1: Consider function f(n)= 2n+2 and g(n)= 𝑛2
. Then we have to find some
constant c.
▪ So, we can conclude that for n>2, we obtain f(n)<g(n).
Prepared by: Prof. Khushali B Kathiriya 22
n=1,
f(n)=2n+2
f(1)=2(1)+2 = 4
g(n) = 𝑛2
g(1) = 12 = 1
So, f(n) > g(n).
n=2,
f(n)=2n+2
f(2)=2(2)+2 = 6
g(n) = 𝑛2
g(2) = 22 = 4
So, f(n) > g(n).
n=3,
f(n)=2n+2
f(3)=2(3)+2 = 8
g(n) = 𝑛2
g(3) = 32 = 9
So, f(n) < g(n).
24. Ω
▪ It represents the lower bound of the runtime of an algorithm.
▪ Big Ω Notation's role is to calculate the shortest amount of time an algorithm can
take for its execution,
▪ i.e., it is used for calculating the best time complexity of an algorithm.
▪ Let f(n) and g(n) be 2 non-negative functions.
▪ We want: f(n) = O(g(n))
▪ So we can write i.e., f(n) ≥ c * g(n)
▪ Where, c>0, n>=k, k>=0.
▪ Then f(n) is big oh of g(n) or f(n) ∈ O(g(n)).
Prepared by: Prof. Khushali B Kathiriya 24
25. Ω
▪ f(n) ≥ c * g(n)
Prepared by: Prof. Khushali B Kathiriya 25
26. Ω
▪ Example-1: Consider function f(n)= 2𝑛2
+5 and g(n)=7n. Then we have to find some
constant c.
▪ So, we can conclude that for n>1, we obtain f(n) ≥ g(n).
Prepared by: Prof. Khushali B Kathiriya 26
n=1,
f(n)= 2𝑛2
+5
f(1)= 2(1)2
+5 = 7
g(n) =7n
g(1) = 7(1)= 7
So, f(n) ≥ g(n).
n=2,
f(n)= 2𝑛2
+5
f(2)=2(2)2
+5 = 13
g(n) =7n
g(2) = 7(2)= 14
So, f(n) ≥ g(n).
28. ▪ By this method the running time is between upper and lower bound.
▪ C1* g(n) ≤ f(n) ≤ C2 * g(n)
▪ Then we can say that,
▪ F(n) ∈ 𝜃 (g(n))
Prepared by: Prof. Khushali B Kathiriya 28
55. ▪ Arranging the elements in increasing order or decreasing order.
▪ It can be
▪ Number(0-9),
▪ Character ( A- Z)
▪ String
▪ Records etc.
Prepared by: Prof. Khushali B Kathiriya 55
56. 1. Bubble sort
2. Selection sort
3. Insertion sort
4. Shell sort
5. Heap sort
6. Bucket sort
7. Radix sort
8. Counting sort
Prepared by: Prof. Khushali B Kathiriya 56
Sorting in Linear time
79. ▪ This method is a improvement over the simple insertion sort.
▪ In this method the elements at fixed distance are compared. The distance will then
decremented by the fixed amount and again the comparison will be made.
Prepared by: Prof. Khushali B Kathiriya 79
80. Prepared by: Prof. Khushali B Kathiriya 80
61 109 149 111 34 2 24 119 122 125 27 145
12 Elements
81. ▪ Total elements N =12
▪ So here,
K= N/2
K=12/2
K=6(K is distance between arrry set)
Prepared by: Prof. Khushali B Kathiriya 81
126. ▪ In this sort method array is partitioned into buckets.
▪ Each bucket is then sorted individually, using some other sorting algorithm such as
insertion sort.
▪ Algorithm
1. Set up array of initially empty buckets
2. Put each element in corresponding bucket
3. Sort each non empty bucket
4. Visit the buckets in order and put all the elements into a sequence and print them.
Prepared by: Prof. Khushali B Kathiriya 126
127. ▪ Array= [0.56, 0.12, 0.84, 0.56, 0.28, 0, -0.13, 0.47, 0.94, 0.31, 0.12, -0.2]
▪ Here we have 12 elements.
▪ Now we generate our buckets from 0 to 9.
Prepared by: Prof. Khushali B Kathiriya 127