ADA GTU Study Material Presentations Unit-2 14082021030333PM
ADA GTU Study Material Presentations Unit-2 14082021030333PM
GTU # 3150703
Unit-2:
Analysis of Algorithm
Rupesh Vaishnav
Computer Engineering Department
Darshan Institute of Engineering & Technology, Rajkot
[email protected]
9428037452
Outline
Looping
Analysis of Algorithm
The efficient algorithm
Average, Best and Worst case 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 and Heap sort
Sorting in linear time : Bucket sort, Radix sort and Counting sort
Amortized analysis
Analysis of Algorithm
Introduction
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
Rupesh Vaishnav #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
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:
𝒄 .𝒈(𝒏)
An upper bound of an algorithm defines the
maximum time required, we can always solve
𝒇 (𝒏)
the problem in at most time.
𝒏𝟎 𝒏
𝒇 (𝒏)=𝑶 (𝒈 (𝒏))
implies:
𝒇 (𝒏)
A lower bound of an algorithm defines the
𝒄 .𝒈(𝒏) minimum time required, it is not possible to
have any other algorithm (for the same
problem) whose time complexity is less than
for random input.
𝒏𝟎 𝒏
𝒇 (𝒏)= 𝜴(𝒈(𝒏))
𝒏𝟎
𝒏
𝒇 (𝒏)=𝜽 (𝒈 (𝒏))
= { : 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
𝑓 (𝑛 ) ≤ 𝑔 (𝑛 ) ⟹ 𝑓 ( 𝑛 ) =O(𝑔(𝑛))
1 1 2
2 4 4
3 9 8
4 16 16
Here for ,
5 25 32
6 36 64
7 49 128
𝒇 (𝒏)=𝑶(𝒈(𝒏))
g (n)=n +1
2
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.
For each of the following pairs of functions, either is , is , or . Determine which relationship is
correct.
1. - is
The low order terms in a function are relatively insignificant for large 𝒏
= { : there exist positive constants and such that for all } 𝐟 ( 𝐧 )=Ω(𝐠(𝐧))
𝒏=𝟏𝟎 steps
𝒏=𝟏𝟎𝟎 steps
𝒏=𝟏𝟎𝟎𝟎 steps
𝒏=𝟏𝟎𝟎𝟎𝟎 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 .
𝐜
∗ ( 𝒏+𝟏
𝟏 )
𝐜
∗ (𝟐
𝒏)
Total time is denoted as,
𝜽 ( 𝒏𝟐 )
𝜽 (𝒏)
𝒕 ( 𝒏 )=𝜽 ( 𝒏𝟑)
𝜽 ( 𝟏)
printf(“sum is now %d”,)
Example 5:
𝒕 ( 𝒏 )=𝜽 ( 𝒏 )
𝟔
34
45 swap 34 34 34
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]
swap(A[j], A[j+1]) 𝛉( 𝐧 )
𝟐
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
Rupesh Vaishnav #3150703 (ADA) Unit 2 – Analysis of Algorithm 54
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
Rupesh Vaishnav #3150703 (ADA) Unit 2 – Analysis of Algorithm 56
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 12
45 34 56 23 45
12
1 2 3 4 5 1 2 3 4 5
Swap
12
45 23
34 34
56 34
45
56
23 56
45
Rupesh Vaishnav #3150703 (ADA) Unit 2 – Analysis of Algorithm 61
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 :
Shift down
Step 3 :
𝒋 𝒊=𝟑, 𝒙=𝟏𝟐 𝒋=𝒊 – 𝟏 𝒂𝒏𝒅 𝒋 >𝟎
1 5 12 -5 16 2 12 14 while do
1 2 3 4 5 6 7 8
Step 4 :
𝒊=𝟒, 𝒙=− 𝟓 𝒋=𝒊 –𝟏 𝒂𝒏𝒅 𝒋 >𝟎
while do
𝒋 𝒋
-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
Step 6 :
𝒊=𝟔 , 𝒙=𝟐 𝒋=𝒊 – 𝟏 𝒂𝒏𝒅 𝒋 >𝟎
𝒋 𝒋 while do
-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
Shift down
Step 8 :
𝒊=𝟖, 𝒙=𝟏𝟒 𝒋=𝒊 – 𝟏 𝒂𝒏𝒅 𝒋 >𝟎
𝒋 while do
-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
6 7
2 4 1
6 7 9
Rupesh Vaishnav #3150703 (ADA) Unit 2 – Analysis of Algorithm 73
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 7
14 16
17 1 14
7 17
16
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 Swap & 71 Already a Max-heap
remove the Swap & remove the last
last element element
1 7
14 1
7
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 94 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
6
do exchange A[1] A[i] 4 5
heap-size[A] heap-size[A] – 1 2 1 3
MAX-HEAPIFY(A, 1, n)
if largest i Yes
then exchange A[i] A[largest]
MAX-HEAPIFY(A, largest, 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
Each segment is
sorted within itself
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)
10 30 60 12 42 93 68 85 80
Each segment is
10 30 60 12 42 93 68 85 80 sorted within itself
using insertion sort
10 30 60 12 42 93 68 85 80
Rupesh Vaishnav #3150703 (ADA) Unit 2 – Analysis of Algorithm 99
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
Each segment is
10 30 42 12 60 93 68 85 80 sorted within itself
using insertion sort
10 12 42 30 60 85 68 93 80
Rupesh Vaishnav #3150703 (ADA) Unit 2 – Analysis of Algorithm 100
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
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
Expected running time is with = size of original sequence. If is then sorting algorithm in
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
Elements 3 6 4 1 3 4 1 4 2
Input array 3 6 4 1 3 4 1 4 2
Index
Elements 02 01 02 03 0 01
+ +
Step 4 In array 𝐶, from index 2 to 𝑛 add the value with previous element
Index
Elements 2 3 5 8 8 9
Input array 3 6 4 1 3 4 1 4 2
Output Array B 1 1 2 3 3 4 4 4 6
8 8 8 8
𝐴= 0 + 1 + 2 + 3 Counter Number of bit
2 2 2 2 value flips
0 0000 0
𝐴=8+4+2+1=𝟏𝟓 1 0001 1
2 0010 2
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