Unit-2 Analysis of Algorithm
Unit-2 Analysis of Algorithm
(ADA)
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
𝟑
Search for𝟕
Best Case
Average Case
Case 1: Numbers are already Case 2: Numbers are Case 3: Numbers are
in required order, i.e., randomly arranged initially. initially arranged in
Ascending order Some numbers will change Descending order so, all
No change is required their position numbers will change their
position
Best Case Average Case Worst Case
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 } 𝐟 ( 𝐧 ) =Ω(𝐠 ( 𝐧 ))
= { : there exist positive constants , andsuch that for all } 𝐟 (𝐧 )=𝛉 (𝐠 ( 𝐧))
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 } 𝐟 ( 𝐧 ) =Ω(𝐠 ( 𝐧 ))
= { : there exist positive constants , andsuch 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:
𝒕 ( 𝒏 )=𝜽 ( 𝒏𝟔 )
Rupesh Vaishnav #3150703 (ADA) Unit 2 – Analysis of Algorithm 44
Sorting Algorithms
Bubble Sort, Selection Sort, Insertion Sort
Introduction
Sorting is any process of arranging items systematically or arranging items in a sequence
ordered by some criterion.
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 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] 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
Unsorted Array (elements 2 to 8) stored 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 =
1 2 3 4 5 6 7 8 14
Swap
The entire array is sorted
now.
Rupesh Vaishnav #3150703 (ADA) Unit 2 – Analysis of Algorithm 57
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.
The time complexity of selection sort is
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 12
45
1 2 3 4 5 1 2 3 4 5
Swap
45
12 34
23 56
34 23
34
45
56 45
56
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
Shift Shift
Shift down downdown
Step 7 :
𝒋 𝒊=𝟕 , 𝒙=𝟏𝟐 𝒋=𝒊 – 𝟏 𝒂𝒏𝒅 𝒋>𝟎
-5 1 2 12 12 14
5 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
The entire array is sorted
Shift down
now.
Rupesh Vaishnav #3150703 (ADA) Unit 2 – Analysis of Algorithm 66
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;
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 - Not a Heap
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
41
51 4 3 15 10
4 3
Swap
13 13 4 5 10
13
Swap
1 3 4 5 10
19 7
14 16
17 1 14
7 17
16
7 16 14
7 17
16
1 14 17 1 7
14 16
17
19
16 14 17 1 7 16
19 16
17 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 19
16 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 7
14 16 17 19 17 17 14 16 17 19
14
7 Swap & 71 Already a Max-
remove the heap the last
Swap & remove
last element element
1 14
7 17
Step 11
1 7 14 16 17 19
Remove the
1
The entire array is sorted
last element
now.
Rupesh Vaishnav #3150703 (ADA) Unit 2 – Analysis of Algorithm 88
Exercises
Sort the following elements using Heap Sort Method.
1. 34, 18, 65, 32, 51, 21
2. 20, 50, 30, 75, 90, 65, 25, 10, 40
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
10 12 30 42 60 68 80 85 93 itself using
insertion sort
The entire array is sorted
now.
Rupesh Vaishnav #3150703 (ADA) Unit 2 – Analysis of Algorithm 101
Shell Sort - Procedure
Divide the array into several smaller non contiguous segments.
The distance between successive elements in one segment is called a gap.
Each segment is sorted within itself using insertion sort.
Then re-segment into larger segments (smaller gap) and repeat sorting process.
Continue until only one segment is left, i.e., gap = 1.
Final sorting finishes the entire array sorting.
Time complexity of shell sort is
Algorithm: RADIX-SORT(A, d)
for i ← 1 to d
do use a stable sort to sort array A on digit i
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
+ +
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
2
0001
0010
1
2
3 0011 1
s can be
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