SlideShare a Scribd company logo
1 of 150
Download to read offline
Prepared by:
Prof. Khushali B Kathiriya
SUB. CODE: 3150703
SEMESTER :5TH IT
▪ 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
Prepared by:
Prof. Khushali B Kathiriya
Efficiency of Algorithm
Time Complexity
Space
Complexity
Prepared by: Prof. Khushali B Kathiriya 4
▪ 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
Prepared by:
Prof. Khushali B Kathiriya
▪ 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
}
▪ 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
▪ 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
}
▪ Example-4
Prepared by: Prof. Khushali B Kathiriya 10
for(i=0; i<=n; i++)
{
for(j=1; j<=n; j++)
{
c[i][j]=0;
for(k=1;k<=n; k++)
{
c[i][j]= c[i][j]+a[i][k] * b[k][j];
}
}
}
for(i=0; i<=n; i++)
{
for(j=1; j<=n; j++)
{
c[i][j]=0;
for(k=1;k<=n; k++)
{
c[i][j]= c[i][j]+a[i][k] * b[k][j];
}
}
}
………………………….n+1
………………………….n(n+1)
………………………….n*n
………………………….n*n*(n+1)
…….n*n*n
▪ Example-4 (Cont.)
▪ Total time
=(n+1) + n(n+1) + n*n + n*n*(n+1) + (n*n*n)
= 2𝑛3 + 3𝑛2 + 2n +1
Prepared by: Prof. Khushali B Kathiriya 11
for(i=0; i<=n; i++)
{
for(j=1; j<=n; j++)
{
c[i][j]=0;
for(k=1;k<=n; k++)
{
c[i][j]= c[i][j]+a[i][k] * b[k][j];
}
}
}
………………………….n+1
………………………….n(n+1)
………………………….n*n
………………………….n*n*(n+1)
…….n*n*n
Prepared by:
Prof. Khushali B Kathiriya
▪ 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
▪ 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
▪ 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
▪ 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
▪ 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
Prepared by:
Prof. Khushali B Kathiriya
▪ 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
(Θ)
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
O
▪ f(n) ≤ c * g(n)
Prepared by: Prof. Khushali B Kathiriya 21
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).
O
23
Prepared by: Prof. Khushali B Kathiriya
Ω
▪ 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
Ω
▪ f(n) ≥ c * g(n)
Prepared by: Prof. Khushali B Kathiriya 25
Ω
▪ 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).
Ω
Prepared by: Prof. Khushali B Kathiriya 27
▪ 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
Prepared by: Prof. Khushali B Kathiriya 29
Prepared by:
Prof. Khushali B Kathiriya
Prepared by: Prof. Khushali B Kathiriya 31
Prepared by: Prof. Khushali B Kathiriya 32
Prepared by:
Prof. Khushali B Kathiriya
Prepared by: Prof. Khushali B Kathiriya 34
Prepared by: Prof. Khushali B Kathiriya 35
Prepared by:
Prof. Khushali B Kathiriya
Prepared by: Prof. Khushali B Kathiriya 37
Prepared by: Prof. Khushali B Kathiriya 38
Prepared by: Prof. Khushali B Kathiriya 39
Prepared by: Prof. Khushali B Kathiriya 40
Prepared by:
Prof. Khushali B Kathiriya
(Most IMP)
Prepared by: Prof. Khushali B Kathiriya 42
Prepared by: Prof. Khushali B Kathiriya 43
Prepared by:
Prof. Khushali B Kathiriya
Prepared by: Prof. Khushali B Kathiriya 45
Prepared by:
Prof. Khushali B Kathiriya
Prepared by: Prof. Khushali B Kathiriya 47
Prepared by: Prof. Khushali B Kathiriya 48
Prepared by: Prof. Khushali B Kathiriya 49
Prepared by:
Prof. Khushali B Kathiriya
Prepared by: Prof. Khushali B Kathiriya 51
Prepared by: Prof. Khushali B Kathiriya 52
Prepared by: Prof. Khushali B Kathiriya 53
Prepared by:
Prof. Khushali B Kathiriya
▪ 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
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
Prepared by:
Prof. Khushali B Kathiriya
Prepared by: Prof. Khushali B Kathiriya 58
59
Prepared by: Prof. Khushali B Kathiriya
Prepared by: Prof. Khushali B Kathiriya 60
Prepared by: Prof. Khushali B Kathiriya 61
▪ Consider elements for bubble sort
1. 70, 30, 20, 50, 60, 10, 40
2. DESIGN
Prepared by: Prof. Khushali B Kathiriya 62
Prepared by:
Prof. Khushali B Kathiriya
Prepared by: Prof. Khushali B Kathiriya 64
Prepared by: Prof. Khushali B Kathiriya 65
Prepared by: Prof. Khushali B Kathiriya 66
67
Prepared by: Prof. Khushali B Kathiriya
Prepared by: Prof. Khushali B Kathiriya 68
Prepared by: Prof. Khushali B Kathiriya 69
Prepared by: Prof. Khushali B Kathiriya 70
▪ Consider elements for selection sort
1. 70, 30, 20, 50, 60, 10, 40
2. ANALYSIS
Prepared by: Prof. Khushali B Kathiriya 71
Prepared by:
Prof. Khushali B Kathiriya
Prepared by: Prof. Khushali B Kathiriya 73
Prepared by: Prof. Khushali B Kathiriya 74
75
Prepared by: Prof. Khushali B Kathiriya
76
Prepared by: Prof. Khushali B Kathiriya
▪ Consider elements for insertion sort
1. 70, 30, 20, 50, 60, 10, 40
Prepared by: Prof. Khushali B Kathiriya 77
Prepared by:
Prof. Khushali B Kathiriya
▪ 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
Prepared by: Prof. Khushali B Kathiriya 80
61 109 149 111 34 2 24 119 122 125 27 145
12 Elements
▪ 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
Prepared by: Prof. Khushali B Kathiriya 82
61 109 149 111 34 2 24 119 122 125 27 145
▪ K= 6
Prepared by: Prof. Khushali B Kathiriya 83
61 109 149 111 34 2 24 119 122 125 27 145
▪ K= 6
24 109 149 111 34 2 61 119 122 125 27 145
▪ K=6
Prepared by: Prof. Khushali B Kathiriya 84
24 109 149 111 34 2 62 119 122 125 27 145
24 109 149 111 34 2 61 119 122 125 27 145
▪ K=6
Prepared by: Prof. Khushali B Kathiriya 85
24 109 149 111 34 2 62 119 122 125 27 145
24 109 122 111 34 2 61 119 149 125 27 145
▪ K=6
Prepared by: Prof. Khushali B Kathiriya 86
24 109 149 111 34 2 62 119 122 125 27 145
24 109 122 111 34 2 61 119 149 125 27 145
▪ K=6
Prepared by: Prof. Khushali B Kathiriya 87
24 109 149 111 34 2 62 119 122 125 27 145
24 109 122 111 27 2 61 119 149 125 34 145
▪ K=6
Prepared by: Prof. Khushali B Kathiriya 88
24 109 149 111 34 2 62 119 122 125 27 145
24 109 122 111 27 2 61 119 149 125 34 145
▪ K=K/2 = 6/2 = 3
Prepared by: Prof. Khushali B Kathiriya 89
24 109 122 111 27 2 61 119 149 125 34 145
24 109 122 111 27 2 61 119 149 125 34 145
▪ K=K/2 = 6/2 = 3
Prepared by: Prof. Khushali B Kathiriya 90
24 109 122 111 27 2 61 119 149 125 34 145
24 27 122 111 109 2 61 119 149 125 34 145
▪ K=K/2 = 6/2 = 3
Prepared by: Prof. Khushali B Kathiriya 91
24 109 122 111 27 2 61 119 149 125 34 145
24 27 2 111 109 122 61 119 149 125 34 145
▪ K=K/2 = 6/2 = 3
Prepared by: Prof. Khushali B Kathiriya 92
24 109 2 111 109 122 61 119 149 125 34 145
24 27 2 61 109 122 111 119 149 125 34 145
▪ K=K/2 = 6/2 = 3
Prepared by: Prof. Khushali B Kathiriya 93
24 27 2 61 109 122 111 119 149 125 34 145
24 27 2 61 109 122 111 119 149 125 34 145
▪ K=K/2 = 6/2 = 3
Prepared by: Prof. Khushali B Kathiriya 94
24 27 2 61 109 122 111 119 149 125 34 145
24 27 2 61 109 122 111 119 149 125 34 145
▪ K=K/2 = 6/2 = 3
Prepared by: Prof. Khushali B Kathiriya 95
24 27 2 61 109 122 111 119 149 125 34 145
24 27 2 61 109 122 111 119 149 125 34 145
▪ K=K/2 = 6/2 = 3
Prepared by: Prof. Khushali B Kathiriya 96
24 27 2 61 109 122 111 119 149 125 34 145
24 27 2 61 109 122 111 34 149 125 119 145
▪ K=K/2 = 6/2 = 3
Prepared by: Prof. Khushali B Kathiriya 97
24 27 2 61 109 122 111 34 149 125 119 145
24 27 2 61 109 122 111 34 145 125 119 149
▪ K=K/2 = 6/2 = 3
Prepared by: Prof. Khushali B Kathiriya 98
24 27 2 61 109 122 111 34 145 125 119 149
24 27 2 61 34 122 111 109 145 125 119 149
▪ K=K/2 =3 /2 = 1.5 = 1
Prepared by: Prof. Khushali B Kathiriya 99
24 27 2 61 34 122 111 109 145 125 119 149
24 27 2 61 34 122 111 109 145 125 119 149
▪ K=K/2 =3 /2 = 1.5 = 1
Prepared by: Prof. Khushali B Kathiriya 100
24 2 27 61 34 122 111 109 145 125 119 149
24 2 27 61 34 122 111 109 145 125 119 149
2 24 27 61 34 122 111 109 145 125 119 149
▪ K=K/2 =3 /2 = 1.5 = 1
Prepared by: Prof. Khushali B Kathiriya 101
2 24 27 61 34 122 111 109 145 125 119 149
2 24 27 61 34 122 111 109 145 125 119 149
▪ K=K/2 =3 /2 = 1.5 = 1
Prepared by: Prof. Khushali B Kathiriya 102
2 24 27 61 34 122 111 109 145 125 119 149
2 24 27 34 61 122 111 109 145 125 119 149
▪ K=K/2 =3 /2 = 1.5 = 1
Prepared by: Prof. Khushali B Kathiriya 103
2 24 27 34 61 122 111 109 145 125 119 149
2 24 27 34 61 122 111 109 145 125 119 149
▪ K=K/2 =3 /2 = 1.5 = 1
Prepared by: Prof. Khushali B Kathiriya 104
2 24 27 34 61 122 111 109 145 125 119 149
2 24 27 34 61 111 122 109 145 125 119 149
▪ K=K/2 =3 /2 = 1.5 = 1
Prepared by: Prof. Khushali B Kathiriya 105
2 24 27 34 61 111 122 109 145 125 119 149
2 24 27 34 61 111 109 122 145 125 119 149
2 24 27 34 61 109 111 122 145 125 119 149
▪ K=K/2 =3 /2 = 1.5 = 1
Prepared by: Prof. Khushali B Kathiriya 106
2 24 27 34 61 109 111 122 145 125 119 149
2 24 27 34 61 109 111 122 145 125 119 149
▪ K=K/2 =3 /2 = 1.5 = 1
Prepared by: Prof. Khushali B Kathiriya 107
2 24 27 34 61 109 111 122 145 125 119 149
2 24 27 34 61 109 111 122 125 145 119 149
▪ K=K/2 =3 /2 = 1.5 = 1
Prepared by: Prof. Khushali B Kathiriya 108
2 24 27 34 61 109 111 122 125 145 119 149
2 24 27 34 61 109 111 122 125 119 145 149
2 24 27 34 61 109 111 122 119 125 145 149
2 24 27 34 61 109 111 119 122 125 145 149
▪ Consider elements for shell sort
1. 70, 30, 20, 50, 60, 10, 40
Prepared by: Prof. Khushali B Kathiriya 109
Prepared by:
Prof. Khushali B Kathiriya
Heap Sort
Heap
Construction
Deletion of
maximum
key
Prepared by: Prof. Khushali B Kathiriya 111
▪ Array = [14, 12 , 9, 8, 7, 10 , 18]
▪ Step:1
▪ Heap Construction
Prepared by: Prof. Khushali B Kathiriya 112
14
9
12
7
8 10 18
▪ Step: 2
▪ Start Heaping from bottom and Swap with maximum no.
Prepared by: Prof. Khushali B Kathiriya 113
14
18
12
7
8 10 9
14
9
12
7
8 10 18
Array = [14, 12 , 18, 8, 7, 10 , 9]
Swap with maximum
no.
▪ Step:3
▪ Heaping from top and swapping with maximum no.
Prepared by: Prof. Khushali B Kathiriya 114
14
18
12
7
8 10 9
18
14
12
7
8 10 9
Array = [18, 12 , 14, 8, 7, 10 , 9]
▪ Step: 4
▪ Swapping with last node and then delete maximum number
Prepared by: Prof. Khushali B Kathiriya 115
18
14
12
7
8 10 9
9
14
12
7
8 10 18
Array = [9, 12 , 14, 8, 7, 10 , 18]
▪ Step: 5
▪ Heaping with maximum number
Prepared by: Prof. Khushali B Kathiriya 116
9
14
12
7
8 10
14
9
12
7
8 10
Array = [14, 12 , 9, 8, 7, 10 | 18]
▪ Step: 6
Prepared by: Prof. Khushali B Kathiriya 117
14
9
12
7
8 10
10
9
12
7
8 14
Array = [10, 12 , 9, 8, 7| 14 , 18]
▪ Step: 7
Prepared by: Prof. Khushali B Kathiriya 118
10
9
12
7
8
12
9
10
7
8
Array = [12, 10 , 9, 8, 7 | 14, 18]
▪ Step: 8
Prepared by: Prof. Khushali B Kathiriya 119
7
9
10
12
8
7
9
10
8
Array = [7, 10 , 9, 8 |12, 14, 18]
▪ Step : 9
Prepared by: Prof. Khushali B Kathiriya 120
7
9
10
8
10
9
7
8
10
9
8
7
Array = [10 , 8, 9, 7 |12, 14, 18]
▪ Step: 10
Prepared by: Prof. Khushali B Kathiriya 121
7
9
8
10
7
9
8
Array = [ 7, 8, 9 |10, 12, 14, 18]
▪ Step: 11
Prepared by: Prof. Khushali B Kathiriya 122
7
9
8
9
7
8
7
9
8
7
8
Array = [ 7, 8 |9, 10, 12, 14, 18]
▪ Step: 12
Prepared by: Prof. Khushali B Kathiriya 123
7
8
7
Array = [ 7 |8, 9, 10, 12, 14, 18]
Array = [ 7, 8, 9, 10, 12, 14, 18] : Sorted Array
▪ Consider elements for heap sort
1. 70, 30, 20, 50, 60, 10, 40
Prepared by: Prof. Khushali B Kathiriya 124
Prepared by:
Prof. Khushali B Kathiriya
▪ 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
▪ 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
0
1
2
3
4
5
6
7
8
9
Prepared by: Prof. Khushali B Kathiriya 128
▪ Step: 1
▪ Generate bucket from 0 to 1
▪ Step: 2
▪ Fill bucket using given array
▪ Array= [0.56, 0.12, 0.84, 0.56, 0.28, 0, -0.13, 0.47, 0.94, 0.31, 0.12, -0.2]
Prepared by: Prof. Khushali B Kathiriya 129
0
1
2
3
4
5
6
7
8
9
0.56
0.12
0.84
0.56
0.28
0 -0.13
0.47
0.94
0.31
0.12
-0.2
▪ Step: 3
▪ Sort each non empty bucket
Prepared by: Prof. Khushali B Kathiriya 130
130
0
1
2
3
4
5
6
7
8
9
0.56
0.12
0.84
0.56
0.28
0 -0.13
0.47
0.94
0.31
0.12
-0.2 0
1
2
3
4
5
6
7
8
9
-0.2,-0.13,0
0.12, 0.12
0.28
0.47
0.31
0.56 , 0.56
0.84
0.94
▪ Step: 4
▪ Put all elements bucket vise in one sequence
Prepared by: Prof. Khushali B Kathiriya 131
131
0
1
2
3
4
5
6
7
8
9
-0.2,-0.13,0
0.12, 0.12
0.28
0.47
0.31
0.56 , 0.56
0.84
0.94
Sorted array is:
[-0.2,-0.13,0,0.12,0.12,0.28, 0.31, 0.47, 0.56, 0.56, 0.84, 0.94 ]
▪ Consider elements for Bucket sort
1. 121,235,456,123,674,01,90,10,56,45,87,899
Prepared by: Prof. Khushali B Kathiriya 132
Prepared by:
Prof. Khushali B Kathiriya
Prepared by: Prof. Khushali B Kathiriya 134
Prepared by: Prof. Khushali B Kathiriya 135
Prepared by: Prof. Khushali B Kathiriya 136
Prepared by: Prof. Khushali B Kathiriya 137
Prepared by: Prof. Khushali B Kathiriya 138
Prepared by:
Prof. Khushali B Kathiriya
140
Prepared by: Prof. Khushali B Kathiriya
Prepared by: Prof. Khushali B Kathiriya 141
Prepared by: Prof. Khushali B Kathiriya 142
Prepared by: Prof. Khushali B Kathiriya 143
Prepared by: Prof. Khushali B Kathiriya 144
Prepared by: Prof. Khushali B Kathiriya 145
Prepared by: Prof. Khushali B Kathiriya 146
Prepared by: Prof. Khushali B Kathiriya 147
Prepared by: Prof. Khushali B Kathiriya 148
Prepared by: Prof. Khushali B Kathiriya 149
Prepared by: Prof. Khushali B Kathiriya 150

More Related Content

What's hot

Quick sort algorithn
Quick sort algorithnQuick sort algorithn
Quick sort algorithn
Kumar
 

What's hot (20)

Searching and Sorting Techniques in Data Structure
Searching and Sorting Techniques in Data StructureSearching and Sorting Techniques in Data Structure
Searching and Sorting Techniques in Data Structure
 
Bubble sort
Bubble sortBubble sort
Bubble sort
 
Algorithms Lecture 4: Sorting Algorithms I
Algorithms Lecture 4: Sorting Algorithms IAlgorithms Lecture 4: Sorting Algorithms I
Algorithms Lecture 4: Sorting Algorithms I
 
Ppt bubble sort
Ppt bubble sortPpt bubble sort
Ppt bubble sort
 
3.3 shell sort
3.3 shell sort3.3 shell sort
3.3 shell sort
 
Searching & Sorting Algorithms
Searching & Sorting AlgorithmsSearching & Sorting Algorithms
Searching & Sorting Algorithms
 
Linear search-and-binary-search
Linear search-and-binary-searchLinear search-and-binary-search
Linear search-and-binary-search
 
SEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMSSEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMS
 
Insertion Sorting
Insertion SortingInsertion Sorting
Insertion Sorting
 
Bubble sort
Bubble sortBubble sort
Bubble sort
 
Insertion Sort Algorithm
Insertion Sort AlgorithmInsertion Sort Algorithm
Insertion Sort Algorithm
 
Analysis of algorithm
Analysis of algorithmAnalysis of algorithm
Analysis of algorithm
 
Algorithm And analysis Lecture 03& 04-time complexity.
 Algorithm And analysis Lecture 03& 04-time complexity. Algorithm And analysis Lecture 03& 04-time complexity.
Algorithm And analysis Lecture 03& 04-time complexity.
 
CS8391 Data Structures Part B Questions Anna University
CS8391 Data Structures Part B Questions Anna UniversityCS8391 Data Structures Part B Questions Anna University
CS8391 Data Structures Part B Questions Anna University
 
Shell sort[1]
Shell sort[1]Shell sort[1]
Shell sort[1]
 
Heap and heapsort
Heap and heapsortHeap and heapsort
Heap and heapsort
 
Shell sort
Shell sortShell sort
Shell sort
 
Quick sort algorithn
Quick sort algorithnQuick sort algorithn
Quick sort algorithn
 
Rahat &amp; juhith
Rahat &amp; juhithRahat &amp; juhith
Rahat &amp; juhith
 
Shell sort
Shell sortShell sort
Shell sort
 

More from Khushali Kathiriya

More from Khushali Kathiriya (20)

Chap.3 Knowledge Representation Issues Chap.4 Inference in First Order Logic
Chap.3 Knowledge Representation  Issues  Chap.4 Inference in First Order  LogicChap.3 Knowledge Representation  Issues  Chap.4 Inference in First Order  Logic
Chap.3 Knowledge Representation Issues Chap.4 Inference in First Order Logic
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
learning with complete data.pdf
learning with complete data.pdflearning with complete data.pdf
learning with complete data.pdf
 
CV_Chap 6 Motion Representation
CV_Chap 6 Motion RepresentationCV_Chap 6 Motion Representation
CV_Chap 6 Motion Representation
 
Cv_Chap 4 Segmentation
Cv_Chap 4 SegmentationCv_Chap 4 Segmentation
Cv_Chap 4 Segmentation
 
CV_Chap 3 Features Detection
CV_Chap 3 Features DetectionCV_Chap 3 Features Detection
CV_Chap 3 Features Detection
 
CV_2 Fourier_Transformation
CV_2 Fourier_Transformation CV_2 Fourier_Transformation
CV_2 Fourier_Transformation
 
CV_2 Filtering_Example
CV_2 Filtering_ExampleCV_2 Filtering_Example
CV_2 Filtering_Example
 
CV_2 Image Processing
CV_2 Image ProcessingCV_2 Image Processing
CV_2 Image Processing
 
CV_1 Introduction of Computer Vision and its Application
CV_1 Introduction of Computer Vision and its ApplicationCV_1 Introduction of Computer Vision and its Application
CV_1 Introduction of Computer Vision and its Application
 
ADA_1 Introduction of Algorithm
ADA_1 Introduction of AlgorithmADA_1 Introduction of Algorithm
ADA_1 Introduction of Algorithm
 
AI_ Backtracking in prolog
AI_ Backtracking in prologAI_ Backtracking in prolog
AI_ Backtracking in prolog
 
AI_Cuts in prolog
AI_Cuts in prologAI_Cuts in prolog
AI_Cuts in prolog
 
AI_Recursive search in prolog
AI_Recursive search in prologAI_Recursive search in prolog
AI_Recursive search in prolog
 
AI_List in prolog
AI_List in prologAI_List in prolog
AI_List in prolog
 
AI_11 Game playing
AI_11 Game playingAI_11 Game playing
AI_11 Game playing
 
AI_ Bays theorem
AI_ Bays theoremAI_ Bays theorem
AI_ Bays theorem
 
AI_Bayesian belief network
AI_Bayesian belief networkAI_Bayesian belief network
AI_Bayesian belief network
 
AI_11 Understanding
AI_11  UnderstandingAI_11  Understanding
AI_11 Understanding
 
AI_ 8 Weak Slot and Filler Structure
AI_ 8 Weak Slot and Filler  StructureAI_ 8 Weak Slot and Filler  Structure
AI_ 8 Weak Slot and Filler Structure
 

Recently uploaded

Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
rknatarajan
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
dharasingh5698
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Christo Ananth
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
ankushspencer015
 

Recently uploaded (20)

ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
 
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
 
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 
Vivazz, Mieres Social Housing Design Spain
Vivazz, Mieres Social Housing Design SpainVivazz, Mieres Social Housing Design Spain
Vivazz, Mieres Social Housing Design Spain
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdf
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
 
Call for Papers - International Journal of Intelligent Systems and Applicatio...
Call for Papers - International Journal of Intelligent Systems and Applicatio...Call for Papers - International Journal of Intelligent Systems and Applicatio...
Call for Papers - International Journal of Intelligent Systems and Applicatio...
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELLPVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
 
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdf
 
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
 
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
 

ADA_2_Analysis of Algorithms

  • 1. Prepared by: Prof. Khushali B Kathiriya SUB. CODE: 3150703 SEMESTER :5TH IT
  • 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
  • 4. Efficiency of Algorithm Time Complexity Space Complexity Prepared by: Prof. Khushali B Kathiriya 4
  • 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 }
  • 10. ▪ Example-4 Prepared by: Prof. Khushali B Kathiriya 10 for(i=0; i<=n; i++) { for(j=1; j<=n; j++) { c[i][j]=0; for(k=1;k<=n; k++) { c[i][j]= c[i][j]+a[i][k] * b[k][j]; } } } for(i=0; i<=n; i++) { for(j=1; j<=n; j++) { c[i][j]=0; for(k=1;k<=n; k++) { c[i][j]= c[i][j]+a[i][k] * b[k][j]; } } } ………………………….n+1 ………………………….n(n+1) ………………………….n*n ………………………….n*n*(n+1) …….n*n*n
  • 11. ▪ Example-4 (Cont.) ▪ Total time =(n+1) + n(n+1) + n*n + n*n*(n+1) + (n*n*n) = 2𝑛3 + 3𝑛2 + 2n +1 Prepared by: Prof. Khushali B Kathiriya 11 for(i=0; i<=n; i++) { for(j=1; j<=n; j++) { c[i][j]=0; for(k=1;k<=n; k++) { c[i][j]= c[i][j]+a[i][k] * b[k][j]; } } } ………………………….n+1 ………………………….n(n+1) ………………………….n*n ………………………….n*n*(n+1) …….n*n*n
  • 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).
  • 23. O 23 Prepared by: Prof. Khushali B Kathiriya
  • 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).
  • 27. Ω Prepared by: Prof. Khushali B Kathiriya 27
  • 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
  • 29. Prepared by: Prof. Khushali B Kathiriya 29
  • 31. Prepared by: Prof. Khushali B Kathiriya 31
  • 32. Prepared by: Prof. Khushali B Kathiriya 32
  • 34. Prepared by: Prof. Khushali B Kathiriya 34
  • 35. Prepared by: Prof. Khushali B Kathiriya 35
  • 37. Prepared by: Prof. Khushali B Kathiriya 37
  • 38. Prepared by: Prof. Khushali B Kathiriya 38
  • 39. Prepared by: Prof. Khushali B Kathiriya 39
  • 40. Prepared by: Prof. Khushali B Kathiriya 40
  • 42. (Most IMP) Prepared by: Prof. Khushali B Kathiriya 42
  • 43. Prepared by: Prof. Khushali B Kathiriya 43
  • 45. Prepared by: Prof. Khushali B Kathiriya 45
  • 47. Prepared by: Prof. Khushali B Kathiriya 47
  • 48. Prepared by: Prof. Khushali B Kathiriya 48
  • 49. Prepared by: Prof. Khushali B Kathiriya 49
  • 51. Prepared by: Prof. Khushali B Kathiriya 51
  • 52. Prepared by: Prof. Khushali B Kathiriya 52
  • 53. Prepared by: Prof. Khushali B Kathiriya 53
  • 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
  • 58. Prepared by: Prof. Khushali B Kathiriya 58
  • 59. 59 Prepared by: Prof. Khushali B Kathiriya
  • 60. Prepared by: Prof. Khushali B Kathiriya 60
  • 61. Prepared by: Prof. Khushali B Kathiriya 61
  • 62. ▪ Consider elements for bubble sort 1. 70, 30, 20, 50, 60, 10, 40 2. DESIGN Prepared by: Prof. Khushali B Kathiriya 62
  • 64. Prepared by: Prof. Khushali B Kathiriya 64
  • 65. Prepared by: Prof. Khushali B Kathiriya 65
  • 66. Prepared by: Prof. Khushali B Kathiriya 66
  • 67. 67 Prepared by: Prof. Khushali B Kathiriya
  • 68. Prepared by: Prof. Khushali B Kathiriya 68
  • 69. Prepared by: Prof. Khushali B Kathiriya 69
  • 70. Prepared by: Prof. Khushali B Kathiriya 70
  • 71. ▪ Consider elements for selection sort 1. 70, 30, 20, 50, 60, 10, 40 2. ANALYSIS Prepared by: Prof. Khushali B Kathiriya 71
  • 73. Prepared by: Prof. Khushali B Kathiriya 73
  • 74. Prepared by: Prof. Khushali B Kathiriya 74
  • 75. 75 Prepared by: Prof. Khushali B Kathiriya
  • 76. 76 Prepared by: Prof. Khushali B Kathiriya
  • 77. ▪ Consider elements for insertion sort 1. 70, 30, 20, 50, 60, 10, 40 Prepared by: Prof. Khushali B Kathiriya 77
  • 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
  • 82. Prepared by: Prof. Khushali B Kathiriya 82 61 109 149 111 34 2 24 119 122 125 27 145 ▪ K= 6
  • 83. Prepared by: Prof. Khushali B Kathiriya 83 61 109 149 111 34 2 24 119 122 125 27 145 ▪ K= 6 24 109 149 111 34 2 61 119 122 125 27 145
  • 84. ▪ K=6 Prepared by: Prof. Khushali B Kathiriya 84 24 109 149 111 34 2 62 119 122 125 27 145 24 109 149 111 34 2 61 119 122 125 27 145
  • 85. ▪ K=6 Prepared by: Prof. Khushali B Kathiriya 85 24 109 149 111 34 2 62 119 122 125 27 145 24 109 122 111 34 2 61 119 149 125 27 145
  • 86. ▪ K=6 Prepared by: Prof. Khushali B Kathiriya 86 24 109 149 111 34 2 62 119 122 125 27 145 24 109 122 111 34 2 61 119 149 125 27 145
  • 87. ▪ K=6 Prepared by: Prof. Khushali B Kathiriya 87 24 109 149 111 34 2 62 119 122 125 27 145 24 109 122 111 27 2 61 119 149 125 34 145
  • 88. ▪ K=6 Prepared by: Prof. Khushali B Kathiriya 88 24 109 149 111 34 2 62 119 122 125 27 145 24 109 122 111 27 2 61 119 149 125 34 145
  • 89. ▪ K=K/2 = 6/2 = 3 Prepared by: Prof. Khushali B Kathiriya 89 24 109 122 111 27 2 61 119 149 125 34 145 24 109 122 111 27 2 61 119 149 125 34 145
  • 90. ▪ K=K/2 = 6/2 = 3 Prepared by: Prof. Khushali B Kathiriya 90 24 109 122 111 27 2 61 119 149 125 34 145 24 27 122 111 109 2 61 119 149 125 34 145
  • 91. ▪ K=K/2 = 6/2 = 3 Prepared by: Prof. Khushali B Kathiriya 91 24 109 122 111 27 2 61 119 149 125 34 145 24 27 2 111 109 122 61 119 149 125 34 145
  • 92. ▪ K=K/2 = 6/2 = 3 Prepared by: Prof. Khushali B Kathiriya 92 24 109 2 111 109 122 61 119 149 125 34 145 24 27 2 61 109 122 111 119 149 125 34 145
  • 93. ▪ K=K/2 = 6/2 = 3 Prepared by: Prof. Khushali B Kathiriya 93 24 27 2 61 109 122 111 119 149 125 34 145 24 27 2 61 109 122 111 119 149 125 34 145
  • 94. ▪ K=K/2 = 6/2 = 3 Prepared by: Prof. Khushali B Kathiriya 94 24 27 2 61 109 122 111 119 149 125 34 145 24 27 2 61 109 122 111 119 149 125 34 145
  • 95. ▪ K=K/2 = 6/2 = 3 Prepared by: Prof. Khushali B Kathiriya 95 24 27 2 61 109 122 111 119 149 125 34 145 24 27 2 61 109 122 111 119 149 125 34 145
  • 96. ▪ K=K/2 = 6/2 = 3 Prepared by: Prof. Khushali B Kathiriya 96 24 27 2 61 109 122 111 119 149 125 34 145 24 27 2 61 109 122 111 34 149 125 119 145
  • 97. ▪ K=K/2 = 6/2 = 3 Prepared by: Prof. Khushali B Kathiriya 97 24 27 2 61 109 122 111 34 149 125 119 145 24 27 2 61 109 122 111 34 145 125 119 149
  • 98. ▪ K=K/2 = 6/2 = 3 Prepared by: Prof. Khushali B Kathiriya 98 24 27 2 61 109 122 111 34 145 125 119 149 24 27 2 61 34 122 111 109 145 125 119 149
  • 99. ▪ K=K/2 =3 /2 = 1.5 = 1 Prepared by: Prof. Khushali B Kathiriya 99 24 27 2 61 34 122 111 109 145 125 119 149 24 27 2 61 34 122 111 109 145 125 119 149
  • 100. ▪ K=K/2 =3 /2 = 1.5 = 1 Prepared by: Prof. Khushali B Kathiriya 100 24 2 27 61 34 122 111 109 145 125 119 149 24 2 27 61 34 122 111 109 145 125 119 149 2 24 27 61 34 122 111 109 145 125 119 149
  • 101. ▪ K=K/2 =3 /2 = 1.5 = 1 Prepared by: Prof. Khushali B Kathiriya 101 2 24 27 61 34 122 111 109 145 125 119 149 2 24 27 61 34 122 111 109 145 125 119 149
  • 102. ▪ K=K/2 =3 /2 = 1.5 = 1 Prepared by: Prof. Khushali B Kathiriya 102 2 24 27 61 34 122 111 109 145 125 119 149 2 24 27 34 61 122 111 109 145 125 119 149
  • 103. ▪ K=K/2 =3 /2 = 1.5 = 1 Prepared by: Prof. Khushali B Kathiriya 103 2 24 27 34 61 122 111 109 145 125 119 149 2 24 27 34 61 122 111 109 145 125 119 149
  • 104. ▪ K=K/2 =3 /2 = 1.5 = 1 Prepared by: Prof. Khushali B Kathiriya 104 2 24 27 34 61 122 111 109 145 125 119 149 2 24 27 34 61 111 122 109 145 125 119 149
  • 105. ▪ K=K/2 =3 /2 = 1.5 = 1 Prepared by: Prof. Khushali B Kathiriya 105 2 24 27 34 61 111 122 109 145 125 119 149 2 24 27 34 61 111 109 122 145 125 119 149 2 24 27 34 61 109 111 122 145 125 119 149
  • 106. ▪ K=K/2 =3 /2 = 1.5 = 1 Prepared by: Prof. Khushali B Kathiriya 106 2 24 27 34 61 109 111 122 145 125 119 149 2 24 27 34 61 109 111 122 145 125 119 149
  • 107. ▪ K=K/2 =3 /2 = 1.5 = 1 Prepared by: Prof. Khushali B Kathiriya 107 2 24 27 34 61 109 111 122 145 125 119 149 2 24 27 34 61 109 111 122 125 145 119 149
  • 108. ▪ K=K/2 =3 /2 = 1.5 = 1 Prepared by: Prof. Khushali B Kathiriya 108 2 24 27 34 61 109 111 122 125 145 119 149 2 24 27 34 61 109 111 122 125 119 145 149 2 24 27 34 61 109 111 122 119 125 145 149 2 24 27 34 61 109 111 119 122 125 145 149
  • 109. ▪ Consider elements for shell sort 1. 70, 30, 20, 50, 60, 10, 40 Prepared by: Prof. Khushali B Kathiriya 109
  • 112. ▪ Array = [14, 12 , 9, 8, 7, 10 , 18] ▪ Step:1 ▪ Heap Construction Prepared by: Prof. Khushali B Kathiriya 112 14 9 12 7 8 10 18
  • 113. ▪ Step: 2 ▪ Start Heaping from bottom and Swap with maximum no. Prepared by: Prof. Khushali B Kathiriya 113 14 18 12 7 8 10 9 14 9 12 7 8 10 18 Array = [14, 12 , 18, 8, 7, 10 , 9] Swap with maximum no.
  • 114. ▪ Step:3 ▪ Heaping from top and swapping with maximum no. Prepared by: Prof. Khushali B Kathiriya 114 14 18 12 7 8 10 9 18 14 12 7 8 10 9 Array = [18, 12 , 14, 8, 7, 10 , 9]
  • 115. ▪ Step: 4 ▪ Swapping with last node and then delete maximum number Prepared by: Prof. Khushali B Kathiriya 115 18 14 12 7 8 10 9 9 14 12 7 8 10 18 Array = [9, 12 , 14, 8, 7, 10 , 18]
  • 116. ▪ Step: 5 ▪ Heaping with maximum number Prepared by: Prof. Khushali B Kathiriya 116 9 14 12 7 8 10 14 9 12 7 8 10 Array = [14, 12 , 9, 8, 7, 10 | 18]
  • 117. ▪ Step: 6 Prepared by: Prof. Khushali B Kathiriya 117 14 9 12 7 8 10 10 9 12 7 8 14 Array = [10, 12 , 9, 8, 7| 14 , 18]
  • 118. ▪ Step: 7 Prepared by: Prof. Khushali B Kathiriya 118 10 9 12 7 8 12 9 10 7 8 Array = [12, 10 , 9, 8, 7 | 14, 18]
  • 119. ▪ Step: 8 Prepared by: Prof. Khushali B Kathiriya 119 7 9 10 12 8 7 9 10 8 Array = [7, 10 , 9, 8 |12, 14, 18]
  • 120. ▪ Step : 9 Prepared by: Prof. Khushali B Kathiriya 120 7 9 10 8 10 9 7 8 10 9 8 7 Array = [10 , 8, 9, 7 |12, 14, 18]
  • 121. ▪ Step: 10 Prepared by: Prof. Khushali B Kathiriya 121 7 9 8 10 7 9 8 Array = [ 7, 8, 9 |10, 12, 14, 18]
  • 122. ▪ Step: 11 Prepared by: Prof. Khushali B Kathiriya 122 7 9 8 9 7 8 7 9 8 7 8 Array = [ 7, 8 |9, 10, 12, 14, 18]
  • 123. ▪ Step: 12 Prepared by: Prof. Khushali B Kathiriya 123 7 8 7 Array = [ 7 |8, 9, 10, 12, 14, 18] Array = [ 7, 8, 9, 10, 12, 14, 18] : Sorted Array
  • 124. ▪ Consider elements for heap sort 1. 70, 30, 20, 50, 60, 10, 40 Prepared by: Prof. Khushali B Kathiriya 124
  • 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
  • 128. 0 1 2 3 4 5 6 7 8 9 Prepared by: Prof. Khushali B Kathiriya 128 ▪ Step: 1 ▪ Generate bucket from 0 to 1
  • 129. ▪ Step: 2 ▪ Fill bucket using given array ▪ Array= [0.56, 0.12, 0.84, 0.56, 0.28, 0, -0.13, 0.47, 0.94, 0.31, 0.12, -0.2] Prepared by: Prof. Khushali B Kathiriya 129 0 1 2 3 4 5 6 7 8 9 0.56 0.12 0.84 0.56 0.28 0 -0.13 0.47 0.94 0.31 0.12 -0.2
  • 130. ▪ Step: 3 ▪ Sort each non empty bucket Prepared by: Prof. Khushali B Kathiriya 130 130 0 1 2 3 4 5 6 7 8 9 0.56 0.12 0.84 0.56 0.28 0 -0.13 0.47 0.94 0.31 0.12 -0.2 0 1 2 3 4 5 6 7 8 9 -0.2,-0.13,0 0.12, 0.12 0.28 0.47 0.31 0.56 , 0.56 0.84 0.94
  • 131. ▪ Step: 4 ▪ Put all elements bucket vise in one sequence Prepared by: Prof. Khushali B Kathiriya 131 131 0 1 2 3 4 5 6 7 8 9 -0.2,-0.13,0 0.12, 0.12 0.28 0.47 0.31 0.56 , 0.56 0.84 0.94 Sorted array is: [-0.2,-0.13,0,0.12,0.12,0.28, 0.31, 0.47, 0.56, 0.56, 0.84, 0.94 ]
  • 132. ▪ Consider elements for Bucket sort 1. 121,235,456,123,674,01,90,10,56,45,87,899 Prepared by: Prof. Khushali B Kathiriya 132
  • 134. Prepared by: Prof. Khushali B Kathiriya 134
  • 135. Prepared by: Prof. Khushali B Kathiriya 135
  • 136. Prepared by: Prof. Khushali B Kathiriya 136
  • 137. Prepared by: Prof. Khushali B Kathiriya 137
  • 138. Prepared by: Prof. Khushali B Kathiriya 138
  • 140. 140 Prepared by: Prof. Khushali B Kathiriya
  • 141. Prepared by: Prof. Khushali B Kathiriya 141
  • 142. Prepared by: Prof. Khushali B Kathiriya 142
  • 143. Prepared by: Prof. Khushali B Kathiriya 143
  • 144. Prepared by: Prof. Khushali B Kathiriya 144
  • 145. Prepared by: Prof. Khushali B Kathiriya 145
  • 146. Prepared by: Prof. Khushali B Kathiriya 146
  • 147. Prepared by: Prof. Khushali B Kathiriya 147
  • 148. Prepared by: Prof. Khushali B Kathiriya 148
  • 149. Prepared by: Prof. Khushali B Kathiriya 149
  • 150. Prepared by: Prof. Khushali B Kathiriya 150