Professional Documents
Culture Documents
GTU # 3150703
Unit-2:
Analysis of Algorithm
Rupesh Vaishnav
Computer Engineering Department
Darshan Institute of Engineering & Technology, Rajkot
rupesh.vaishnav@darshan.ac.in
9428037452
Introduction
Algorithm: Linear_Search
for i = 1 to last index of A
if A[i] equals element x
return i
return -1
Worst Case
Best Case
Average Case
Pass 1
:
34
45 swa 34 34 34
p
34
45 45 45 45
56 56 56
23 23
swa
p
23 23 23
56 56
12
swa
p
12 12 12 12
56
swa
swa
p
p
45 45
23 23 23
34 34
12 12
23
swa
swa
p
p
23 23
45 45
12 12 12
34 34
swa
p
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+1])
swap(A[j], 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
Rupesh Vaishnav #3150703 (ADA) ⬥ Unit 2 – Analysis of Algorithm 26
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 28
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 12
45
1 2 3 4 5 1 2 3 4 5
Swap
45
12 34
23 56
34 34
23
56
45 45
56
Rupesh Vaishnav #3150703 (ADA) ⬥ Unit 2 – Analysis of Algorithm 33
Selection Sort Analysis
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
1 2 3 4 5 6 7 8
Shift down
Step 3 :
1 5 12 -5 16 2 12 14
1 2 3 4 5 6 7 8
Step 4 :
-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
1 2 3 4 5 6 7 8
Step 6 :
-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
1 2 3 4 5 6 7 8
Shift down
Step 8 :
-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
16
14 10
8 7 9 3
2 4 1 Heap 16 14 10 8 7 9 3 2 4 1
2 4 1
6 7 9
Rupesh Vaishnav #3150703 (ADA) ⬥ Unit 2 – Analysis of Algorithm 44
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
1
10 5 3 4 10
1
5 3
Swap
1
4
51 4 3 15 10
4 3
Swap
13 13 4 5 10
3
1
Swap
1 3 4 5 10
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
39 4 7 2 1 93
1
9
2 3
4 7
4 5 6
2 1 3
1 3 4 7 2 1 9
1
Yes
3
2 3
4 7
4 5
Yes 2 1
Yes
3 4 7 2 1 9
1
7
2 3
4 3
4 5
2 1
heap-size[A] ← length[A]
for i ← ⌊length[A]/2⌋ downto 1
do MAX-HEAPIFY(A, i)
Step 1: Divide input array into segments, where Initial Segmenting Gap = 4 (n/2)
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 63
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 64
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
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
Step 1
Index
Elements 3 6 4 1 3 4 1 4 2
Step 2
Index
Elements 0 0 0 0 0 0
3 6 4 1 3 4 1 4 2
Index
Elements 02 01 02 03 0 10
+ +
Step 4 In array 𝐶, from index 2 to 𝑛 add the value with previous element
Index
Elements 2 3 5 8 8 9
3 6 4 1 3 4 1 4 2
Output Array B 1 1 2 3 3 4 4 4 6