✓ Looping
Outline
▪ Introduction to Algorithm
• Definition
• Characteristics
• Types
• Simple Multiplication Methods
▪ Mathematics for Algorithmic Sets
• Set Theory
• Functions and Relations
• Vectors and Matrices
• Linear Inequalities and Linear Equations
• Logic and Quantifiers
Introduction to Algorithm
3
What is an Algorithm?
�A step-by-step procedure, to solve the different kinds of problems.
�Suppose, we want to make a Chocolate Cake.
�An unambiguous sequence of computational steps that transform the input into the output.
Outpu
t
Input Process
Ingredients Recipe Cake
4
What is an Algorithm?
�A process or a set of rules to be followed to achieve desired output, especially by a computer.
�An algorithm is any well-defined computational procedure that takes some value, or a set of
values as input and produces some value, or a set of values as output.
Outpu
t
Algorithm Program
Input
5
Characteristics of An Algorithm
�Finiteness: An algorithm must always terminate after a finite number of steps.
�Definiteness: Each step of an algorithm must be precisely defined.
�Input: An algorithm has zero or more inputs.
�Output: An algorithm must have at least one desirable output.
�Effectiveness: All the operations to be performed in the algorithm must be sufficiently basic so
that they can, in principle be done exactly and in a finite length of time.
6
Types of Algorithm
�Simple recursive algorithms
�Backtracking algorithms
�Divide and conquer algorithms
�Dynamic programming algorithms
�Greedy algorithms
�Branch and bound algorithms
�Brute force algorithms
�Randomized algorithms
✓ 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
9
Introduction
What is Analysis of an Algorithm?
✔ Analyzing an algorithm means calculating/predicting the resources that the
algorithm requires.
✔ Analysis provides theoretical estimation for the required resources of an
algorithm to solve a specific computational problem.
✔ Two most important resources are computing time (time complexity) and
storage space (space complexity).
Why Analysis is required?
✔ By analyzing some of the candidate algorithms for a problem, the most
efficient one can be easily identified.
10
Efficiency of Algorithm
�The efficiency of an algorithm is a measure of the amount of resources consumed in solving a
problem of size 𝑛.
�An algorithm must be analyzed to determine its resource usage.
�Two major computational resources are execution time and memory space.
�Memory Space requirement can not be compared directly, so the important resource is
computational time required by an algorithm.
�To measure the efficiency of an algorithm requires to measure its execution time using any of
the following approaches:
1. Empirical Approach: To run it and measure how much processor time is needed.
2. Theoretical Approach: Mathematically computing how much time is needed as a function of input size.
11
How Analysis is Done?
Theoretical (priori) approach
Empirical (posteriori) approach
▪ Programming different competing
techniques & running them on
various inputs using computer.
▪ Implementation of different
techniques may be difficult.
▪ The same hardware and software
environments must be used for
comparing two algorithms.
▪ Results may not be indicative of the
running time on other inputs not
included in the experiment.
12
Time Complexity
�Time complexity of an algorithm quantifies the amount of time taken by an algorithm to run as
a function of the length of the input.
�Running time of an algorithm depends upon,
1. Input Size
2. Nature of Input
�Generally time grows with the size of input, for example, sorting 100 numbers will take less
time than sorting of 10,000 numbers.
�So, running time of an algorithm is usually measured as a function of input size.
�Instead of measuring actual time required in executing each statement in the code, we
consider how many times each statement is executed.
�So, in theoretical computation of time complexity, running time is measured in terms of number
of steps/primitive operations performed.
13
Problem & Instance
�
14
Linear Search
�Suppose, you are given a jar containing some business cards.
�You are asked to determine whether the name “Bill Gates" is in the jar.
�To do this, you decide to simply go through all the cards one by one.
�How long this takes?
�Can be determined by how many cards are in the jar, i.e., Size of Input.
�Linear search is a method for finding a particular value from the given list.
�The algorithm checks each element, one at a time and in sequence, until the desired element is
found.
�Linear search is the simplest search algorithm.
�It is a special case of brute-force search.
15
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
17
Linear Search - Analysis
�The required element in the given array can be found at,
Best Case
Average Case
Worst Case
Case 1: element 2 which is
at the first position so
minimum comparison is
required
Case 2: element 3 anywhere
after the first position so, an
average number of
comparison is required
Case 3: element 7 at last
position or element does not
found at all, maximum
comparison is required
Best Case Average Case Worst Case
18
Analysis of Algorithm
Best Case Average Case Worst Case
Resource usage is
minimum
Resource usage is
average
Resource usage is
maximum
Algorithm’s behavior
under optimal condition
Algorithm’s behavior
under random condition
Algorithm’s behavior
under the worst condition
Minimum number of
steps or operations
Average number of steps
or operations
Maximum number of
steps or operations
Lower bound on running
time
Average bound on
running time
Upper bound on running
time
Generally do not occur in
real
Average and worst-case performances are the most
used in algorithm analysis.
19
▪ Suppose, you are writing a program to find a
book from the shelf.
▪ For any required book, it will start checking
books one by one from the bottom.
▪ If you wanted Harry Potter 3, it would only
take 3 actions (or tries) because it’s the third
book in the sequence.
▪ If Harry Potter 7 — it’s the last book so it
would have to check all 7 books.
▪ What if there are total 10 books? How about
10,00,000 books? It would take 1 million tries.
Book Finder Example
21
Number Sorting - Example
�Suppose you are sorting numbers in Ascending / Increasing order.
�The initial arrangement of given numbers can be in any of the following three orders.
Case 1: Numbers are already
in required order, i.e.,
Ascending order
No change is required
Case 2: Numbers are
randomly arranged initially.
Some numbers will change
their position
Case 3: Numbers are initially
arranged in Descending
order so, all numbers will
change their position
Best Case Average Case Worst Case
22
Best, Average, & Worst Case
Problem Best Case Average Case Worst Case
Linear Search Element at the first
position
Element in any of
the middle positions
Element at last position
or not present
Book Finder The first book Any book in-
between
The last book
Sorting Already sorted Randomly arranged Sorted in reverse order
Asymptotic Notations
24
Introduction
�The theoretical (priori) approach of analyzing an algorithm to measure the efficiency does not
depend on the implementation of the algorithm.
�In this approach, the running time of an algorithm is describes as Asymptotic Notations.
�Computing the running time of algorithm’s operations in mathematical units of computation
and defining the mathematical formula of its run-time performance is referred to as Asymptotic
Analysis.
�An algorithm may not have the same performance for different types of inputs. With the
increase in the input size, the performance will change.
�Asymptotic analysis accomplishes the study of change in performance of the algorithm with
the change in the order of the input size.
�Using Asymptotic analysis, we can very well define the best case, average case, and worst case
scenario of an algorithm.
25
Asymptotic Notations
�
26
�
27
28
�
29
�
30
31
�
32
33
Asymptotic Notations
1. O-Notation (Big O notation) (Upper Bound)
1. Ω-Notation (Omega notation) (Lower Bound)
1. θ-Notation (Theta notation) (Same order)
34
Asymptotic Notations – Examples
�
Algo. 1 running
time
Algo. 2 running
time
Algo. 1 running
time
Algo. 2 running
time
1
2
3
4
5
1
2
3
4
5
1
4
9
16
25
1
2
3
4
5
1
4
9
16
25
1
2
3
4
5
35
Asymptotic Notations – Examples
�
1
2
3
4
5
6
7
1
4
9
16
25
36
49
2
4
8
16
32
64
128
36
Asymptotic Notations – Examples
f(n)=30n+8
Increasing n →
g (n)=n2+1
Value
of
function
→
37
Common Orders of Magnitude
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
38
Growth of Function
�
1.
2.
39
Asymptotic Notations in Equations
�
Negligible
𝑂( 𝑓(𝑛)+𝑔(𝑛))=𝑂(max(𝑓(𝑛),𝑔(𝑛)))
40
Asymptotic Notations
1. O-Notation (Big O notation) (Upper Bound)
1. Ω-Notation (Omega notation) (Lower Bound)
1. θ-Notation (Theta notation) (Same order)
41
�
42
�
43
Exercises
�
Analyzing Control Statements
45
For Loop
# Input : int A[n], array of n integers
# Output : Sum of all numbers in array A
Algorithm: int Sum(int A[], int n)
{
int s=0;
for (int i=0; i<n; i++)
s = s + A[i];
return s;
}
1
n+1
n
Total time taken = n+1+n+2 = 2n+3
Time Complexity f(n) =
2n+3
46
Running Time of Algorithm
�
47
Analyzing Control Statements
Example 1:
Example 2:
Example 3:
48
Analyzing Control Statements
Example 6:
Example 4:
Example 5:
Sorting Algorithms
Bubble Sort, Selection Sort, Insertion Sort
50
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.
51
Bubble Sort – Example
45 34 56 23 12
Sort the following array in Ascending order
Pass 1
:
45
34
56
23
12
34
45
56
23
12
34
45
56
23
12
34
45
23
56
12
34
45
swa
p
swa
p
23
56
swa
p
12
56
52
Bubble Sort – Example
Pass 2
:
34
45
23
12
56
34
45
23
12
56
34
23
45
12
56
34
23
12
45
56
swa
p
23
45
swa
p
12
45
Pass 3
:
23
34
12
45
56
23
12
34
45
56
swa
p
23
34
swa
p
12
34
Pass 4
:
swa
p
12
23
53
Bubble Sort - Algorithm
# Input: Array A
# Output: Sorted array A
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
swap(A[j], A[j+1])
temp ← A[j]
A[j] ← A[j+1]
A[j+1] ← temp
54
Bubble Sort
�
55
Bubble Sort Algorithm – Best Case Analysis
# Input: Array A
# Output: Sorted array A
Algorithm: Bubble_Sort(A)
int flag=1;
for i ← 1 to n-1 do
for j ← 1 to n-i do
if A[j] > A[j+1] then
flag = 0;
swap(A[j],A[j+1])
if(flag == 1)
cout<<"already sorted"<<endl
break;
Pass 1
:
34
45
59
12
23
i =
1
j =
1
j =
2
j =
3
j =
4
Condition never
becomes true
58
Selection Sort – Example 1
5 1 12 -5 16 2 12 14
Step 1
:
5 1 12 -5 16 2 12 14
Unsorted Array
1 2 3 4 5 6 7 8
Step 2
:
5 1 12 -5 16 2 12 14
1 2 3 4 5 6 7 8
▪ Minj denotes the current index and Minx is the value
stored at current index.
▪ So, Minj = 1, Minx = 5
▪ Assume that currently Minx is the smallest value.
▪ Now find the smallest value from the remaining entire
Unsorted array.
Unsorted Array (elements 2 to 8)
Swap
-5 5
Index = 4, value = -5
Sort the following elements in Ascending order
59
Selection Sort – Example 1
Step 3
:
-5 1 12 5 16 2 12 14
1 2 3 4 5 6 7 8
Unsorted Array (elements 3 to 8) ▪ Now Minj = 2, Minx = 1
▪ Find min value from remaining
unsorted array
No Swapping as min value is already at right place
1
Step 4
:
-5 1 12 5 16 2 12 14
1 2 3 4 5 6 7 8
Unsorted Array
(elements 4 to 8)
Swap
2 12
▪ Minj = 3, Minx = 12
▪ Find min value from remaining
unsorted array
Index = 2, value = 1
Index = 6, value = 2
60
Selection Sort – Example 1
Step 5
:
Step 6
:
-5 1 2 5 16 12 12 14
1 2 3 4 5 6 7 8
-5 1 2 5 16 12 12 14
1 2 3 4 5 6 7 8
Swap
Unsorted Array
(elements 5 to
8)
5
Unsorted Array
(elements 6 to
8)
12 16
▪ Now Minj = 4, Minx = 5
▪ Find min value from remaining
unsorted array
No Swapping as min value is already at right place
▪ Minj = 5, Minx = 16
▪ Find min value from remaining
unsorted array
Index = 4, value = 5
Index = 6, value = 12
61
Selection Sort – Example 1
Step 7
:
Step 8
:
-5 1 2 5 12 16 12 14
1 2 3 4 5 6 7 8
Swap
12 16
Unsorted Array
(elements 7 to
8)
-5 1 2 5 12 12 16 14
1 2 3 4 5 6 7 8
Swap
14 16
Unsorted Array
(element 8)
▪ Now Minj = 6, Minx = 16
▪ Find min value from remaining
unsorted array
▪ Minj = 7, Minx = 16
▪ Find min value from remaining
unsorted array
Index = 7, value = 12
Index = 8, value = 14
The entire array is sorted now.
62
Selection Sort
�
63
Selection Sort - Algorithm
# Input: Array A
# Output: Sorted array A
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;
64
Selection Sort – Example 2
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
Sort in Ascending order
i = 1
minj ← 1
minx ← 45
j = 2
Pass 1
:
1 2 3 4 5
45
34
2
3
34
A[j] = 34
56
No
Change
65
Selection Sort – Example 2
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
Sort in Ascending order
i = 1
minj ← 2
minx ← 34
j = 2
Pass 1
:
1 2 3 4 5
45
23
4
3
12
A[j] = 23
12
4 5
5
45 34 56 23 12
1 2 3 4 5
45
12
Unsorted
Array
Swap
45 34 56 23 45
12 23 34
56
45 56
34
67
Insertion Sort – Example
5 1 12 -5 16 2 12 14
Step 1
:
5 1 12 -5 16 2 12 14
Unsorted Array
1 2 3 4 5 6 7 8
Step 2
:
1 2 3 4 5 6 7 8
Shift down
Sort the following elements in Ascending order
5 1 12 -5 16 2 12 14
1
68
Insertion Sort – Example
Step 3
:
Step 4
:
1 2 3 4 5 6 7 8
1 5 12 -5 16 2 12 14
1 2 3 4 5 6 7 8
No Shift will take place
1 5 12 -5 16 2 12 14
-5
Shift down
Shift down
Shift down
69
Insertion Sort – Example
1 2 3 4 5 6 7 8
Shift down
-5 1 5 12 16 2 12 14
1 2 3 4 5 6 7 8
No Shift will take place
-5 1 5 12 16 2 12 14
2
Shift down
Shift down
Step 5
:
Step 6
:
70
Insertion Sort – Example
Step 7
:
Step 8
:
1 2 3 4 5 6 7 8
Shift down
-5 1 2 5 12 12 16 14
1 2 3 4 5 6 7 8
-5 1 2 5 12 16 12 14
Shift down
1
2
14
The entire array is sorted now.
71
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;
72
Insertion Sort Algorithm – Best Case Analysis
# 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;
34
45
59
12
23 x=23
T[j]=23
T[j]=34
T[j]=45
i=2
i=3
i=4
i=5
Pass 1
:
T[j]=12
x=34
x=45
x=59
Heap & Heap Sort Algorithm
74
Introduction
�A heap data structure is a binary tree with the following two properties.
1. It is a complete binary tree: Each level of the tree is completely filled, except possibly the bottom level. At
this level it is filled from left to right.
2. It satisfies the heap order property: the data item stored in each node is greater than or equal to the
data item stored in its children node.
a
b c
d e f
Binary Tree but not a Heap Complete Binary Tree - Heap
a
b c
d e f
9
6 7
2 4 8
Not a Heap
9
6 7
2 4 1
Heap
75
Array Representation of Heap
�Heap can be implemented using an Array.
�An array 𝐴 that represents a heap is an object with two attributes:
1. 𝑙𝑒𝑛𝑔𝑡ℎ[𝐴], which is the number of elements in the array, and
2. ℎ𝑒𝑎𝑝−𝑠𝑖𝑧𝑒[𝐴], the number of elements in the heap stored within array 𝐴
16
14 10
8 7 9 3
2 4 1 16 14 10 8 7 9 3 2 4 1
Array representation of heap
Heap
76
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 𝟐𝒋+𝟏
16
14 10
8 7 9 3
2 4 1 16 14 10 8 7 9 3 2 4 1
Heap 14 8 2 4
77
Types of Heap
1. Max-Heap − Where the value of the root node is
greater than or equal to either of its children.
9
6 7
2 4 1
2. Min-Heap − Where the value of the root node is less than
or equal to either of its children.
1
2 4
6 7 9
78
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.
79
Heap Sort – Example 1
4 10 3 5 1
Sort the following elements in Ascending order
4 10 3 5 1
Step 1 : Create Complete Binary Tree
4
10 3
5 1
Now, a binary tree is
created and we have to
convert it into a Heap.
80
Heap Sort – Example 1
4 10 3 5 1
Sort the following elements in Ascending order
4 10 3 5 1
4
10 3
5 1
Step 2 : Create Max Heap
In a Max Heap, parent node
is always greater than or
equal to the child nodes.
10 is greater than 4
So, swap 10 & 4
Swap
10 4
10
4
81
Heap Sort – Example 1
4 10 3 5 1
Sort the following elements in Ascending order
10 4 3 5 1
10
4 3
5 1
In a Max Heap, parent node
is always greater than or
equal to the child nodes.
5 is greater than 4
So, swap 5 & 4
Swap
4
5
Max Heap is created
Step 2 : Create Max Heap
5
4
82
Heap Sort – Example 1
4 10 3 5 1
Sort the following elements in Ascending order
10 5 3 4 1
10
5 3
4 1
1. Swap the first and the
last nodes and
2. Delete the last node.
Step 3 : Apply Heap Sort
Swap
10
1
1
10
83
Heap Sort – Example 1
4 10 3 5 1
Sort the following elements in Ascending order
1 5 3 4 10
1
5 3
4
Step 3 : Apply Heap Sort
Max Heap Property is
violated so, create a
Max Heap again.
1
5 1
Swap
4
5
1
1
4
84
Heap Sort – Example 1
4 10 3 5 1
Sort the following elements in Ascending order
5 4 3 1 10
5
4 3
1
Step 3 : Apply Heap Sort
Max Heap is created
1. Swap the first and the
last nodes and
2. Delete the last node.
Swap
1 5
1
5
85
Heap Sort – Example 1
4 10 3 5 1
Sort the following elements in Ascending order
1 4 3 5 10
1
4 3
Step 3 : Apply Heap Sort
Create Max Heap
again
1. Swap the first and the
last nodes and
2. Delete the last node.
1
4
Swap
Max Heap is created
4
4
1
3
4
3
86
Heap Sort – Example 1
4 10 3 5 1
Sort the following elements in Ascending order
3 1 4 5 10
3
1
Step 3 : Apply Heap Sort
Already a Max Heap
1. Swap the first and the
last nodes and
2. Delete the last node.
Swap
1
3
3
1
87
Heap Sort – Example 1
4 10 3 5 1
Sort the following elements in Ascending order
1 3 4 5 10
1
Step 3 : Apply Heap Sort
Already a Max Heap
Remove the last element
from heap and the
sorting is over.
1
88
Heap Sort – Example 2
�Sort given element in ascending order using heap sort. 19, 7, 16, 1, 14, 17
19 7 16 1 14 17
19
7 16
1 14 17
Step 2: Create Max-heap
Step 1: Create binary tree
19
7 16
1 14 17
16
17
7
14
16
17
14 7
89
Heap Sort – Example 2
19
14 17
1 7 16
19 14 17 1 7 16
16
14 17
1 7
16 14 17 1 7 19
Swap &
remove
the last
element
Create Max-heap
16
19
16 19 16
17
16
17
Step 3 Step 4
90
Heap Sort – Example 2
17
14 16
1 7
17 14 16 1 7 19
7
14 16
1
7 14 16 1 17 19
Swap &
remove
the last
element
Create Max-heap
7
17
17
7 16 7
16
7
Step 5 Step 6
91
Heap Sort – Example 2
16
14 7
1
16 14 7 1 17 19
1
14 7
1 14 7 16 17 19
Swap &
remove
the last
element
Create Max-heap
16
1
16
1 14 1
14
1
Step 7 Step 8
92
Heap Sort – Example 2
14
1 7
14 1 7 16 17 19
7
1
7 1 14 16 17 19
Swap &
remove the
last element
Already a Max-heap
14
7
14
7 7
1
7
1
Swap & remove the last
element
1 7 14 16 17 19
1
Remove the
last element
1 7 14 16 17 19
The entire array is sorted now.
Step 9 Step 10
Step 11
93
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
94
Binary Tree Analysis
95
Heap Sort – Algorithm
�
96
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)
4 1 3 2 9 7
heap-size[A] = 6
i = 3
4
1 3
2 9 7
1
2 3
4 5 6
4
1 3
2 9 7
1
2 3
4 5 6
4
1 7
2 9 3
1
2 3
4 5 6
4
9 7
2 1 3
1
2 3
4 5 6
i = 2 i = 1
4 1 7 2 9 3 4 9 7 2 1 3 9 4 7 2 1 3
7
3
9
1
9
4
97
Heap Sort – Algorithm
�
9
4 7
2 1 3
1
2 3
4 5 6
9 4 7 2 1 3
3 4 7 2 1 9
98
Heap Sort – Algorithm
�
1
Yes
Yes
Yes
3
4 7
2 1
1
2 3
4 5
3 4 7 2 1 9
99
Heap Sort – Algorithm
�
7
4 3
2 1
1
2 3
4 5
3 4 7 2 1 9
100
Heap Sort Algorithm – Analysis
�
heap-size[A] ← length[A]
for i ← ⌊length[A]/2⌋ downto 1
do MAX-HEAPIFY(A, i)
Sorting Algorithms
Shell Sort, Radix Sort, Bucket Sort, Counting Sort
102
Shell Sort - Example
�Sort the following elements in ascending order using shell sort.
80 93 60 12 42 30 68 85 10
80 93 60 12 42 30 68 85 10
10 93 60 12 42 30 68 85 80
Step 1: Divide input array into segments, where Initial Segmenting Gap = 4 (n/2)
Each segment is
sorted within
itself using
insertion sort
This algorithm avoids large shifts
as in case of insertion sort, if the
smaller value is to the far right
and has to be moved to the far
left.
103
Shell Sort - Example
�Sort the following elements in ascending order using shell sort.
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
10 30 60 12 42 93 68 85 80
10 30 60 12 42 93 68 85 80
Each segment is
sorted within
itself using
insertion sort
104
Shell Sort - Example
�Sort the following elements in ascending order using shell sort.
80 93 60 12 42 30 68 85 10
Step 2: Now, the Segmenting Gap = 2 (4/2)
Each segment is
sorted within
itself using
insertion sort
10 30 60 12 42 93 68 85 80
10 30 42 12 60 93 68 85 80
10 12 42 30 60 85 68 93 80
105
Shell Sort - Example
�Sort the following elements in ascending order using shell sort.
80 93 60 12 42 30 68 85 10
Step 3: Now, the Segmenting Gap = 1 (2/2)
Each segment is
sorted within
itself using
insertion sort
10 12 42 30 60 85 68 93 80
10 12 30 42 60 68 80 85 93
The entire array is sorted now.
106
Shell Sort - Procedure
�
107
Radix Sort
�Radix Sort puts the elements in order by comparing the digits of the numbers.
�Each element in the 𝒏-element array 𝑨 has 𝒅 digits, where digit 1 is the lowest-order digit and
digit 𝑑 is the highest order digit.
�Sort following elements in Ascending order using radix sort.
363, 729, 329, 873, 691, 521, 435, 297
Algorithm: RADIX-SORT(A, d)
for i ← 1 to d
do use a stable sort to sort array A on digit
i
108
Radix Sort - Example
3 6 3
7 2 9
3 2 9
8 7 3
6 9 1
5 2 1
4 3 5
2 9 7
Sort on column
1
6 9 1
5 2 1
3 6 3
8 7 3
4 3 5
2 9 7
7 2 9
3 2 9
Sort on column
2
5 2 1
7 2 9
3 2 9
4 3 5
3 6 3
8 7 3
6 9 1
2 9 7
Sort on column
3
The entire array is sorted now.
109
Bucket Sort – Introduction
�
45 96 29 30 27 12 39 61 91
110
Bucket Sort – Example
45 96 29 30 27 12 39 61 91
0 4
1 9
3 5 7 8
2 6
45 96
29 30
27
12
39
61
91
Sort each bucket queue with insertion sort
27
29
91
96
Merge all bucket queues together in order
45 96 29 30 27 12 39 61 91
12 27 29 30 39 45 61 91 96
111
Bucket Sort - Algorithm
# Input: Array A
# Output: Sorted array A
Algorithm: Bucket-Sort(A[1,…,n])
n ← length[A]
for i ← 1 to n do
insert A[i] into bucket B[⌊A[i] ÷ n ⌋]
for i ← 0 to n – 1 do
sort bucket B[i] with insertion sort
concatenate the buckets B[0], B[1], . . ., B[n - 1] together
in order.
112
Counting Sort – Example
�Sort the following elements in Ascending order using counting sort.
3 6 4 1 3 4 1 4 2
Step 1
Index
Elements
Step 2
0 0 0 0 0 0
Index
Elements
3 6 4 1 3 4 1 4 2
113
Counting Sort – Example
�Sort the following elements in Ascending order using counting sort.
3 6 4 1 3 4 1 4 2
Step 3
Index
Elements
Step 4
Update an array C with the occurrences of each value of array
𝐴
In array 𝐶, from index 2 to 𝑛 add the value with previous element
2 3 5 8 8 9
Index
Elements
0 0 0 0 0 0
2 1 2 3 1
0
+ +
114
Counting Sort – Example
�Create an output array 𝐵[1…9]. Start positioning elements of Array 𝐴 𝑡𝑜 𝐵 as shown below.
3 6 4 1 3 4 1 4 2
2 3 5 8 8 9
Temporary Array C
Output Array B 1 1 2 3 3 4 4 4 6
2 7
1 6
4
0 5 8
3
115
Counting Sort - Procedure
�Counting sort assumes that each of the 𝑛 input elements is an integer in the range 0 to 𝑘, for
some integer 𝑘.
�When 𝒌=𝑶(𝒏), the counting sort runs in 𝜽(𝒏) time.
�The basic idea of counting sort is to determine, for each input element 𝑥, the number of
elements less than 𝑥.
�This information can be used to place element 𝑥 directly into its position in the output array.
116
Counting Sort - Algorithm
# Input: Array A
# Output: Sorted array A
Algorithm: Counting-Sort(A[1,…,n], B[1,…,n], k)
for i ← 1 to k do
c[i] ← 0
for j ← 1 to n do
c[A[j]] ← c[A[j]] + 1
for i ← 2 to k do
c[i] ← c[i] + c[i-1]
for j ← n downto 1 do
B[c[A[j]]] ← A[j]
c[A[j]] ← c[A[j]] - 1
Amortized Analysis
118
Introduction
�Amortized analysis considers not just one operation, but a sequence of operations on a given
data structure or a database.
�Amortized Analysis is used for algorithms where an occasional operation is very slow, but most
of the other operations are faster.
�The time required to perform a sequence of data structure operations is averaged over all
operations performed.
�In Amortized Analysis, we analyze a sequence of operations and guarantee a worst case
average time which is lower than the worst case time of a particular expensive operation.
�So, Amortized analysis can be used to show that the average cost of an operation is small even
though a single operation might be expensive.
119
Amortized Analysis Techniques
�There are three most common techniques of amortized analysis,
1. The aggregate method
▪ A sequence of 𝑛 operation takes worst case time 𝑇(𝑛)
▪ Amortized cost per operation is 𝑇(𝑛)/𝑛
2. The accounting method
▪ Assign each type of operation an (different) amortized cost
▪ Overcharge some operations
▪ Store the overcharge as credit on specific objects
▪ Then use the credit for compensation for some later operations
3. The potential method
▪ Same as accounting method
▪ But store the credit as “potential energy” and as a whole.
120
Amortized Analysis - Example
0
Counter
value [7] [6] [5] [4] [3] [2] [1] [0]
Increment
cost
Total
cost
0 0 0 0 1 0 1
0 0 0 0 1 0 1
0 0 0 0 0 1 1 0
0 0 0 0 1
0 0 0 0 1 0 0 0
0 0 0 0 0 1
0 0 0 0 0 1 0 0
0 0 0 0 0 1 1
0 0 0 0 0 0 0
0 0 0 0 0 0 1
0 0 0 0 0 0 0
9
10
6
7
8
3
4
5
0
1
2
1
2
2
1
4
1
3
1
1
2
16
18
10
11
15
4
7
8
1
3
0 0 0 0 1 0 1
11 1 19
0
0
1
1
0
0
1
1
0
0
1
0
+
121
Amortized Analysis - Example
0
Counter
value [7] [6] [5] [4] [3] [2] [1] [0]
Increment
cost
Total
cost
0 0 0 0 1 0 1
0 0 0 0 1 0 1
0 0 0 0 0 1 1 0
0 0 0 0 1
0 0 0 0 1 0 0 0
0 0 0 0 0 1
0 0 0 0 0 1 0 0
0 0 0 0 0 1 1
0 0 0 0 0 0 0
0 0 0 0 0 0 1
0 0 0 0 0 0 0
9
10
6
7
8
3
4
5
0
1
2
1
2
2
1
4
1
3
1
1
2
16
18
10
11
15
4
7
8
1
3
0 0 0 0 1 0 1
11 1 19
0
0
1
1
0
0
1
1
0
0
1
0
122
Aggregate Method
�
Counter
value
Number of bit
flips
0 0 0 0 0 0
1 0 0 0 1 1
2 0 0 1 0 2
3 0 0 1 1 1
4 0 1 0 0 3
5 0 1 0 1 1
6 0 1 1 0 2
7 0 1 1 1 1
8 1 0 0 0 4
Total Flips = 15
123
Aggregate Method
�
K-1
125
Amortized Analysis - Example
0
Counter
value [7] [6] [5] [4] [3] [2] [1] [0]
Increment
cost
Total
cost
0 0 0 0 1 0 1
0 0 0 0 1 0 1
0 0 0 0 0 1 1 0
0 0 0 0 1
0 0 0 0 1 0 0 0
0 0 0 0 0 1
0 0 0 0 0 1 0 0
0 0 0 0 0 1 1
0 0 0 0 0 0 0
0 0 0 0 0 0 1
0 0 0 0 0 0 0
9
10
6
7
8
3
4
5
0
1
2
1
2
2
1
4
1
3
1
1
2
16
18
10
11
15
4
7
8
1
3
0 0 0 0 1 0 1
11 1 19
0
0
1
1
0
0
1
1
0
0
1
0
Running time of an increment operation is proportional to
the number of bits flipped.
126
Potential Method
�
127
Potential Method
�
Thank You!

More Related Content

PPTX
Algorithm analysis (All in one)
PDF
Analysis of algorithm. big-oh notation.omega notation theta notation.performa...
PPTX
Data structures and ALGORITHMS methd binary SEARCH
PPTX
Data structures and algorithms (DSA) are foundational concepts in computer sc...
PDF
Slides [DAA] Unit 2 Ch 2.pdf
PPTX
Algorithm.pptx
PPTX
Algorithm.pptx
PPTX
Algorithm description in data structures
Algorithm analysis (All in one)
Analysis of algorithm. big-oh notation.omega notation theta notation.performa...
Data structures and ALGORITHMS methd binary SEARCH
Data structures and algorithms (DSA) are foundational concepts in computer sc...
Slides [DAA] Unit 2 Ch 2.pdf
Algorithm.pptx
Algorithm.pptx
Algorithm description in data structures

Similar to daa unit 1.pptx (20)

PPTX
Design and analysis of algorithms unit1.pptx
PDF
Algorithm Analysis.pdf
PPT
introegthnhhdfhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhppt
PDF
introduction to analysis of algorithm in computer science
PDF
Introduction to analysis algorithm in computer Science
PDF
1_Algo_lyst1729856407228_lyst1736581584300.pdf
PPTX
Unit ii algorithm
PDF
1-Algorithm Analysijhjhjhjhjhjhjhjhjhjs.pdf
PDF
Analysis and Algorithms: basic Introduction
PPTX
Algorithm in data structure bca .pptx
PDF
Design Analysis and Algorithm Module1.pdf
PPTX
DA lecture 3.pptx
PPT
Complexity
PPT
Design and analysis of algorithm in Computer Science
PDF
Discrete structure ch 3 short question's
PPTX
ADS Introduction
PPTX
Analysis of Algorithm full version 2024.pptx
PPTX
Design Analysis of Alogorithm 1 ppt 2024.pptx
PPT
Algorithms
PDF
Algorithm Design and Analysis
Design and analysis of algorithms unit1.pptx
Algorithm Analysis.pdf
introegthnhhdfhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhppt
introduction to analysis of algorithm in computer science
Introduction to analysis algorithm in computer Science
1_Algo_lyst1729856407228_lyst1736581584300.pdf
Unit ii algorithm
1-Algorithm Analysijhjhjhjhjhjhjhjhjhjs.pdf
Analysis and Algorithms: basic Introduction
Algorithm in data structure bca .pptx
Design Analysis and Algorithm Module1.pdf
DA lecture 3.pptx
Complexity
Design and analysis of algorithm in Computer Science
Discrete structure ch 3 short question's
ADS Introduction
Analysis of Algorithm full version 2024.pptx
Design Analysis of Alogorithm 1 ppt 2024.pptx
Algorithms
Algorithm Design and Analysis
Ad

More from LakshayYadav46 (10)

PPTX
lakshay yadav training report on alarm.pptx
PPTX
project machine learning using java.pptx
PPTX
quick and merge.pptx
PPT
minor project.ppt
PPT
PPTX
hauntedplacesppt.pptx
PPTX
Presentation Session - Frequently Asked Questions in PIs.pptx
PPTX
heartdiseaseprediction.pptx
PPTX
Calculator using python with gui.pptx
PPTX
201302057 lakshay it.pptx
lakshay yadav training report on alarm.pptx
project machine learning using java.pptx
quick and merge.pptx
minor project.ppt
hauntedplacesppt.pptx
Presentation Session - Frequently Asked Questions in PIs.pptx
heartdiseaseprediction.pptx
Calculator using python with gui.pptx
201302057 lakshay it.pptx
Ad

Recently uploaded (20)

PDF
Systems Analysis and Design, 12th Edition by Scott Tilley Test Bank.pdf
PDF
Tetra Pak Index 2023 - The future of health and nutrition - Full report.pdf
PPTX
FMIS 108 and AISlaudon_mis17_ppt_ch11.pptx
PPTX
The Data Security Envisioning Workshop provides a summary of an organization...
PDF
Loose-Leaf for Auditing & Assurance Services A Systematic Approach 11th ed. E...
PPTX
New ISO 27001_2022 standard and the changes
PPTX
CHAPTER-2-THE-ACCOUNTING-PROCESS-2-4.pptx
PPTX
chrmotography.pptx food anaylysis techni
PPTX
Business_Capability_Map_Collection__pptx
PDF
CS3352FOUNDATION OF DATA SCIENCE _1_MAterial.pdf
PPTX
MBA JAPAN: 2025 the University of Waseda
PDF
Jean-Georges Perrin - Spark in Action, Second Edition (2020, Manning Publicat...
PPT
Image processing and pattern recognition 2.ppt
PPTX
DS-40-Pre-Engagement and Kickoff deck - v8.0.pptx
PPTX
Lesson-01intheselfoflifeofthekennyrogersoftheunderstandoftheunderstanded
PDF
Best Data Science Professional Certificates in the USA | IABAC
PPT
statistic analysis for study - data collection
PPTX
Tapan_20220802057_Researchinternship_final_stage.pptx
PPT
DU, AIS, Big Data and Data Analytics.ppt
PPTX
statsppt this is statistics ppt for giving knowledge about this topic
Systems Analysis and Design, 12th Edition by Scott Tilley Test Bank.pdf
Tetra Pak Index 2023 - The future of health and nutrition - Full report.pdf
FMIS 108 and AISlaudon_mis17_ppt_ch11.pptx
The Data Security Envisioning Workshop provides a summary of an organization...
Loose-Leaf for Auditing & Assurance Services A Systematic Approach 11th ed. E...
New ISO 27001_2022 standard and the changes
CHAPTER-2-THE-ACCOUNTING-PROCESS-2-4.pptx
chrmotography.pptx food anaylysis techni
Business_Capability_Map_Collection__pptx
CS3352FOUNDATION OF DATA SCIENCE _1_MAterial.pdf
MBA JAPAN: 2025 the University of Waseda
Jean-Georges Perrin - Spark in Action, Second Edition (2020, Manning Publicat...
Image processing and pattern recognition 2.ppt
DS-40-Pre-Engagement and Kickoff deck - v8.0.pptx
Lesson-01intheselfoflifeofthekennyrogersoftheunderstandoftheunderstanded
Best Data Science Professional Certificates in the USA | IABAC
statistic analysis for study - data collection
Tapan_20220802057_Researchinternship_final_stage.pptx
DU, AIS, Big Data and Data Analytics.ppt
statsppt this is statistics ppt for giving knowledge about this topic

daa unit 1.pptx

  • 1. ✓ Looping Outline ▪ Introduction to Algorithm • Definition • Characteristics • Types • Simple Multiplication Methods ▪ Mathematics for Algorithmic Sets • Set Theory • Functions and Relations • Vectors and Matrices • Linear Inequalities and Linear Equations • Logic and Quantifiers
  • 3. 3 What is an Algorithm? �A step-by-step procedure, to solve the different kinds of problems. �Suppose, we want to make a Chocolate Cake. �An unambiguous sequence of computational steps that transform the input into the output. Outpu t Input Process Ingredients Recipe Cake
  • 4. 4 What is an Algorithm? �A process or a set of rules to be followed to achieve desired output, especially by a computer. �An algorithm is any well-defined computational procedure that takes some value, or a set of values as input and produces some value, or a set of values as output. Outpu t Algorithm Program Input
  • 5. 5 Characteristics of An Algorithm �Finiteness: An algorithm must always terminate after a finite number of steps. �Definiteness: Each step of an algorithm must be precisely defined. �Input: An algorithm has zero or more inputs. �Output: An algorithm must have at least one desirable output. �Effectiveness: All the operations to be performed in the algorithm must be sufficiently basic so that they can, in principle be done exactly and in a finite length of time.
  • 6. 6 Types of Algorithm �Simple recursive algorithms �Backtracking algorithms �Divide and conquer algorithms �Dynamic programming algorithms �Greedy algorithms �Branch and bound algorithms �Brute force algorithms �Randomized algorithms
  • 7. ✓ 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
  • 9. 9 Introduction What is Analysis of an Algorithm? ✔ Analyzing an algorithm means calculating/predicting the resources that the algorithm requires. ✔ Analysis provides theoretical estimation for the required resources of an algorithm to solve a specific computational problem. ✔ Two most important resources are computing time (time complexity) and storage space (space complexity). Why Analysis is required? ✔ By analyzing some of the candidate algorithms for a problem, the most efficient one can be easily identified.
  • 10. 10 Efficiency of Algorithm �The efficiency of an algorithm is a measure of the amount of resources consumed in solving a problem of size 𝑛. �An algorithm must be analyzed to determine its resource usage. �Two major computational resources are execution time and memory space. �Memory Space requirement can not be compared directly, so the important resource is computational time required by an algorithm. �To measure the efficiency of an algorithm requires to measure its execution time using any of the following approaches: 1. Empirical Approach: To run it and measure how much processor time is needed. 2. Theoretical Approach: Mathematically computing how much time is needed as a function of input size.
  • 11. 11 How Analysis is Done? Theoretical (priori) approach Empirical (posteriori) approach ▪ Programming different competing techniques & running them on various inputs using computer. ▪ Implementation of different techniques may be difficult. ▪ The same hardware and software environments must be used for comparing two algorithms. ▪ Results may not be indicative of the running time on other inputs not included in the experiment.
  • 12. 12 Time Complexity �Time complexity of an algorithm quantifies the amount of time taken by an algorithm to run as a function of the length of the input. �Running time of an algorithm depends upon, 1. Input Size 2. Nature of Input �Generally time grows with the size of input, for example, sorting 100 numbers will take less time than sorting of 10,000 numbers. �So, running time of an algorithm is usually measured as a function of input size. �Instead of measuring actual time required in executing each statement in the code, we consider how many times each statement is executed. �So, in theoretical computation of time complexity, running time is measured in terms of number of steps/primitive operations performed.
  • 14. 14 Linear Search �Suppose, you are given a jar containing some business cards. �You are asked to determine whether the name “Bill Gates" is in the jar. �To do this, you decide to simply go through all the cards one by one. �How long this takes? �Can be determined by how many cards are in the jar, i.e., Size of Input. �Linear search is a method for finding a particular value from the given list. �The algorithm checks each element, one at a time and in sequence, until the desired element is found. �Linear search is the simplest search algorithm. �It is a special case of brute-force search.
  • 15. 15 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
  • 16. 17 Linear Search - Analysis �The required element in the given array can be found at, Best Case Average Case Worst Case Case 1: element 2 which is at the first position so minimum comparison is required Case 2: element 3 anywhere after the first position so, an average number of comparison is required Case 3: element 7 at last position or element does not found at all, maximum comparison is required Best Case Average Case Worst Case
  • 17. 18 Analysis of Algorithm Best Case Average Case Worst Case Resource usage is minimum Resource usage is average Resource usage is maximum Algorithm’s behavior under optimal condition Algorithm’s behavior under random condition Algorithm’s behavior under the worst condition Minimum number of steps or operations Average number of steps or operations Maximum number of steps or operations Lower bound on running time Average bound on running time Upper bound on running time Generally do not occur in real Average and worst-case performances are the most used in algorithm analysis.
  • 18. 19 ▪ Suppose, you are writing a program to find a book from the shelf. ▪ For any required book, it will start checking books one by one from the bottom. ▪ If you wanted Harry Potter 3, it would only take 3 actions (or tries) because it’s the third book in the sequence. ▪ If Harry Potter 7 — it’s the last book so it would have to check all 7 books. ▪ What if there are total 10 books? How about 10,00,000 books? It would take 1 million tries. Book Finder Example
  • 19. 21 Number Sorting - Example �Suppose you are sorting numbers in Ascending / Increasing order. �The initial arrangement of given numbers can be in any of the following three orders. Case 1: Numbers are already in required order, i.e., Ascending order No change is required Case 2: Numbers are randomly arranged initially. Some numbers will change their position Case 3: Numbers are initially arranged in Descending order so, all numbers will change their position Best Case Average Case Worst Case
  • 20. 22 Best, Average, & Worst Case Problem Best Case Average Case Worst Case Linear Search Element at the first position Element in any of the middle positions Element at last position or not present Book Finder The first book Any book in- between The last book Sorting Already sorted Randomly arranged Sorted in reverse order
  • 22. 24 Introduction �The theoretical (priori) approach of analyzing an algorithm to measure the efficiency does not depend on the implementation of the algorithm. �In this approach, the running time of an algorithm is describes as Asymptotic Notations. �Computing the running time of algorithm’s operations in mathematical units of computation and defining the mathematical formula of its run-time performance is referred to as Asymptotic Analysis. �An algorithm may not have the same performance for different types of inputs. With the increase in the input size, the performance will change. �Asymptotic analysis accomplishes the study of change in performance of the algorithm with the change in the order of the input size. �Using Asymptotic analysis, we can very well define the best case, average case, and worst case scenario of an algorithm.
  • 25. 27
  • 28. 30
  • 30. 32
  • 31. 33 Asymptotic Notations 1. O-Notation (Big O notation) (Upper Bound) 1. Ω-Notation (Omega notation) (Lower Bound) 1. θ-Notation (Theta notation) (Same order)
  • 32. 34 Asymptotic Notations – Examples � Algo. 1 running time Algo. 2 running time Algo. 1 running time Algo. 2 running time 1 2 3 4 5 1 2 3 4 5 1 4 9 16 25 1 2 3 4 5 1 4 9 16 25 1 2 3 4 5
  • 33. 35 Asymptotic Notations – Examples � 1 2 3 4 5 6 7 1 4 9 16 25 36 49 2 4 8 16 32 64 128
  • 34. 36 Asymptotic Notations – Examples f(n)=30n+8 Increasing n → g (n)=n2+1 Value of function →
  • 35. 37 Common Orders of Magnitude 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
  • 37. 39 Asymptotic Notations in Equations � Negligible 𝑂( 𝑓(𝑛)+𝑔(𝑛))=𝑂(max(𝑓(𝑛),𝑔(𝑛)))
  • 38. 40 Asymptotic Notations 1. O-Notation (Big O notation) (Upper Bound) 1. Ω-Notation (Omega notation) (Lower Bound) 1. θ-Notation (Theta notation) (Same order)
  • 43. 45 For Loop # Input : int A[n], array of n integers # Output : Sum of all numbers in array A Algorithm: int Sum(int A[], int n) { int s=0; for (int i=0; i<n; i++) s = s + A[i]; return s; } 1 n+1 n Total time taken = n+1+n+2 = 2n+3 Time Complexity f(n) = 2n+3
  • 44. 46 Running Time of Algorithm �
  • 45. 47 Analyzing Control Statements Example 1: Example 2: Example 3:
  • 46. 48 Analyzing Control Statements Example 6: Example 4: Example 5:
  • 47. Sorting Algorithms Bubble Sort, Selection Sort, Insertion Sort
  • 48. 50 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.
  • 49. 51 Bubble Sort – Example 45 34 56 23 12 Sort the following array in Ascending order Pass 1 : 45 34 56 23 12 34 45 56 23 12 34 45 56 23 12 34 45 23 56 12 34 45 swa p swa p 23 56 swa p 12 56
  • 50. 52 Bubble Sort – Example Pass 2 : 34 45 23 12 56 34 45 23 12 56 34 23 45 12 56 34 23 12 45 56 swa p 23 45 swa p 12 45 Pass 3 : 23 34 12 45 56 23 12 34 45 56 swa p 23 34 swa p 12 34 Pass 4 : swa p 12 23
  • 51. 53 Bubble Sort - Algorithm # Input: Array A # Output: Sorted array A 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 swap(A[j], A[j+1]) temp ← A[j] A[j] ← A[j+1] A[j+1] ← temp
  • 53. 55 Bubble Sort Algorithm – Best Case Analysis # Input: Array A # Output: Sorted array A Algorithm: Bubble_Sort(A) int flag=1; for i ← 1 to n-1 do for j ← 1 to n-i do if A[j] > A[j+1] then flag = 0; swap(A[j],A[j+1]) if(flag == 1) cout<<"already sorted"<<endl break; Pass 1 : 34 45 59 12 23 i = 1 j = 1 j = 2 j = 3 j = 4 Condition never becomes true
  • 54. 58 Selection Sort – Example 1 5 1 12 -5 16 2 12 14 Step 1 : 5 1 12 -5 16 2 12 14 Unsorted Array 1 2 3 4 5 6 7 8 Step 2 : 5 1 12 -5 16 2 12 14 1 2 3 4 5 6 7 8 ▪ Minj denotes the current index and Minx is the value stored at current index. ▪ So, Minj = 1, Minx = 5 ▪ Assume that currently Minx is the smallest value. ▪ Now find the smallest value from the remaining entire Unsorted array. Unsorted Array (elements 2 to 8) Swap -5 5 Index = 4, value = -5 Sort the following elements in Ascending order
  • 55. 59 Selection Sort – Example 1 Step 3 : -5 1 12 5 16 2 12 14 1 2 3 4 5 6 7 8 Unsorted Array (elements 3 to 8) ▪ Now Minj = 2, Minx = 1 ▪ Find min value from remaining unsorted array No Swapping as min value is already at right place 1 Step 4 : -5 1 12 5 16 2 12 14 1 2 3 4 5 6 7 8 Unsorted Array (elements 4 to 8) Swap 2 12 ▪ Minj = 3, Minx = 12 ▪ Find min value from remaining unsorted array Index = 2, value = 1 Index = 6, value = 2
  • 56. 60 Selection Sort – Example 1 Step 5 : Step 6 : -5 1 2 5 16 12 12 14 1 2 3 4 5 6 7 8 -5 1 2 5 16 12 12 14 1 2 3 4 5 6 7 8 Swap Unsorted Array (elements 5 to 8) 5 Unsorted Array (elements 6 to 8) 12 16 ▪ Now Minj = 4, Minx = 5 ▪ Find min value from remaining unsorted array No Swapping as min value is already at right place ▪ Minj = 5, Minx = 16 ▪ Find min value from remaining unsorted array Index = 4, value = 5 Index = 6, value = 12
  • 57. 61 Selection Sort – Example 1 Step 7 : Step 8 : -5 1 2 5 12 16 12 14 1 2 3 4 5 6 7 8 Swap 12 16 Unsorted Array (elements 7 to 8) -5 1 2 5 12 12 16 14 1 2 3 4 5 6 7 8 Swap 14 16 Unsorted Array (element 8) ▪ Now Minj = 6, Minx = 16 ▪ Find min value from remaining unsorted array ▪ Minj = 7, Minx = 16 ▪ Find min value from remaining unsorted array Index = 7, value = 12 Index = 8, value = 14 The entire array is sorted now.
  • 59. 63 Selection Sort - Algorithm # Input: Array A # Output: Sorted array A 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;
  • 60. 64 Selection Sort – Example 2 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 Sort in Ascending order i = 1 minj ← 1 minx ← 45 j = 2 Pass 1 : 1 2 3 4 5 45 34 2 3 34 A[j] = 34 56 No Change
  • 61. 65 Selection Sort – Example 2 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 Sort in Ascending order i = 1 minj ← 2 minx ← 34 j = 2 Pass 1 : 1 2 3 4 5 45 23 4 3 12 A[j] = 23 12 4 5 5 45 34 56 23 12 1 2 3 4 5 45 12 Unsorted Array Swap 45 34 56 23 45 12 23 34 56 45 56 34
  • 62. 67 Insertion Sort – Example 5 1 12 -5 16 2 12 14 Step 1 : 5 1 12 -5 16 2 12 14 Unsorted Array 1 2 3 4 5 6 7 8 Step 2 : 1 2 3 4 5 6 7 8 Shift down Sort the following elements in Ascending order 5 1 12 -5 16 2 12 14 1
  • 63. 68 Insertion Sort – Example Step 3 : Step 4 : 1 2 3 4 5 6 7 8 1 5 12 -5 16 2 12 14 1 2 3 4 5 6 7 8 No Shift will take place 1 5 12 -5 16 2 12 14 -5 Shift down Shift down Shift down
  • 64. 69 Insertion Sort – Example 1 2 3 4 5 6 7 8 Shift down -5 1 5 12 16 2 12 14 1 2 3 4 5 6 7 8 No Shift will take place -5 1 5 12 16 2 12 14 2 Shift down Shift down Step 5 : Step 6 :
  • 65. 70 Insertion Sort – Example Step 7 : Step 8 : 1 2 3 4 5 6 7 8 Shift down -5 1 2 5 12 12 16 14 1 2 3 4 5 6 7 8 -5 1 2 5 12 16 12 14 Shift down 1 2 14 The entire array is sorted now.
  • 66. 71 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;
  • 67. 72 Insertion Sort Algorithm – Best Case Analysis # 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; 34 45 59 12 23 x=23 T[j]=23 T[j]=34 T[j]=45 i=2 i=3 i=4 i=5 Pass 1 : T[j]=12 x=34 x=45 x=59
  • 68. Heap & Heap Sort Algorithm
  • 69. 74 Introduction �A heap data structure is a binary tree with the following two properties. 1. It is a complete binary tree: Each level of the tree is completely filled, except possibly the bottom level. At this level it is filled from left to right. 2. It satisfies the heap order property: the data item stored in each node is greater than or equal to the data item stored in its children node. a b c d e f Binary Tree but not a Heap Complete Binary Tree - Heap a b c d e f 9 6 7 2 4 8 Not a Heap 9 6 7 2 4 1 Heap
  • 70. 75 Array Representation of Heap �Heap can be implemented using an Array. �An array 𝐴 that represents a heap is an object with two attributes: 1. 𝑙𝑒𝑛𝑔𝑡ℎ[𝐴], which is the number of elements in the array, and 2. ℎ𝑒𝑎𝑝−𝑠𝑖𝑧𝑒[𝐴], the number of elements in the heap stored within array 𝐴 16 14 10 8 7 9 3 2 4 1 16 14 10 8 7 9 3 2 4 1 Array representation of heap Heap
  • 71. 76 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 𝟐𝒋+𝟏 16 14 10 8 7 9 3 2 4 1 16 14 10 8 7 9 3 2 4 1 Heap 14 8 2 4
  • 72. 77 Types of Heap 1. Max-Heap − Where the value of the root node is greater than or equal to either of its children. 9 6 7 2 4 1 2. Min-Heap − Where the value of the root node is less than or equal to either of its children. 1 2 4 6 7 9
  • 73. 78 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.
  • 74. 79 Heap Sort – Example 1 4 10 3 5 1 Sort the following elements in Ascending order 4 10 3 5 1 Step 1 : Create Complete Binary Tree 4 10 3 5 1 Now, a binary tree is created and we have to convert it into a Heap.
  • 75. 80 Heap Sort – Example 1 4 10 3 5 1 Sort the following elements in Ascending order 4 10 3 5 1 4 10 3 5 1 Step 2 : Create Max Heap In a Max Heap, parent node is always greater than or equal to the child nodes. 10 is greater than 4 So, swap 10 & 4 Swap 10 4 10 4
  • 76. 81 Heap Sort – Example 1 4 10 3 5 1 Sort the following elements in Ascending order 10 4 3 5 1 10 4 3 5 1 In a Max Heap, parent node is always greater than or equal to the child nodes. 5 is greater than 4 So, swap 5 & 4 Swap 4 5 Max Heap is created Step 2 : Create Max Heap 5 4
  • 77. 82 Heap Sort – Example 1 4 10 3 5 1 Sort the following elements in Ascending order 10 5 3 4 1 10 5 3 4 1 1. Swap the first and the last nodes and 2. Delete the last node. Step 3 : Apply Heap Sort Swap 10 1 1 10
  • 78. 83 Heap Sort – Example 1 4 10 3 5 1 Sort the following elements in Ascending order 1 5 3 4 10 1 5 3 4 Step 3 : Apply Heap Sort Max Heap Property is violated so, create a Max Heap again. 1 5 1 Swap 4 5 1 1 4
  • 79. 84 Heap Sort – Example 1 4 10 3 5 1 Sort the following elements in Ascending order 5 4 3 1 10 5 4 3 1 Step 3 : Apply Heap Sort Max Heap is created 1. Swap the first and the last nodes and 2. Delete the last node. Swap 1 5 1 5
  • 80. 85 Heap Sort – Example 1 4 10 3 5 1 Sort the following elements in Ascending order 1 4 3 5 10 1 4 3 Step 3 : Apply Heap Sort Create Max Heap again 1. Swap the first and the last nodes and 2. Delete the last node. 1 4 Swap Max Heap is created 4 4 1 3 4 3
  • 81. 86 Heap Sort – Example 1 4 10 3 5 1 Sort the following elements in Ascending order 3 1 4 5 10 3 1 Step 3 : Apply Heap Sort Already a Max Heap 1. Swap the first and the last nodes and 2. Delete the last node. Swap 1 3 3 1
  • 82. 87 Heap Sort – Example 1 4 10 3 5 1 Sort the following elements in Ascending order 1 3 4 5 10 1 Step 3 : Apply Heap Sort Already a Max Heap Remove the last element from heap and the sorting is over. 1
  • 83. 88 Heap Sort – Example 2 �Sort given element in ascending order using heap sort. 19, 7, 16, 1, 14, 17 19 7 16 1 14 17 19 7 16 1 14 17 Step 2: Create Max-heap Step 1: Create binary tree 19 7 16 1 14 17 16 17 7 14 16 17 14 7
  • 84. 89 Heap Sort – Example 2 19 14 17 1 7 16 19 14 17 1 7 16 16 14 17 1 7 16 14 17 1 7 19 Swap & remove the last element Create Max-heap 16 19 16 19 16 17 16 17 Step 3 Step 4
  • 85. 90 Heap Sort – Example 2 17 14 16 1 7 17 14 16 1 7 19 7 14 16 1 7 14 16 1 17 19 Swap & remove the last element Create Max-heap 7 17 17 7 16 7 16 7 Step 5 Step 6
  • 86. 91 Heap Sort – Example 2 16 14 7 1 16 14 7 1 17 19 1 14 7 1 14 7 16 17 19 Swap & remove the last element Create Max-heap 16 1 16 1 14 1 14 1 Step 7 Step 8
  • 87. 92 Heap Sort – Example 2 14 1 7 14 1 7 16 17 19 7 1 7 1 14 16 17 19 Swap & remove the last element Already a Max-heap 14 7 14 7 7 1 7 1 Swap & remove the last element 1 7 14 16 17 19 1 Remove the last element 1 7 14 16 17 19 The entire array is sorted now. Step 9 Step 10 Step 11
  • 88. 93 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
  • 90. 95 Heap Sort – Algorithm �
  • 91. 96 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) 4 1 3 2 9 7 heap-size[A] = 6 i = 3 4 1 3 2 9 7 1 2 3 4 5 6 4 1 3 2 9 7 1 2 3 4 5 6 4 1 7 2 9 3 1 2 3 4 5 6 4 9 7 2 1 3 1 2 3 4 5 6 i = 2 i = 1 4 1 7 2 9 3 4 9 7 2 1 3 9 4 7 2 1 3 7 3 9 1 9 4
  • 92. 97 Heap Sort – Algorithm � 9 4 7 2 1 3 1 2 3 4 5 6 9 4 7 2 1 3 3 4 7 2 1 9
  • 93. 98 Heap Sort – Algorithm � 1 Yes Yes Yes 3 4 7 2 1 1 2 3 4 5 3 4 7 2 1 9
  • 94. 99 Heap Sort – Algorithm � 7 4 3 2 1 1 2 3 4 5 3 4 7 2 1 9
  • 95. 100 Heap Sort Algorithm – Analysis � heap-size[A] ← length[A] for i ← ⌊length[A]/2⌋ downto 1 do MAX-HEAPIFY(A, i)
  • 96. Sorting Algorithms Shell Sort, Radix Sort, Bucket Sort, Counting Sort
  • 97. 102 Shell Sort - Example �Sort the following elements in ascending order using shell sort. 80 93 60 12 42 30 68 85 10 80 93 60 12 42 30 68 85 10 10 93 60 12 42 30 68 85 80 Step 1: Divide input array into segments, where Initial Segmenting Gap = 4 (n/2) Each segment is sorted within itself using insertion sort This algorithm avoids large shifts as in case of insertion sort, if the smaller value is to the far right and has to be moved to the far left.
  • 98. 103 Shell Sort - Example �Sort the following elements in ascending order using shell sort. 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 10 30 60 12 42 93 68 85 80 10 30 60 12 42 93 68 85 80 Each segment is sorted within itself using insertion sort
  • 99. 104 Shell Sort - Example �Sort the following elements in ascending order using shell sort. 80 93 60 12 42 30 68 85 10 Step 2: Now, the Segmenting Gap = 2 (4/2) Each segment is sorted within itself using insertion sort 10 30 60 12 42 93 68 85 80 10 30 42 12 60 93 68 85 80 10 12 42 30 60 85 68 93 80
  • 100. 105 Shell Sort - Example �Sort the following elements in ascending order using shell sort. 80 93 60 12 42 30 68 85 10 Step 3: Now, the Segmenting Gap = 1 (2/2) Each segment is sorted within itself using insertion sort 10 12 42 30 60 85 68 93 80 10 12 30 42 60 68 80 85 93 The entire array is sorted now.
  • 101. 106 Shell Sort - Procedure �
  • 102. 107 Radix Sort �Radix Sort puts the elements in order by comparing the digits of the numbers. �Each element in the 𝒏-element array 𝑨 has 𝒅 digits, where digit 1 is the lowest-order digit and digit 𝑑 is the highest order digit. �Sort following elements in Ascending order using radix sort. 363, 729, 329, 873, 691, 521, 435, 297 Algorithm: RADIX-SORT(A, d) for i ← 1 to d do use a stable sort to sort array A on digit i
  • 103. 108 Radix Sort - Example 3 6 3 7 2 9 3 2 9 8 7 3 6 9 1 5 2 1 4 3 5 2 9 7 Sort on column 1 6 9 1 5 2 1 3 6 3 8 7 3 4 3 5 2 9 7 7 2 9 3 2 9 Sort on column 2 5 2 1 7 2 9 3 2 9 4 3 5 3 6 3 8 7 3 6 9 1 2 9 7 Sort on column 3 The entire array is sorted now.
  • 104. 109 Bucket Sort – Introduction � 45 96 29 30 27 12 39 61 91
  • 105. 110 Bucket Sort – Example 45 96 29 30 27 12 39 61 91 0 4 1 9 3 5 7 8 2 6 45 96 29 30 27 12 39 61 91 Sort each bucket queue with insertion sort 27 29 91 96 Merge all bucket queues together in order 45 96 29 30 27 12 39 61 91 12 27 29 30 39 45 61 91 96
  • 106. 111 Bucket Sort - Algorithm # Input: Array A # Output: Sorted array A Algorithm: Bucket-Sort(A[1,…,n]) n ← length[A] for i ← 1 to n do insert A[i] into bucket B[⌊A[i] ÷ n ⌋] for i ← 0 to n – 1 do sort bucket B[i] with insertion sort concatenate the buckets B[0], B[1], . . ., B[n - 1] together in order.
  • 107. 112 Counting Sort – Example �Sort the following elements in Ascending order using counting sort. 3 6 4 1 3 4 1 4 2 Step 1 Index Elements Step 2 0 0 0 0 0 0 Index Elements 3 6 4 1 3 4 1 4 2
  • 108. 113 Counting Sort – Example �Sort the following elements in Ascending order using counting sort. 3 6 4 1 3 4 1 4 2 Step 3 Index Elements Step 4 Update an array C with the occurrences of each value of array 𝐴 In array 𝐶, from index 2 to 𝑛 add the value with previous element 2 3 5 8 8 9 Index Elements 0 0 0 0 0 0 2 1 2 3 1 0 + +
  • 109. 114 Counting Sort – Example �Create an output array 𝐵[1…9]. Start positioning elements of Array 𝐴 𝑡𝑜 𝐵 as shown below. 3 6 4 1 3 4 1 4 2 2 3 5 8 8 9 Temporary Array C Output Array B 1 1 2 3 3 4 4 4 6 2 7 1 6 4 0 5 8 3
  • 110. 115 Counting Sort - Procedure �Counting sort assumes that each of the 𝑛 input elements is an integer in the range 0 to 𝑘, for some integer 𝑘. �When 𝒌=𝑶(𝒏), the counting sort runs in 𝜽(𝒏) time. �The basic idea of counting sort is to determine, for each input element 𝑥, the number of elements less than 𝑥. �This information can be used to place element 𝑥 directly into its position in the output array.
  • 111. 116 Counting Sort - Algorithm # Input: Array A # Output: Sorted array A Algorithm: Counting-Sort(A[1,…,n], B[1,…,n], k) for i ← 1 to k do c[i] ← 0 for j ← 1 to n do c[A[j]] ← c[A[j]] + 1 for i ← 2 to k do c[i] ← c[i] + c[i-1] for j ← n downto 1 do B[c[A[j]]] ← A[j] c[A[j]] ← c[A[j]] - 1
  • 113. 118 Introduction �Amortized analysis considers not just one operation, but a sequence of operations on a given data structure or a database. �Amortized Analysis is used for algorithms where an occasional operation is very slow, but most of the other operations are faster. �The time required to perform a sequence of data structure operations is averaged over all operations performed. �In Amortized Analysis, we analyze a sequence of operations and guarantee a worst case average time which is lower than the worst case time of a particular expensive operation. �So, Amortized analysis can be used to show that the average cost of an operation is small even though a single operation might be expensive.
  • 114. 119 Amortized Analysis Techniques �There are three most common techniques of amortized analysis, 1. The aggregate method ▪ A sequence of 𝑛 operation takes worst case time 𝑇(𝑛) ▪ Amortized cost per operation is 𝑇(𝑛)/𝑛 2. The accounting method ▪ Assign each type of operation an (different) amortized cost ▪ Overcharge some operations ▪ Store the overcharge as credit on specific objects ▪ Then use the credit for compensation for some later operations 3. The potential method ▪ Same as accounting method ▪ But store the credit as “potential energy” and as a whole.
  • 115. 120 Amortized Analysis - Example 0 Counter value [7] [6] [5] [4] [3] [2] [1] [0] Increment cost Total cost 0 0 0 0 1 0 1 0 0 0 0 1 0 1 0 0 0 0 0 1 1 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 9 10 6 7 8 3 4 5 0 1 2 1 2 2 1 4 1 3 1 1 2 16 18 10 11 15 4 7 8 1 3 0 0 0 0 1 0 1 11 1 19 0 0 1 1 0 0 1 1 0 0 1 0 +
  • 116. 121 Amortized Analysis - Example 0 Counter value [7] [6] [5] [4] [3] [2] [1] [0] Increment cost Total cost 0 0 0 0 1 0 1 0 0 0 0 1 0 1 0 0 0 0 0 1 1 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 9 10 6 7 8 3 4 5 0 1 2 1 2 2 1 4 1 3 1 1 2 16 18 10 11 15 4 7 8 1 3 0 0 0 0 1 0 1 11 1 19 0 0 1 1 0 0 1 1 0 0 1 0
  • 117. 122 Aggregate Method � Counter value Number of bit flips 0 0 0 0 0 0 1 0 0 0 1 1 2 0 0 1 0 2 3 0 0 1 1 1 4 0 1 0 0 3 5 0 1 0 1 1 6 0 1 1 0 2 7 0 1 1 1 1 8 1 0 0 0 4 Total Flips = 15
  • 119. 125 Amortized Analysis - Example 0 Counter value [7] [6] [5] [4] [3] [2] [1] [0] Increment cost Total cost 0 0 0 0 1 0 1 0 0 0 0 1 0 1 0 0 0 0 0 1 1 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 9 10 6 7 8 3 4 5 0 1 2 1 2 2 1 4 1 3 1 1 2 16 18 10 11 15 4 7 8 1 3 0 0 0 0 1 0 1 11 1 19 0 0 1 1 0 0 1 1 0 0 1 0 Running time of an increment operation is proportional to the number of bits flipped.