Unit 2
Unit 2
Unit-2
Analysis of
Algorithms
3
Analysis of Algorithm
What is the analysis of an algorithm?
• Analyzing an algorithm means calculating/predicting the resources that
the algorithm requires.
• Two most important resources are computing time (time complexity) and
storage space (space complexity).
Why analysis is required?
• By analyzing some of the candidate algorithms for a problem, the most
efficient one can be easily identified.
5
Analysis of Algorithms 5 Darshan Institute of Engineering & Technology
Problem & Instance
Instance: An Instance of a problem consists of the input needed to
compute the solution to the problem.
Example:
Problem: to multiply two positive numbers
Instance:
Instance size: Any integer (generally ) that in some way measures
the number of components in an instance.
Examples:
1. Sorting problem: Instance size is number of elements to be sorted.
2. Graph problem: Instance size is number of nodes or edges or both.
Comparing value of ith index with the given element one by one, until we get the required
element or end of the array
Step 1: i=0 Step 3: i=2
i i
2 ≠1 3≠1
Step 2: i=1 Step 4: i=3
𝟏
i i
9≠1 Element found at ith index, i=3
Analysis of Algorithms 11 Darshan Institute of Engineering & Technology
Linear Search Algorithm
# Input : Array A, element
# Output : First index of element in A or
-1 if not found
Algorithm: Linear_Search
for i = 1 to last index of A:
if A[i] equals element:
return i
return -1
2
2 9 3 1 8
Best Case
Average Case
15
Book Finder
Suppose, you are writing a program to
find a book from the shelf.
For any required book, it will start
checking books one by one from the
bottom.
If you wanted Harry Potter 3, it would
only take 3 actions (or tries) because it’s
the third book in the sequence.
If Harry Potter 7 — it’s the last book so
it would have to check all 7 books.
What if there are total 10 books? How
about 10,00,000 books? It would take 1
million tries.
𝑓 (𝑛)=Ω(𝑔 (𝑛))
= { : there exist positive constants and such that
for all}
𝑓 (𝑛)=𝜃(𝑔(𝑛))
= { : there exist positive constants , and such that for all}
𝑓 (𝑛 ) ≥ 𝑔 (𝑛 ) 𝑓 (𝑛 )=Ω(𝑔(𝑛)) 𝑓 (𝑛 ) ≤ 𝑔 (𝑛 ) 𝑓 (𝑛 )=O(𝑔(𝑛))
1 1 1 1 1 1
2 4 2 2 2 4
3 9 3 3 3 9
4 16 4 4 4 16
5 25 5 5 5 25
1 1 2
2 4 4
3 9 8
4 16 16
5
Here for ,
25 32
6 36 64
7 49 128
4 2 8 16 64 16 24
16 4 64 256 4096 65536 2.09 x 1013
64 6 384 4096 262144 1.84 × 1019 1.26 x 1029
256 8 2048 65536 16777216 1.15 × 1077
1024 10 10240 1048576 1.07 × 109 1.79 × 10308
4096 12 49152 16777216 6.87 × 1010 101233
1. - is
The low order terms in a function are relatively insignificant for large
𝜃 (𝑛3)
Express in terms of O notation.
𝑂(𝑛log 𝑛)
35
Sum of n Elements of Array
Algorithm
//Input: int A[n], array of n integers
//Output: Sum of all numbers in array A
int Sum(int A[], int n)
{
n+1
int s=0;
for (int i=0; i<n; i++)
1 s = s + A[i];
return s; Total:
} The time complexity of
the algorithm is :
n
Analysis of Algorithms 36 Darshan Institute of Engineering & Technology
Running Time of Algorithm
Estimated running time for different values of :
The time complexity of the
algorithm is :
𝑛= 10 steps
𝑛=100 steps
𝑛=1000 steps
𝑛=10000 steps
𝑛= 10 steps
𝑛=100 steps
𝑛=1000 steps
𝑛=10000 steps
𝜃 ( 𝑛2 )
𝜃 (𝑛 )
𝑡 𝑛 =𝜃 𝑛 )
( 3
( )
𝜃 (1)
Example 5: printf(“sum is now %d”,)
𝑡 ( 𝑛 )=𝜃 ( 𝑛 6 )
Analysis of Algorithms 40 Darshan Institute of Engineering & Technology
Home Work
Example 7: Example 8:
sum = 0
while ()
While
sum = sum +
Example 9:
42
Applications of Sorting
1. Phone Bill: the calls made are date wise sorted.
2. Bank statement or Credit card Bill: transactions made are date
wise sorted.
3. Filling forms online: “select country” drop down box will have
the name of countries sorted in Alphabetical order.
4. Online shopping: the items can be sorted price wise, date wise or
relevance wise.
5. Files or folders on your desktop are sorted date wise.
Pass 1 :
34
45 34 34 34
swap
34
45 45 45 45
56 56 56
23 23
23 23 23
56 swap 56
12
swap
12 12 12 12
56
34 34 34 23
34 23 12
23
swap
swap
45 45
23 23 23
34 34
12 12
23
swap
swap
23 23
45 45
12 12 12
34 34
swap
12 12 12
45 45 45 45
56 56 56 56 56 56
Algorithm: Bubble_Sort(A)
for i ← 1 to n do 𝜽 (𝒏)
for j ← 1 to n-i do
if A[j] > A[j+1] then
thenswap(A[j],A[j+1])
temp ←← A[j]
temp A[j] 𝜽𝒏 )
( 𝟐
A[j] ←← A[j+1]
A[j] A[j+1]
A[j+1] ←← temp
A[j+1] temp
𝜽 (𝒏 )
𝟐
The time complexity of bubble sort is
𝑻 ( 𝒏 )=𝜽 ( 𝒏𝟐 )
Analysis of Algorithms 48 Darshan Institute of Engineering & Technology
Bubble Sort: Best Case Analysis
Best case time complexity = Pass 1 : i = 1
int flag=1; 12 j = 1
for(i = 1; i < n; i++) 23 j = 2
for(j = 1; j < n-i; j++)
34
j = 3
45
if(A[j] > A[j+1]) Condition never 59 j = 4
becomes true
flag=0;
swap(A[j],A[j+1])
if(flag == 1)
cout<<"already sorted"<<endl
break;
Step 1 :
Unsorted Array
5 1 12 -5 16 2 12 14
1 2 3 4 5 6 7 8
Step 2 :
Unsorted Array (elements 2 to 8) Minj = 1, Minx = 5
Find the smallest value
-5
5 1 12 -5
5 16 2 12 14 from the entire Unsorted
1 2 3 4 5 6 7 8 array
Swap
Index = 4, value = -5
Step 4 :
Unsorted Array
(elements 4 to 8) Minj = 3, Minx = 12
Swap
Step 6 :
Unsorted Array
(elements 6 to 8)
Minj = 5, Minx = 16
-5 1 2 5 12
16 16
12 12 14 Find min value from
1 2 3 4 5 6 7 8 Remaining unsorted array
Index = 6, value = 12
Swap
Analysis of Algorithms 53 Darshan Institute of Engineering & Technology
Selection Sort
Step 7 :
Unsorted Array Minj = 6, Minx = 16
(elements 7 to 8)
Find min value from
-5 1 2 5 12 12
16 16
12 14 Remaining unsorted array
Index = 7, value = 12
1 2 3 4 5 6 7 8
Swap
Step 8 :
Unsorted Array
(element 8)
Minj = 7, Minx = 16
-5 1 2 5 12 12 14
16 16
14 Find min value from
1 2 3 4 5 6 7 8 Remaining unsorted array
Index = 8, value = 14
Swap
Analysis of Algorithms 54 Darshan Institute of Engineering & Technology
Selection Sort
Selection sort divides the array or list into two parts,
1. The sorted part at the left end
2. and the unsorted part at the right end.
Initially, the sorted part is empty and the unsorted part is the
entire list.
The smallest element is selected from the unsorted array and
swapped with the leftmost element, and that element becomes a
part of the sorted array.
Then it finds the second smallest element and exchanges it with
the element in the second leftmost position.
This process continues until the entire array is sorted.
45
45 34 56 23 12 45
12 34 56 23 12
45
1 2 3 4 5 1 2 3 4 5
45
12 34
23 56
34 23
34
45
56 45
56
Analysis of Algorithms 58 Darshan Institute of Engineering & Technology
Selection Sort - Analysis
# Input: Array A
# Output: Sorted array A
Algorithm: Selection_Sort(A)
for i ← 1 to n-1 do
minj ← i;
minx ← A[i]; 𝜃 (𝑛 )
for j ← i + 1 to n do
if A[j] < minx then
minj ← j;
minx ← A[j];
𝜃 (𝑛 )2
A[minj] ← A[i];
A[i] ← minx;
𝜽 (𝒏 )
𝟐
The time complexity of Selection sort is
Analysis of Algorithms 60 Darshan Institute of Engineering & Technology
Selection Sort - Analysis
Algorithm: Selection_Sort(A) Cost Times
for i ← 1 to n-1 do
minj ← i ; minx ← A[i];
for j ← i + 1 to n do
A[minj] = A[i];
A[i] = minx;
Step 1 :
Unsorted Array
5 1 12 -5 16 2 12 14
1 2 3 4 5 6 7 8
Step 2 :
Shift down
Step 4 :
Step 6 :
Shift down
Step 8 :
Shift down
Analysis of Algorithms 66 Darshan Institute of Engineering & Technology
Insertion Sort - Algorithm
# Input: Array T
# Output: Sorted array T
Algorithm: Insertion_Sort(T[1,…,n])
for i ← 2 to n do
x ← T[i];
j ← i – 1;
while x < T[j] and j > 0 do
T[j+1] ← T[j];
j ← j – 1;
T[j+1] ← x;
𝜽 (𝒏 )
𝟐
The time complexity of Insertion sort is
Analysis of Algorithms 68 Darshan Institute of Engineering & Technology
Insertion Sort – Best Case Analysis
Pass 1 :
# Input: Array T
12
# Output: Sorted array T
23 i=2 T[j]=12
Algorithm: Insertion_Sort(T[1,…,n]) 34 i=3 T[j]=23
for i ← 2 to n do 45 i=4 T[j]=34
x ← T[i]; 59 i=5 T[j]=45
j ← i - 1;
while x < T[j] and j > 0 do
T[j+1] ← T[j];
j ← j – 1;
T[j+1] ← x;
The best case time complexity of Insertion sort is 𝜽 (𝒏)
Analysis of Algorithms 69 Darshan Institute of Engineering & Technology
Insertion Sort - Analysis
Algorithm: Insertion_Sort(T) Cost Times
for i←2 to n do
x←T[i]
j←i-1
while x<T[j] and j>0 do
T[j+1] ← T[j]
j = j - 1
T[j+1] = x;
+ c4 +c5+ c6 c4 c5 c6+
=(-C4- C5- C6(C1 +C2 +C3 +C7 + C4+ C5+ C6)- 1(C2 +C3 +C7)
=
The time complexity of Insertion sort algorithm is
Step 2 :
Step 6 :
39 93 6 5 23 14
23 9
14 6 5 3 14
Step 3 :
39 36 63 5 23 14
Step 4 :
39 36 35 53 23 14
a a
b c b c
d e f d e f
9 9
6 7 6 7
2 4 8 2 4 1
Heap
16
14 10
8 7 9 3
Array representation of heap
2 4 1
16 14 10 8 7 9 3 2 4 1
2 4 1
6 7 9
Analysis of Algorithms 78 Darshan Institute of Engineering & Technology
Heap Sort
1. Build the complete binary tree using given elements.
2. Create Max-heap to sort in ascending order.
3. Once the heap is created, swap the last node with the root node
and delete the last node from the heap.
4. Repeat step 2 and 3 until the heap is empty.
4 10 3 5 1
10
4 10 3 5 1
10 3
4 10 3 5 1
10 3
4 10 3 5 1
Now, a binary 4
tree is created
and we have to
10 3
convert it into a
Heap.
5 1
4 10 3 5 1
4 10 3 5 1
4 10 3 5 1
10 4 3 5 1
10 5 3 4 1
10 5 3 4 1
1 5 3 4 10
1 5 3 4 10
5 1 3 4 10
5 4 3 1 10
1 4 3 5 10
1 4 3 5 10
4 1 3 5 10
3 1 4 5 10
3 1 4 5 10
1 3 4 5 10
19 19
7 16 14 17
1 14 17 1 7 16
19 14 17 1 7 16 16 14 17 1 7 19
19 16
Swap &
remove
14 17 14 17
the last
element
1 7 16 1 7
Create Max-heap
17 14 16 1 7 19 7 14 16 1 17 19
17 7
Swap &
remove
14 16 14 16
the last
element
1 7 1
Create Max-heap
16 14 7 1 17 19 1 14 7 16 17 19
16 1
Swap &
remove
14 7 14 7
the last
element
Create Max-heap
14 1 7 16 17 19 7 1 14 16 17 19
14 Swap &
7
remove
the last
element
1 7 1
Already a Max-heap
Swap & remove the last
element
1 7 14 16 17 19 1 7 14 16 17 19
4 1 7 2 9 3 4 9 7 2 1 3 9 4 7 2 1 3
i=3 1 i=2 1 i=1 1
4 4 94
2 3 2 3 2 3
1 37 19 7 49 7
4 5 6 4 5 6 4 5 6
2 9 3
7 2 1
9 3 2 1 3
4 5
BUILD-MAX-HEAP(A) 2 1
O ( log𝑛 )
3. Sort the given elements with Heap Sort Method: 20, 50, 30, 75,
90, 60, 25, 10, 40.
80 93 60 12 42 30 68 85 10
10 93 60 12 42 30 68 85 80
10 30 60 12 42 93 68 85 80
10 30 60 12 42 93 68 85 80
10 30 60 12 42 93 68 85 80
10 30 60 12 42 93 68 85 80
10 30 42 12 60 93 68 85 80
10 12 42 30 60 85 68 93 80
10 12 42 30 60 85 68 93 80
10 12 30 42 60 68 80 85 93
45 96 29 30 27 12 39 61 91
Algorithm: RADIX-SORT(A, d)
for i ← 1 to d
do use a stable sort to sort array A
on digit i
Sort following elements in ascending order using radix sort.
363, 729, 329, 873, 691, 521, 435, 297
Sort on column 1
Sort on column 2
45 96 29 30 27 12 39 61 91
45 96 29 30 27 12 39 61 91
29
27 39 91
96
12 27
29 30 45 61 96
91
0 1 2 3 4 5 6 7 8 9
12 27 29 30 39 45 61 91 96
Index
Elements 3 6 4 1 3 4 1 4 2
+ +
Step 4 In array from index add the value with previous element
Index
Elements 2 3 5 8 8 9
Input Array A 3 6 4 1 3 4 1 4 2
Temporary Array C 2 32 5 87 8 9
Output Array B 1 1 2 3 3 4 4 4 6
Input Array A 3 6 4 1 3 4 1 4 2
Temporary Array C 21 3 5 68 8 9
2 7
Output Array B 1 1 2 3 3 4 4 4 6
Input Array A 3 6 4 1 3 4 1 4 2
Temporary Array C 21 3 53
0 2 4 658 8 98
Output Array B 1 1 2 3 3 4 4 4 6
The final array is sorted
Analysis of Algorithms 133 Darshan Institute of Engineering & Technology
Counting Sort - Procedure
Counting sort assumes that each of the input elements is an
integer in the range , for some integer .
When , the counting sort runs in time.
The basic idea of counting sort is to determine, for each input
element , the number of elements less than .
This information can be used to place element directly into its
position in the output array.
137
Amortized Analysis
Amortized analysis considers not just one operation, but a
sequence of operations on a given data structure.
The time required to perform a sequence of data structure
operations is averaged over all operations performed.
Amortized analysis can be used to show that the average cost of
an operation is small even though a single operation might be
expensive.
Counter value [7] [6] [5] [4] [3] [2] [1] [0] Incre. cost Total cost
0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 1 1 1
+
2 0 0 0 0 0 0 1 0 2 3
3 0 0 0 0 0 0 1 1 1 4
4 0 0 0 0 0 1 0 0 3 7
5 0 0 0 0 0 1 0 1 1 8
6 0 0 0 0 0 1 1 0 2 10
7 0 0 0 0 0 1 1 1 1 11
8 0 0 0 0 1 0 0 0 4 15
9 0 0 0 0 1 0 0 1 1 16
10 0 0 0 0 1 0 1 0 2 18
11 0 0 0 0 1 0 1 1 1 19
15
Analysis of Algorithms 143 Darshan Institute of Engineering & Technology
1. Aggregate Method
Therefore, the total number of flips in the sequence is,
K-1
Counter value [7] [6] [5] [4] [3] [2] [1] [0] Incre. cost Total cost
0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 1 1 1
2 0 0 0 0 0 0 1 0 2 3
3 0 0 0 0 0 0 1 1 1 4
4 0 0 0 0 0 1 0 0 3 7
5 0 0 0 0 0 1 0 1 1 8
6 0 0 0 0 0 1 1 0 2 10
7 0 0 0 0 0 1 1 1 1 11
8 0 0 0 0 1 0 0 0 4 15
9 0 0 0 0 1 0 0 1 1 16
10 0 0 0 0 1 0 1 0 2 18
11 0 0 0 0 1 0 1 1 1 19