DAA Unit-2
DAA Unit-2
Algorithms (ADA)
GTU # 3150703
Unit-2:
Analysis of
Algorithm
Rupesh Vaishnav
Computer Engineering
Department
Darshan Institute of Engineering & Technology, Rajkot
[email protected]
9428037452
Looping
Outline
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
Rupesh Vaishnav #3150703 (ADA) Unit 2 – Analysis of Algorithm 13
Analysis of Algorithm
implies:
𝒄 . 𝒈(𝒏)
𝒇 (𝒏) implies:
𝒏 𝟎 𝒇 (𝒏)=𝜴 (𝒈 (𝒏)) 𝒏
𝒏𝟎
𝒏
𝒇 (𝒏)=𝜽(𝒈 (𝒏))
1 1 1 1 1 1
2 4 2 2 2 4
3 9 3 3 3 9
4 1 4 4 4 1
5 6
2 5 5 5 6
2
5 5
Rupesh Vaishnav #3150703 (ADA) Unit 2 – Analysis of Algorithm 30
Asymptotic Notations – Examples
Example 3: and
𝑓 ( 𝑛 ) ≤ 𝑔 ( 𝑛 ) ⟹ 𝑓 ( 𝑛 ) =O(𝑔 (𝑛))
1 1 2
2 4 4
3 9 8
4 1 1
6 6 Here for ,
5 2 3
5 2
6 3 6
6 4
7 4 12
9 8
𝒇 (𝒏)=𝑶 (𝒈(𝒏))
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 × 1.26 x
1019 1029
256 8 2048 65536 16777216 1.15 ×
1077
102 10 10240 1048576 1.07 × 109 1.79 ×
4 10308
409 12 49152 1677721 6.87 × 101233
6 Vaishnav
Rupesh #3150703 6 1010of Algorithm
(ADA) Unit 2 – Analysis 33
Growth of Function
1.
2.
𝑂( 𝑓(𝑛)
Maximum Rule: Let, the max rule says that:
+𝑔(𝑛))=𝑂(max(𝑓(𝑛),𝑔(𝑛)))
1. - is
The low order terms in a function are relatively insignificant for large 𝒏
Big O Thus,
Omega Theta θ Notation:
Notation: Notation:
𝒏=𝟏𝟎 steps
𝒏=𝟏𝟎𝟎 steps
𝒏=𝟏𝟎𝟎𝟎 steps
𝒏=𝟏𝟎𝟎𝟎𝟎 steps
𝐜
∗ (𝒏 ) 𝟐
Total time is denoted as,
𝜽 (𝒏)
𝒕 ( 𝒏 )=𝜽 ( 𝒏𝟑 )
𝜽 ( 𝟏)
printf(“sum is now %d”,)
Exampl
e 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 :
3
4 swap
3 3 3
5
4
3 4 4 4
4
5 5 5
2 5
2
swap
6
2 6
2 6
3
2
5 3
5
1
swap
3
1 3
1 3
6
1 6
2
1
5
2 2 2 2
6
swap
swap
4 4
2 4
2 4
3
2 3
1 3
2
1
swap
swap
5
2 5
3
2
4 3
4
1 3
4
1 4
2
1
3 2
3
swap
3
1 3
5
1 5
2
1
4 2
4 2
4 4
2
5 2
5 2
5 5 5 5
6 6 6 6 6 6
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 3 :
Unsorted Array Now Minj = 2, Minx = 1
(elements 3 to 8) Find min value from
1 1 1 1
-5 1 5 2 remaining unsorted array
2 6 2 4
1 2 3 4 5 6 7 8
Index = 2,
value = 1
No Swapping as min value is already at right place
Step 4 :
Unsorted Array Minj = 3, Minx = 12
(elements 4 to 8) Find min value from
remaining unsorted array
1 1 1 1 1
-5 1 2 5 2 Index = 6,
2 6 2 2 4
1 2 3 4 5 6 7 8 value = 2
Swap
1 1 1 1 Index = 6,
-5 1 2 5 value = 12
6
2 2
6 2 4
1 2 3 4 5 6 7 8
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
1 1 1 1 remaining unsorted array
-5 1 2 5
2 6
2 2
6 4 Index = 7,
1 2 3 4 5 6 7 8
value = 12
Swap
Step 8 :
Unsorted Array Minj = 7, Minx = 16
(element 8) Find min value from
remaining unsorted array
1 1 1 1
-5 1 2 5
2 2 6
4 4
6 Index = 8,
1 2 3 4 5 6 7 8 value = 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;
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 order5 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
Rupesh Vaishnav #3150703 (ADA) Unit 2 – Analysis of Algorithm 63
Insertion Sort – Example
Step 3 :
𝒋 𝒊=𝟑 , 𝒙=𝟏𝟐 𝒋=𝒊 – 𝟏 𝒂𝒏𝒅 𝒋>𝟎
1 5 1 -5 1 2 1 1 while do
1 2 2
3 4 6
5 6 2
7 4
8
Step 5 :
𝒋 𝒊=𝟓 , 𝒙=𝟏𝟔 𝒋=𝒊 – 𝟏 𝒂𝒏𝒅 𝒋>𝟎
-5 1 5 1 1 2 1 1 while do
1 2 3 2
4 6
5 6 2
7 4
8
-5 1 5
2 1 1 2 1 1
1 2 3 2
4 6
5 6 2
7 4
8
ShiftShiftShift
downdown down
Rupesh Vaishnav #3150703 (ADA) Unit 2 – Analysis of Algorithm 65
Insertion Sort – Example
Step 7 :
𝒋 𝒊=𝟕 , 𝒙=𝟏𝟐 𝒋=𝒊 – 𝟏 𝒂𝒏𝒅 𝒋>𝟎
-5 1 2 5 1 1 1 1 while do
1 2 3 4 2
5 2
6
6 2
7 4
8
Shift
down
Step 8 :
𝒊=𝟖 , 𝒙=𝟏𝟒 𝒋=𝒊 – 𝟏 𝒂𝒏𝒅 𝒋>𝟎
𝒋 while do
-5 1 2 5 1 1 1 1
1 2 3 4 2
5 2
6 4
6
7 4
8
The entire array is
Shift
down sorted 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
1
6
1 1
4 0
Array representation
8 7 9 3
of heap
2 4 1 1 1 1 8 7 9 3 2 4 1
Hea
6 4 0
p
Rupesh Vaishnav #3150703 (ADA) Unit 2 – Analysis of Algorithm 71
Array Representation of Heap
In the array 𝐴, that represents a heap
1. length[𝐴] = heap-size[𝐴]
2. For any node 𝒊 the parent node is 𝒊/𝟐
3. For any node 𝒋, its left child is 𝟐𝒋 and right child is 𝟐𝒋+ 𝟏
𝟖 𝟗 𝟏𝟎 1
2 4 1 1 1 1 8 7 9 3 2 4 1
Hea 4
6 4 0
p
Rupesh Vaishnav #3150703 (ADA) Unit 2 – Analysis of Algorithm 72
Types of Heap
1. Max-Heap − Where the value of the 9
root node is greater than or equal to
either of its children.
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 1
0
3
In a Max Heap, 5 1
parent node is
always greater than
or equal to the child
nodes.
Rupesh Vaishnav #3150703 (ADA) Unit 2 – Analysis of Algorithm 76
Heap Sort – Example 1
Sort the following elements in
Ascending 4order
10 3 5 1
In a Max Heap, 4
5 1
parent node is
always greater than Max Heap is
or equal to the child created
nodes.
Rupesh Vaishnav #3150703 (ADA) Unit 2 – Analysis of Algorithm 77
Heap Sort – Example 1
Sort the following elements in
Ascending 4order
10 3 5 1
10
1 5 3 4 1
0 5 3
Swa
p
1. Swap the first and 4 1
the last nodes and 0
2. Delete the last
node.
1
4
1 1 1
7 7
6 4 6
7
1 1 1 1
1 1 7
4 7 4 7
6
19
1 14 17 1 7 16
1 16
1 14 17
1 1 7 19
6 9 7 6
1 1 Create Max-
9
6 6
7
Swap heap
1 1 & 1
1
4 7 remo 7
6
4
ve
the
1
1 7 last 1 7
6
9
eleme
nt
17
7 14 16 1 7
1 19 7
1 14 16
7 1 17 19
7 6
1 1 Create Max-
7 7
7 6
Swap heap
1 1 & 1
1 7
4 6 remo 6
4
ve
the
1
1 7 last 1
7
eleme
nt
16
1 14 7 1 17 19 1 14
1 7 16 17 19
6 4
1 1 Create Max-
1 1
6 4
Swap heap
1 &
7 1 7
4 remo 1
4
ve
the
1
1 last
6
eleme
nt
14
7 1 7
1 16 17 19 7
1 1
7 14 16 17 19
4
1 Already a
7 Swap & 7
1
4
remove Max-heap
Swap & remove
1 the last the last element
1 7 element 1
7
4
Step 11
1 7 14 16 17 19
Remove
1
The entire array
theis last
element
sorted 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
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)
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
10 93 60 12 42 30 68 85 80
sort
80 93 60 12 42 30 68 85 10
Each segment
10 30 60 12 42 93 68 85 80 is 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
10 30 42 12 60 93 68 85 80 is 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
10 12 30 42 60 68 80 85 93 within 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
45 96 29 30 27 12 39 61 91
45 96 29 30 27 12 39 61 91
4 9 2 3 2 1 3 6 9
5 6 9 0 7 2 9 1 1
2 3 9
9
7 9 6
1
1 2 3 4 6 9
2 7
9 0 5 1 1
6
0 1 2 3 4 5 6 7 8 9
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
value of array 𝐴
Step 3 Update an array C with the occurrences of each
Index
Elements 02 01 0
2 03 0
0 0
1
+ +
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
1
Temporary Array C 2
0 2
3 4
3
5 6
7
5
8 8 8
9
Output Array B 1 1 2 3 3 4 4 4 6
𝐴=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
8
0111
1000
1
4
Total Flips = 15
K-1
Total time
The amortized cost of each operation is