Professional Documents
Culture Documents
Unit-2: Analysis of Algorithm
Unit-2: Analysis of Algorithm
GTU # 3150703
Unit-2:
Analysis of Algorithm
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=1 Step 3: i=3
i i
𝟐≠𝟏 𝟑≠𝟏
Step 2: i=2 Step 4: i=4
i i
𝟗≠𝟏 Element found at ith index, i=4
Dr. Gopi Sanghani #3150703 (ADA) Unit 2 – Analysis of Algorithm 10
Linear Search - Algorithm
# Input : Array A, element x
# Output : First index of element x in A or -1 if not found
Algorithm: Linear_Search
for i = 1 to last index of A
if A[i] equals element x
return i
return -1
Worst Case
Search for
Best Case
Average Case
Worst Case
Search for
Best Case
Average Case
𝟓 𝟗 𝟏𝟐 𝟐𝟑 𝟑𝟐 𝟒𝟏
𝟗 𝟓 𝟏𝟐 𝟑𝟐 𝟐𝟑 𝟒𝟏 𝟓 𝟗 𝟏𝟐 𝟐𝟑 𝟑𝟐 𝟒𝟏
𝟒𝟏 𝟑𝟐 𝟐𝟑 𝟏𝟐 𝟗 𝟓
Case 1: Numbers are already Case 2: Numbers are Case 3: Numbers are initially
in required order, i.e., randomly arranged initially. arranged in Descending order
Ascending order Some numbers will change so, all numbers will change
No change is required their position their position
𝟓 𝟗 𝟏𝟐 𝟐𝟑 𝟑𝟐 𝟒𝟏
𝟗 𝟓 𝟏𝟐 𝟑𝟐 𝟐𝟑 𝟒𝟏 𝟓 𝟗 𝟏𝟐 𝟐𝟑 𝟑𝟐 𝟒𝟏
𝟒𝟏 𝟑𝟐 𝟐𝟑 𝟏𝟐 𝟗 𝟓
implies:
𝒄. 𝒈(𝒏)
𝒇(𝒏)
𝒏
𝒏𝟎
θ(𝑔(𝑛)) = {𝑓(𝑛) : there exist positive constants c , c and n0 such that 0 ≤ 𝑐 𝑔 𝑛 ≤ 𝑓 𝑛 ≤ 𝑐 𝑔 𝑛 for all 𝑛0 ≤ 𝑛}
𝒄𝟏 . 𝒈(𝒏)
implies:
𝒏
𝒏𝟎
Ω O
𝒏 𝒇(𝒏) = 𝒏𝟐 𝒈(𝒏) = 𝟐𝒏
1 1 2 𝑓(𝑛) < 𝑔(𝑛)
2 4 4 𝑓(𝑛) = 𝑔(𝑛)
3 9 8 𝑓(𝑛) > 𝑔(𝑛)
4 16 16 𝑓(𝑛) = 𝑔(𝑛)
Here for ,
5 25 32 𝑓(𝑛) < 𝑔(𝑛)
6 36 64 𝑓(𝑛) < 𝑔(𝑛)
0
7 49 128 𝑓(𝑛) < 𝑔(𝑛)
g (n)=n2+1
Value of function
f(n)=30n+8
In general, any function is faster-
growing than any function.
Base value 𝑛
Increasing n
𝟐 𝟑 𝒏
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. 𝑛 𝑛 2 2 3 2
2. 8 𝑛 ( )
𝟒
𝟑
- is 𝟑
The low order terms in a function are relatively insignificant for large
𝒏 = 𝟏𝟎 23 steps
As grows, the number of steps grow in linear proportion to for the given algorithm Sum.
The dominating term in the function of time complexity is : As gets large, the becomes
insignificant.
The time is linear in proportion to .
𝟏
Statement is executed once only 𝟐
So, The execution time is some
constant 𝟑
Analysis
Example 2:
2 2
2
𝟏
2
𝟐 𝟐
𝟑
printf(“sum is now %d”, )
Example 5:
𝟐
𝟐
Pass 1 :
34
45 34 34 34
swap
34
45 45 45 45
56 56 56
23 23
swap
23 23 23
56 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-1 do
for j ← 1 to n-i do
if A[j] > A[j+1] then
temp ← A[j] A[j+1])
swap(A[j], 𝟐
A[j] ← A[j+1]
A[j+1] ← temp
Step 1 :
Unsorted Array
5 1 12 -5 16 2 12 14
1 2 3 4 5 6 7 8
Step 2 :
Minj denotes the current index and Minx is the value stored
Unsorted Array (elements 2 to 8) at current index.
So, Minj = 1, Minx = 5
-5
5 1 12 -5
5 16 2 12 14 Assume that currently Minx is the smallest value.
Now find the smallest value from the remaining entire
1 2 3 4 5 6 7 8
Unsorted array.
Swap Index = 4, value = -5
Dr. Gopi Sanghani #3150703 (ADA) Unit 2 – Analysis of Algorithm 48
Selection Sort – Example 1
Step 3 :
Unsorted Array (elements 3 to 8) Now Minj = 2, Minx = 1
Find min value from remaining
-5 1 12 5 16 2 12 14 unsorted array
1 2 3 4 5 6 7 8
Index = 2, value = 1
Swap
Swap
Dr. Gopi Sanghani #3150703 (ADA) Unit 2 – Analysis of Algorithm 50
Selection Sort – Example 1
Step 7 :
Unsorted Array Now Minj = 6, Minx = 16
(elements 7 to 8) Find min value from remaining
unsorted array
-5 1 2 5 12 12
16 16
12 14
1 2 3 4 5 6 7 8 Index = 7, value = 12
Swap
Step 8 :
Unsorted Array Minj = 7, Minx = 16
(element 8) Find min value from remaining
unsorted array
-5 1 2 5 12 12 14
16 16
14
Index = 8, value = 14
1 2 3 4 5 6 7 8
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];
A[minj] ← A[i];
A[i] ← minx;
45 34 56 23 12
1 2 3 4 5
45 34 56 23 12 45
12 34 56 23 45
12
1 2 3 4 5 1 2 3 4 5
Swap
45
12 23
34 34
56 34
45
23
56 45
56
Dr. Gopi Sanghani #3150703 (ADA) Unit 2 – Analysis of Algorithm 55
Insertion Sort – Example
Sort the following elements in Ascending order
5 1 12 -5 16 2 12 14
Step 1 :
Unsorted Array
5 1 12 -5 16 2 12 14
1 2 3 4 5 6 7 8
Step 2 :
𝒋
𝒊 = 𝟐, 𝒙 = 𝟏 𝒋 = 𝒊 – 𝟏 𝒂𝒏𝒅 𝒋 > 𝟎
51 1 12 -5 16 2 12 14 while do
1 2 3 4 5 6 7 8
Shift down
Step 3 :
𝒋
𝒊 = 𝟑, 𝒙 = 𝟏𝟐 𝒋 = 𝒊 – 𝟏 𝒂𝒏𝒅 𝒋 > 𝟎
1 5 12 -5 16 2 12 14 while 𝑥 < 𝑇 𝑗 do
1 2 3 4 5 6 7 8 𝑇 𝑗+1 ←𝑇 𝑗
𝑗−−
Step 4 :
𝒊 = 𝟒, 𝒙 = −𝟓 𝒋 = 𝒊 – 𝟏 𝒂𝒏𝒅 𝒋 > 𝟎
𝒋
while 𝑥 < 𝑇 𝑗 do
𝒋
𝑇 𝑗+1 ←𝑇 𝑗
-5
1 5 12 -5 16 2 12 14 𝑗−−
1 2 3 4 5 6 7 8
Shift down
Shift down
Shift down
Step 5 :
𝒋
𝒊 = 𝟓, 𝒙 = 𝟏𝟔 𝒋 = 𝒊 – 𝟏 𝒂𝒏𝒅 𝒋 > 𝟎
-5 1 5 12 16 2 12 14 while 𝑥 < 𝑇 𝑗 do
1 2 3 4 5 6 7 8 𝑇 𝑗+1 ←𝑇 𝑗
𝑗−−
No Shift will take place
Step 6 :
𝒊 = 𝟔, 𝒙 = 𝟐 𝒋 = 𝒊 – 𝟏 𝒂𝒏𝒅 𝒋 > 𝟎
𝒋 𝒋 while 𝑥 < 𝑇 𝑗 do
𝑇 𝑗+1 ←𝑇 𝑗
-5 1 52 12 16 2 12 14 𝑗−−
1 2 3 4 5 6 7 8
Step 7 :
𝒋
𝒊 = 𝟕, 𝒙 = 𝟏𝟐 𝒋 = 𝒊 – 𝟏 𝒂𝒏𝒅 𝒋 > 𝟎
-5 1 2 5 12 12 14
12 16 while 𝑥 < 𝑇 𝑗 do
1 2 3 4 5 6 7 8 𝑇 𝑗+1 ←𝑇 𝑗
𝑗−−
Shift down
Step 8 :
𝒊 = 𝟖, 𝒙 = 𝟏𝟒 𝒋 = 𝒊 – 𝟏 𝒂𝒏𝒅 𝒋 > 𝟎
𝒋 while 𝑥 < 𝑇 𝑗 do
𝑇 𝑗+1 ←𝑇 𝑗
-5 1 2 5 12 12 14
16 14 𝑗−−
1 2 3 4 5 6 7 8
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;
a a 9 9
b c b c 6 7 6 7
d e f d e f 2 4 8 2 4 1
Binary Tree but not a Heap Complete Binary Tree - Heap Not a Heap Heap
16
14 10
2 4 1 Heap 16 14 10 8 7 9 3 2 4 1
𝟐 𝟑
For node ,
14 10 Left child node is node
Right child is node
𝟒 𝟓 𝟔 𝟕
8 7 9 3
𝟏 𝟐 𝟑 𝟒 𝟓 𝟔 𝟕 𝟖 𝟗 𝟏𝟎
𝟖 𝟗 𝟏𝟎
2 4 1 Heap 16 14 10 8 7 9 3 2 4 1
2 4 1
6 7 9
Dr. Gopi Sanghani #3150703 (ADA) Unit 2 – Analysis of Algorithm 66
Introduction to 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 3
10
1 5 3 4 1
10
5 3
Swap
1
4
51 4 3 15 10
4 3
Swap
31 13 4 5 10
3
1
Swap
1 3 4 5 10
19 14
7 16
17 1 14
7 16
17
19 19
7 16 14
7 16
17
1 14 17 1 14
7 16
17
19
16 14 17 1 7 16
19 17
16 14 17
16 1 7 19
19
16 16
17 Create Max-heap
Swap &
remove
14 17 the last 14 17
16
element
1 7 16
19 1 7
17
7 14 16 1 7
17 19 7
16 14 16
7 1 17 19
17
7 16
7 Create Max-heap
Swap &
remove
14 16 the last 14 7
16
element
1 17
7 1
16
1 14 7 1
16 17 19 1
14 14
1 7 16 17 19
16
1 1
14 Create Max-heap
Swap &
remove
14 7 the last 14
1 7
element
16
1
14
7 1 14
7 16 17 19 17 17 14 16 17 19
14
7 71 Already a Max-heap
Swap &
remove the Swap & remove the last
last element element
1 14
7 17
Step 11
1 7 14 16 17 19
Remove the
1
last element
The entire array is sorted now.
Sort the following elements in Descending order using Hear Sort Algorithm.
1. 65, 77, 5, 23, 32, 45, 99, 83, 69, 81
Algorithm: Heap_Sort(A[1,…,n])
BUILD-MAX-HEAP(A)
for i length[A] downto 2
do exchange A[1] A[i]
heap-size[A] heap-size[A] – 1
MAX-HEAPIFY(A, 1, n)
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 49
2 3 2 3 2 3
1 37 91 7 9
4 7
4 5 6 4 5 6 4 5 6
2 9 73 2 91 3 2 1 3
1
Algorithm: Heap_Sort(A[1,…,n]) 9
BUILD-MAX-HEAP(A) 2 3
for i length[A] downto 2 4 7
do exchange A[1] A[i] 4 5 6
heap-size[A] heap-size[A] – 1 2 1 3
MAX-HEAPIFY(A, 1, n)
1
Algorithm: Heap_Sort(A[1,…,n]) 7
BUILD-MAX-HEAP(A) 2 3
for i length[A] downto 2 4 3
do exchange A[1] A[i] 4 5
heap-size[A] heap-size[A] – 1 2 1
MAX-HEAPIFY(A, 1, n)
80 93 60 12 42 30 68 85 10
Step 1: Divide input array into segments, where Initial Segmenting Gap = 4 (n÷2)
1 2 3 4 5 6 7 8 9
80 93 60 12 42 30 68 85 10
Each segment is
sorted within itself
1 2 3 4 5 6 7 8 9 using insertion sort
10 93 60 12 42 30 68 85 80
80 93 60 12 42 30 68 85 10
Step 1: Divide input array into segments, where Initial Segmenting Gap = 4 (n÷2)
1 2 3 4 5 6 7 8 9
10 30 60 12 42 93 68 85 80
1 2 3 4 5 6 7 8 9
Each segment is
10 30 60 12 42 93 68 85 80 sorted within itself
using insertion sort
1 2 3 4 5 6 7 8 9
10 30 60 12 42 93 68 85 80
Dr. Gopi Sanghani #3150703 (ADA) Unit 2 – Analysis of Algorithm 91
Shell Sort - Example
Sort the following elements in ascending order using shell sort.
80 93 60 12 42 30 68 85 10
10 30 60 12 42 93 68 85 80
1 2 3 4 5 6 7 8 9 Each segment is
10 30 42 12 60 93 68 85 80 sorted within itself
using insertion sort
1 2 3 4 5 6 7 8 9
10 12 42 30 60 85 68 93 80
Dr. Gopi Sanghani #3150703 (ADA) Unit 2 – Analysis of Algorithm 92
Shell Sort - Example
Sort the following elements in ascending order using shell sort.
80 93 60 12 42 30 68 85 10
10 12 42 30 60 85 68 93 80
Each segment is
1 2 3 4 5 6 7 8 9 sorted within itself
10 12 30 42 60 68 80 85 93 using insertion sort
Algorithm: RADIX-SORT(A, d)
for i ← 1 to d
do use a stable sort to sort array A on digit i
3 6 3 6 9 1 5 2 1
7 2 9 5 2 1 7 2 9
3 2 9 3 6 3 3 2 9
8 7 3 8 7 3 4 3 5
6 9 1 4 3 5 3 6 3
5 2 1 2 9 7 8 7 3
4 3 5 7 2 9 6 9 1
2 9 7 3 2 9 2 9 7
45 96 29 30 27 12 39 61 91
45 96 29 30 27 12 39 61 91
45 96 29 30 27 12 39 61 91
29
27 39 96
91
12 27
29 30 45 61 91
96
0 1 2 3 4 5 6 7 8 9
12 27 29 30 39 45 61 91 96
3 6 4 1 3 4 1 4 2
Index 1 2 3 4 5 6 7 8 9
Elements 3 6 4 1 3 4 1 4 2
Elements 0 0 0 0 0 0
Input array 3 6 4 1 3 4 1 4 2
Index 1 2 3 4 5 6
Elements 20 10 2 3 0 1
+ +
Step 4 In array , from index 2 to add the value with previous element
Index 1 2 3 4 5 6
Elements 2 3 5 8 8 9
1 2 3 4 5 6 7 8 9
Input array 3 6 4 1 3 4 1 4 2
1 2 3 4 5 6
Temporary Array C 201 23 453 7856 8 89
1 2 3 4 5 6 7 8 9
Output Array B 1 1 2 3 3 4 4 4 6
s can be 3 0011 1
4 0100 3
𝒌 𝟏 5 0101 1
6 0110 2
𝒊
7 0111 1
𝒊 𝟎
8 1000 4
Total Flips = 15
K-1
Total time
The amortized cost of each operation is
∝
…
𝒊
𝒊 𝟏
𝟎 𝒏
Total actual cost is bounded by total amortized cost plus the net drop in potential over an entire
sequence of operations.
Dr. Gopi Sanghani #3150703 (ADA) Unit 2 – Analysis of Algorithm 115