ADA Unit 2 Notes
ADA Unit 2 Notes
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=0 Step 3: i=2
i i
i i
Element found at ith index, i=3
Linear Search Algorithm
# Input : Array A, element
# Output: First index of element in A
or -1 if not found
Algorithm: Linear_Search(A[1..n],key)
for i = 1 to n do
if (A[i]=key) then
return i
return -1
Linear Search
• The required element in the given array can be found at,
1. E.g. 2: It is at the first position
Best Case: minimum comparison is required
2. E.g. 3 or 1: Anywhere after the first position
Average Case: average number of comparison is required
3. E.g. 8 or 7: Last position or does not found at all
Worst Case: maximum comparison is required
Worst Case
2 9 3 1 8
Best Case
Average Case
Analysis of Algorithm
Best case Average case Worst case
Resource usage is Resource usage is Resource usage is
minimum average maximum
Algorithm’s behavior Algorithm’s behavior Algorithm’s behavior
under optimal under random under the worst
condition condition condition
Minimum number of Average number of Maximum number of
steps or operations steps or operations steps or operations
Lower bound on Average bound on Upper bound on
running time running time running time
Generally do not Average and worst-case performances are
occur in real the most used in algorithm analysis.
C Best = 1 C Avg = (n + 1) / 2 C Worst = n
Number Sorting
• Suppose you are sorting numbers in Ascending / Increasing order.
• The initial arrangement of given numbers can be in any of the
following three orders.
1. Numbers are already in required order, i.e., Ascending order
No change is required – Best Case
2. Numbers are randomly arranged initially.
Some numbers will change their position – Average Case
3. Numbers are initially arranged in Descending or Decreasing
order.
All numbers will change their position – Worst Case
5 9 12 23 32 41
9 5 12 32 23 41 5 9 12 23 32 41
41 32 23 12 9 5
Asymptotic Notations
Asymptotic Notations (Big O, θ - Theta and Ω - Omega) allow us to
analyze an algorithm’s running time.
Asymptotic notation is a shorthand way to represent the time
complexity.
This is also known as an algorithm’s growth rate.
Asymptotic Notations are used,
1. To characterize the complexity of an algorithm.
2. To compare the performance of two or more algorithms solving
the same problem.
Bounds on Running Time
Lower Bound: A lower bound L(n) 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 L(n).
The function f(n)= Θ(g(n)), if there are constants c1, c2 > 0 and a
natural number n0 such that
c1* g(n) ≤ f(n) ≤ c2 * g(n) for all n ≥ n0
Asymptotic Notations - Example
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
Asymptotic Notations - Example
Algo. 1 Algo. 2
running time running time
1 1 2
2 4 4
3 9 8
4 16 16
5 25 32
6 36 64
7 49 128
Asymptotic Notations - Example
g (n)=n2+1
Value of function
f(n)=30n+8
Increasing n
Common Orders of Magnitude (Order of growth)
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
Growth of Function
Arrange the given notations in the increasing order of their values.
1
2n, n log n, n2, 1, n, log n, n!, n3
Solution
1, log n, n, n log n, n2, n3, 2n, n!
2
n3, 1, n2, n log n, n2 log n, log n, n0.5
Solution
1, log n, n0.5, n log n, n2, n2 log n, n3
3
n!, 2n , (n+1)!, 22n, nn, nlog n
Solution
nlog n , 2n , n!, (n+1)!, 22n, nn
Growth of Function
Negligible
Analyzing Control Statement
While analyzing the given fragment of code various programming
constructs appear Those are:
1) Sequencing
2) For loops
3) Recursive calls
4) While & Repeat loops
1) Sequencing:
Sequencing means putting one instruction after another.
Example:
i = 10; 1
printf (“ %d” , i); 1
i = 10 + 5; 1
Here each instruction executes only once. So frequency count
= 1+1+1 = 3
Sum of n Elements of Array
2) For Loops
• Algorithm
//Input: int A[n], array of n integers
//Output: Sum of all numbers in array A
int Sum(int A[], int n)
{
n+1
int s=0;
for (int i=0; i<n; i++)
1 s = s + A[i];
return s;
}
n
Running Time of Algorithm
Running Time of Algorithm
Analyzing Control Statement
Example 1: Example 3:
Analysis
Example 2:
Example 5:
Analyzing Control Statement
3) Recursive calls:
A recursive function is a function that calls itself during its execution.
Example: Algorithm to find factorial of given number
Algorithm factorial(n)
//Problem Description: Algorithm to find factorial of given number.
//Input: Number n
//Output: Returns factorial value of n.
{ If n=3 then
if (n == 0) then
return 1; 3 * fact (2)
else 3*2* fact (1)
return n * factorial(n - 1); 3*2*1*fact (0)
} 3*2*1*1
Here Recursive function can be formulated as: f(n) = n * f(n-1)
We can write recursive relation as: T(n) = T(n-1) + 1
Multiplication for compute f(n-1) To multiply factorial(n-1) by n
Analyzing Control Statement
4) While or Repeat loops:
Analyzing While or Repeat loop we need to find out how many times the loop is repeated.
Example: Algorithm to find maximum value from given Array.
Algorithm max_element(A[0….n-1])
//Problem Description: Algorithm to find maximum value from given Array.
//Input: Array A[0….n-1]
//Output: Returns largest element from the array.
{ j = n-1
max_value A[0];
i 1;
T (n) = 1
i= 1
j n-1;
While (i <= j) do =(n-1)-1+1 =(n – 1)
{
if (A[i] > max_value) then = θ(n)
max_value A[i] ;
}
return max_value;
}
Sorting Algorithms
Example: Bubble sort, Selection sort, Insertion Sort, Heap sort, Shell sort, Quick sort.
Equation used in analysis
1) 1 = 1 + 1 + 1 +……+ 1 = n
2) 1 = 1 + 1 + 1 +……+ 1 = n – 1
3) i = 1 + 2 + 3 +……+ n = n(n+1) /2
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
Only one iteration takes place from 0 to n-2 (n-1-1), i.e. Pass 1
Selection Sort
Step 1 :
Unsorted Array
5 1 12 -5 16 2 12 14
1 2 3 4 5 6 7 8
Step 2 :
Unsorted Array (elements 2 to 8) i = 1, Value A[i] = 5
Find the smallest value
-5
5 1 12 -5
5 16 2 12 14 from the entire Unsorted
1 2 3 4 5 6 7 8 array
Min = 4, value A[min] = -5
Swap
Selection Sort
Step 3 : i = 2, Value A[i] = 1
Unsorted Array (elements 3 to 8)
Find min value from
-5 1 12 5 16 2 12 14 Remaining unsorted array
1 2 3 4 5 6 7 8 min = 2, value A[min] = 1
Step 4 :
Unsorted Array
(elements 4 to 8) i = 3, Value A[i] = 12
Swap
Selection Sort
Step 5 :
Unsorted Array i = 4, value A[i] = 5
(elements 5 to 8)
Find min value from
-5 1 2 5 16 12 12 14 Remaining unsorted array
min= 4, value A[min] = 5
1 2 3 4 5 6 7 8
Step 6 :
Unsorted Array
(elements 6 to 8)
i = 5, value A[i] = 16
-5 1 2 5 12
16 16
12 12 14 Find min value from
1 2 3 4 5 6 7 8 Remaining unsorted array
min = 6, value A[min] = 12
Swap
Selection Sort
Step 7 :
Unsorted Array i = 6, value A[i] = 16
(elements 7 to 8)
Find min value from
-5 1 2 5 12 12
16 16
12 14 Remaining unsorted array
min = 7, value A[min] = 12
1 2 3 4 5 6 7 8
Swap
Step 8 :
Unsorted Array
(element 8)
i = 7, Value A[i] = 16
-5 1 2 5 12 12 14
16 16
14 Find min value from
1 2 3 4 5 6 7 8 Remaining unsorted array
min = 8, value A[min]= 14
Swap
Selection Sort Analysis
# Input: Array A
# Output: Sorted array A
Algorithm: Selection_Sort(A[])
for i ← 1 to n-1 do
{
min ← i;
for j ← i + 1 to n do
{
if (A[j] < A[min]) then
min ← j;
}
temp ← A[i];
A[i] ← A[min];
A[min] ← temp;
}
Selection Sort Algorithm
Algorithm: Selection_Sort(A)
Pass 1 :
for i ← 1 to n-1 do
min ← i; i = 1
for j ← i + 1 to n do
Min ← 12
if A[j] < A[min] then
min ← j ; A[min]← 34
45
temp ← A[i];
A[i] ← A[min]; j = 2 3
A[min] ← temp; No Change
45 34 56 23 12 45
12 34 56 23 12
45
1 2 3 4 5 1 2 3 4 5
45
12 23
34 34
56 34
23
45
56 45
56
Selection Sort Analysis
# Input: Array A
# Output: Sorted array A
Algorithm: Selection_Sort(A)
for i ← 1 to n-1 do
{
min ← i;
for j ← i + 1 to n do
{
if (A[j] < A[min]) then
min ← j;
}
temp ← A[i];
A[i] ← A[min];
A[min] ← temp;
}
The time complexity of Selection sort is
Selection Sort Analysis
Algorithm: Selection_Sort(A) Cost Times
for i ← 1 to n-1 do
min ← i ;
for j ← i + 1 to n do
min ← j ;
temp ← A[i]; C4
A[i] ← A[min]; C5
A[min] ← temp; C6
Selection Sort Analysis
T(n) = C1n+ C2(n-1)+ C3 n(n+1)/2+ C4(n-1) + C5(n-1 ) + C6(n-1)
= an2 + bn + c
= O (n2)
Step 1 :
Unsorted Array
5 1 12 -5 16 2 12 14
1 2 3 4 5 6 7 8
Step 2 :
15 1 12 -5 16 2 12 14
1 2 3 4 5 6 7 8
Shift down
Insertion Sort
Step 3 :
1 5 12 -5 16 2 12 14
1 2 3 4 5 6 7 8
1
-5 5 12 -5 16 2 12 14
1 2 3 4 5 6 7 8
Shift down
Shift down
Shift down
Insertion Sort
Step 5 :
-5 1 5 12 16 2 12 14
1 2 3 4 5 6 7 8
Step 6 :
-5 1 25 12 16 2 12 14
1 2 3 4 5 6 7 8
-5 1 2 12 12 14
5 12 16
1 2 3 4 5 6 7 8
Shift down
Step 8 :
-5 1 2 14 14
5 12 12 16
1 2 3 4 5 6 7 8
Shift down
Sorted List -5 1 2 5 12 12 14 16
Insertion Sort Algorithm
Insertion Sort Analysis
HEAP SORT
a a
b c b c
d e f d e f
9 9
6 7 6 7
2 4 8 2 4 1
2 4 1
1
2. Min-Heap − Where the value of the root
node is less than or equal to either of its
2 4 children.
6 7 9
Array Representation of Heap
heap
16
14 10
8 7 9 3
2 4 1
Array representation of heap
16 14 10 8 7 9 3 2 4 1
Steps of 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.
Heap Sort Example
Sort given element in ascending order using heap sort.
19, 7, 16, 1, 14, 17
1 2 3 4 5 6
19 7 16 1 14 17
Step 1: Create complete binary tree
1
19
2 3 16
7
4 1 5 14 17
6
Heap Sort Example
Sort given element in ascending order using heap sort. 19, 7, 16,
1, 14, 17 1 2 3 4 5 6
19 7 16 1 14 17
4 1 5 14 17 1 5 7 16
6 4 6
Heap Sort Example
Step 3: swap the last node with the root node and delete the
last node from the heap
1 2 3 4 5 6
19 14 17 1 7 16
1 19
Swap &
remove
2 14 3 17 the last
element
6
4 1 5 7 16
Heap Sort Example
Step 2: Create Max-heap
1 2 3 4 5 6
1
16 14 17 1 7 19 16
2 14 17
3
4 5
1 7
Heap Sort Example
Step 3: swap the last node with the Step 2: Create Max-heap
root node and delete the last node
from the heap
1 2 3 4 5 6 1 2 3 4 5 6
17 14 16 1 7 19 7 14 16 1 17 19
1 17 1 7
Swap &
remove
2 14 3 16 the last 14 3
16
2
element
4 1 5 7 4 1
Heap Sort Example
Step 3: swap the last node with the Step 2: Create Max-heap
root node and delete the last node
from the heap
1 2 3 4 5 6 1 2 3 4 5 6
16 14 7 1 17 19 1 14 7 16 17 19
1 16 1 1
Swap &
remove
14 3 7 14 3 7
2 the last 2
element
4 1
Heap Sort Example
Step 3: swap the last node with the Step 2: Create Max-heap
root node and delete the last node
from the heap
1 2 3 4 5 6 1 2 3 4 5 6
14 1 7 16 17 19 7 1 14 16 17 19
1 1 7
14 Swap &
remove
the last
element 2 1
2 1 3 7
Already a Max-heap
Heap Sort Example
Step 3: swap the last node with the Step 2: Create Max-heap
root node and delete the last node
from the heap 1 2 3 4 5 6
1 7 14 16 17 19
1 2 3 4 5 6
1
7 1 14 16 17 19 1
1 7 Swap &
Remove the last element
remove
the last
element The final array is sorted
2 1
1 2 3 4 5 6
1 7 14 16 17 19
Heap Sort Algorithm
Algorithm: BUILD-MAX-HEAP(A)
heap-size[A] ← length[A]
for i ← ⌊length[A]/2⌋ downto 1
do MAX-HEAPIFY(A, i)
Max-Heapify - Algorithm
1
SHELL SORT
Algorithm: RADIX-SORT(A, d)
for i ← 1 to d
do use a stable sort to sort array A
on digit i
Radix Sort
3 6 3
7 2 9
3 2 9
8 7 3
6 9 1
5 2 1 521 873 329
691 363 435 297 729
4 3 5 0 1 2 3 4 5 6 7 8 9
2 9 7
Sort on column 1
Sorted list after sort on column 1 = 691, 521, 363, 873, 435, 297, 729, 329
Radix Sort
6 9 1
5 2 1
3 6 3
8 7 3
4 3 5
329
2 9 7 729 297
521 435 363 873 691
7 2 9
0 1 2 3 4 5 6 7 8 9
3 2 9
Sort on column 2
Sorted list after sort on column 2 = 521, 729, 329, 435, 363, 873, 691, 297
Radix Sort
5 2 1
7 2 9
3 2 9
4 3 5
3 6 3
8 7 3 363
297 329 435 521 691 729 873
6 9 1 0 1 2 3 4 5 6 7 8 9
2 9 7
Sort on column 3
Sorted list after sort on column 3 = 297, 329, 363, 435, 521, 691, 729, 873
BUCKET SORT
•Consider total bucket =10 and range like 0-9, 10-19, 20-29….
Bucket Sort Example
45 96 29 30 27 12 39 61 91
45 96 29 30 27 12 39 61 91
29
27 39 91
96
12 27
29 30 45 61 96
91
0 1 2 3 4 5 6 7 8 9
12 27 29 30 39 45 61 91 96
Bucket Sort Example-2
0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 1 1 1
+
2 0 0 0 0 0 0 1 0 2 3
3 0 0 0 0 0 0 1 1 1 4
4 0 0 0 0 0 1 0 0 3 7
5 0 0 0 0 0 1 0 1 1 8
6 0 0 0 0 0 1 1 0 2 10
7 0 0 0 0 0 1 1 1 1 11
8 0 0 0 0 1 0 0 0 4 15
9 0 0 0 0 1 0 0 1 1 16
10 0 0 0 0 1 0 1 0 2 18
11 0 0 0 0 1 0 1 1 1 19
1. Aggregate Method
1. Aggregate Method
Counter Number of
value bit flips
0 0000 0
1 0001 1
2 0010 2
3 0011 1
4 0100 3
5 0101 1
6 0110 2
7 0111 1
8 1000 4
15
1. Aggregate Method
2. Accounting Method
0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 1 1 1
2 0 0 0 0 0 0 1 0 2 3
3 0 0 0 0 0 0 1 1 1 4
4 0 0 0 0 0 1 0 0 3 7
5 0 0 0 0 0 1 0 1 1 8
6 0 0 0 0 0 1 1 0 2 10
7 0 0 0 0 0 1 1 1 1 11
8 0 0 0 0 1 0 0 0 4 15
9 0 0 0 0 1 0 0 1 1 16
10 0 0 0 0 1 0 1 0 2 18
11 0 0 0 0 1 0 1 1 1 19
3. Potential Method
The potential method is similar to Accounting method in which the
concept of prepaid is used , there will be “potential energy,” or just
“potential,” which can be released to pay for future operations.
We associate the potential with the data structure as a whole rather
than with specific objects within the data structure.
Initial data structure D0.
For n operations D0, D1, D2,……, Dn will be the data structure and C1, C2,
… …, Cn denotes the actual cost.
For each i = 1, 2, ……, n, we let ci be the actual cost of the ith operation
and Di be the data structure that results after applying the i th operation
to data structure Di-1.
Let is a potential function which is a real number.
((Di ) is called potential of Di .
3. Potential Method
The amortized cost of the i th operation is:
Amortized cost