SlideShare a Scribd company logo
Algorithms
Dr. AMIT KUMAR @JUET
Introduction
• The methods of algorithm design form one of the core
practical technologies of computer science.
• The main aim of this lecture is to familiarize the student
with the framework we shall use through the course
about the design and analysis of algorithms.
• We start with a discussion of the algorithms needed to
solve computational problems. The problem of sorting is
used as a running example.
• We introduce a pseudocode to show how we shall
specify the algorithms.
Dr. AMIT KUMAR @JUET
Algorithms
• The word algorithm comes from the name of a Persian
mathematician Abu Ja’far Mohammed ibn-i Musa al
Khowarizmi.
• In computer science, this word refers to a special
method useable by a computer for solution of a problem.
The statement of the problem specifies in general terms
the desired input/output relationship.
• For example, sorting a given sequence of numbers into
nondecreasing order provides fertile ground for
introducing many standard design techniques and
analysis tools.
Dr. AMIT KUMAR @JUET
The problem of sorting
Dr. AMIT KUMAR @JUET
Insertion Sort
Dr. AMIT KUMAR @JUET
Example of Insertion Sort
Dr. AMIT KUMAR @JUET
Example of Insertion Sort
Dr. AMIT KUMAR @JUET
Example of Insertion Sort
Dr. AMIT KUMAR @JUET
Example of Insertion Sort
Dr. AMIT KUMAR @JUET
Example of Insertion Sort
Dr. AMIT KUMAR @JUET
Example of Insertion Sort
Dr. AMIT KUMAR @JUET
Example of Insertion Sort
Dr. AMIT KUMAR @JUET
Example of Insertion Sort
Dr. AMIT KUMAR @JUET
Example of Insertion Sort
Dr. AMIT KUMAR @JUET
Example of Insertion Sort
Dr. AMIT KUMAR @JUET
Example of Insertion Sort
Dr. AMIT KUMAR @JUET
Analysis of algorithms
The theoretical study of computer-program
performance and resource usage.
What’s more important than performance?
• modularity
• correctness
• maintainability
• functionality
• robustness
• user-friendliness
• programmer time
• simplicity
• extensibility
• reliabilityDr. AMIT KUMAR @JUET
Analysis of algorithms
Why study algorithms and performance?
• Algorithms help us to understand scalability.
• Performance often draws the line between what is feasible
and what is impossible.
• Algorithmic mathematics provides a language for talking
about program behavior.
• The lessons of program performance generalize to other
computing resources.
• Speed is fun!
Dr. AMIT KUMAR @JUET
Running Time
• The running time depends on the input: an already
sorted sequence is easier to sort.
• Parameterize the running time by the size of the
input, since short sequences are easier to sort than
long ones.
• Generally, we seek upper bounds on the running
time, because everybody likes a guarantee.
Dr. AMIT KUMAR @JUET
Kinds of analyses
Worst-case: (usually)
• T(n) = maximum time of algorithm on any input of
size n.
Average-case: (sometimes)
• T(n) = expected time of algorithm over all inputs of
size n.
• Need assumption of statistical distribution of inputs.
Best-case:
• Cheat with a slow algorithm that works fast on some
input.
Dr. AMIT KUMAR @JUET
Machine-Independent time
The RAM Model
Machine independent algorithm design depends on a
hypothetical computer called Random Acces Machine (RAM).
Assumptions:
• Each simple operation such as +, -, if ...etc takes exactly
one time step.
• Loops and subroutines are not considered simple
operations.
• Each memory acces takes exactly one time step.
Dr. AMIT KUMAR @JUET
Machine-independent time
What is insertion sort’s worst-case time?
• It depends on the speed of our computer,
• relative speed (on the same machine),
• absolute speed (on different machines).
BIG IDEA:
• Ignore machine-dependent constants.
• Look at growth of
“Asymptotic Analysis”
nnT as)(
Dr. AMIT KUMAR @JUET
Machine-independent time: An example
A pseudocode for insertion sort ( INSERTION SORT ).
INSERTION-SORT(A)
1 for j  2 to length [A]
2 do key  A[ j]
3  Insert A[j] into the sortted sequence A[1,..., j-1].
4 i  j – 1
5 while i > 0 and A[i] > key
6 do A[i+1]  A[i]
7 i  i – 1
8 A[i +1]  key
Dr. AMIT KUMAR @JUET
Analysis of INSERTION-SORT(contd.)
1]1[8
)1(17
)1(][]1[6
][05
114
10]11[sequence
sortedtheinto][Insert3
1][2
][21
timescostSORT(A)-INSERTION
8
27
26
25
4
2
1















nckeyiA
tcii
tciAiA
tckeyiAandi
ncji
njA
jA
ncjAkey
ncAlengthj
n
j j
n
j j
n
j j
do
while
do
tofor
Dr. AMIT KUMAR @JUET
Analysis of INSERTION-SORT(contd.)
)1()1()1()(
2
6
2
5421  

n
j
j
n
j
j tctcncnccnT
).1()1( 8
2
7  

nctc
n
j
j
The total running time is
Dr. AMIT KUMAR @JUET
Analysis of INSERTION-SORT(contd.)
The best case: The array is already sorted.
(tj =1 for j=2,3, ...,n)
)1()1()1()1()( 85421  ncncncncncnT
).()( 854285421 ccccnccccc 
Dr. AMIT KUMAR @JUET
Analysis of INSERTION-SORT(contd.)
•The worst case: The array is reverse sorted
(tj =j for j=2,3, ...,n).
)12/)1(()1()( 521  nncncncnT
)1()2/)1(()2/)1(( 876  ncnncnnc
ncccccccnccc )2/2/2/()2/2/2/( 8765421
2
765 
2
)1(
1



nn
j
n
j
cbnannT  2
)(
Dr. AMIT KUMAR @JUET
Growth of Functions
Although we can sometimes determine the exact
running time of an algorithm, the extra precision is not
usually worth the effort of computing it.
For large inputs, the multiplicative constants and
lower order terms of an exact running time are
dominated by the effects of the input size itself.
Dr. AMIT KUMAR @JUET
Asymptotic Notation
The notation we use to describe the asymptotic running
time of an algorithm are defined in terms of functions
whose domains are the set of natural numbers
 ...,2,1,0N
Dr. AMIT KUMAR @JUET
O-notation
• For a given function , we denote by the set
of functions
• We use O-notation to give an asymptotic upper bound of
a function, to within a constant factor.
• means that there existes some constant c
s.t. is always for large enough n.
)(ng ))(( ngO








0
0
allfor)()(0
s.t.andconstantspositiveexistthere:)(
))((
nnncgnf
ncnf
ngO
))(()( ngOnf 
)(ncg)(nf
Dr. AMIT KUMAR @JUET
Ω-Omega notation
• For a given function , we denote by the
set of functions
• We use Ω-notation to give an asymptotic lower bound on
a function, to within a constant factor.
• means that there exists some constant c s.t.
is always for large enough n.
)(ng ))(( ng








0
0
allfor)()(0
s.t.andconstantspositiveexistthere:)(
))((
nnnfncg
ncnf
ng
))(()( ngnf 
)(nf )(ncg
Dr. AMIT KUMAR @JUET
-Theta notation
• For a given function , we denote by the set
of functions
• A function belongs to the set if there exist
positive constants and such that it can be “sand-
wiched” between and or sufficienly large n.
• means that there exists some constant c1
and c2 s.t. for large enough n.
)(ng ))(( ng








021
021
allfor)()()(c0
s.t.and,,constantspositiveexistthere:)(
))((
nnngcnfng
nccnf
ng
)(nf ))(( ng
1c 2c
)(1 ngc )(2 ngc
Θ
))(()( ngnf 
)()()( 21 ngcnfngc 
Dr. AMIT KUMAR @JUET
Asymptotic notation
Graphic examples of and . ,, O
Dr. AMIT KUMAR @JUET
2
2
22
1 3
2
1
ncnnnc 
21
3
2
1
c
n
c 
Example 1.
Show that
We must find c1 and c2 such that
Dividing bothsides by n2 yields
For
)(3
2
1
)( 22
nnnnf 
)(3
2
1
,7 22
0 nnnn 
Dr. AMIT KUMAR @JUET
Theorem
• For any two functions and , we have
if and only if
)(ng
))(()( ngnf 
)(nf
)).(()(and))(()( ngnfngOnf 
Dr. AMIT KUMAR @JUET
Because :
)2(5223 nnn 
Example 2.
)2(5223)( nnnnf 
)2(5223 nOnn 
Dr. AMIT KUMAR @JUET
Example 3.
610033,3forsince)(61003 2222
 nnncnOnn
Dr. AMIT KUMAR @JUET
Example 3.
3when61003,1forsince)(61003
610033,3forsince)(61003
2332
2222


nnnncnOnn
nnncnOnn
Dr. AMIT KUMAR @JUET
Example 3.
cnncncnOnn
nnnncnOnn
nnncnOnn



when3,anyforsince)(61003
3when61003,1forsince)(61003
610033,3forsince)(61003
22
2332
2222
Dr. AMIT KUMAR @JUET
Example 3.
100when610032,2forsince)(61003
when3,anyforsince)(61003
3when61003,1forsince)(61003
610033,3forsince)(61003
2222
22
2332
2222




nnnncnnn
cnncncnOnn
nnnncnOnn
nnncnOnn
Dr. AMIT KUMAR @JUET
Example 3.
3when61003,3forsince)(61003
100when610032,2forsince)(61003
when3,anyforsince)(61003
3when61003,1forsince)(61003
610033,3forsince)(61003
3232
2222
22
2332
2222





nnnncnnn
nnnncnnn
cnncncnOnn
nnnncnOnn
nnncnOnn
Dr. AMIT KUMAR @JUET
Example 3.
100when61003,anyforsince)(61003
3when61003,3forsince)(61003
100when610032,2forsince)(61003
when3,anyforsince)(61003
3when61003,1forsince)(61003
610033,3forsince)(61003
22
3232
2222
22
2332
2222






nnncncnnn
nnnncnnn
nnnncnnn
cnncncnOnn
nnnncnOnn
nnncnOnn
Dr. AMIT KUMAR @JUET
Example 3.
apply.andbothsince)(61003
100when61003,anyforsince)(61003
3when61003,3forsince)(61003
100when610032,2forsince)(61003
when3,anyforsince)(61003
3when61003,1forsince)(61003
610033,3forsince)(61003
22
22
3232
2222
22
2332
2222







Onnn
nnncncnnn
nnnncnnn
nnnncnnn
cnncncnOnn
nnnncnOnn
nnncnOnn
Dr. AMIT KUMAR @JUET
Example 3.
applies.onlysince)(61003
apply.andbothsince)(61003
100when61003,anyforsince)(61003
3when61003,3forsince)(61003
100when610032,2forsince)(61003
when3,anyforsince)(61003
3when61003,1forsince)(61003
610033,3forsince)(61003
32
22
22
3232
2222
22
2332
2222
Onnn
Onnn
nnncncnnn
nnnncnnn
nnnncnnn
cnncncnOnn
nnnncnOnn
nnncnOnn








Dr. AMIT KUMAR @JUET
Example 3.
applies.onlysince)(61003
applies.onlysince)(61003
apply.andbothsince)(61003
100when61003,anyforsince)(61003
3when61003,3forsince)(61003
100when610032,2forsince)(61003
when3,anyforsince)(61003
3when61003,1forsince)(61003
610033,3forsince)(61003
2
32
22
22
3232
2222
22
2332
2222









nnn
Onnn
Onnn
nnncncnnn
nnnncnnn
nnnncnnn
cnncncnOnn
nnnncnOnn
nnncnOnn
Dr. AMIT KUMAR @JUET
Standard notations and common functions
• Floors and ceilings
    11  xxxxx
Dr. AMIT KUMAR @JUET
Standard notations and common functions
• Logarithms:
)lg(lglglg
)(loglog
logln
loglg 2
nn
nn
nn
nn
kk
e




Dr. AMIT KUMAR @JUET
Standard notations and common functions
• Logarithms:
For all real a>0, b>0, c>0, and n
b
a
a
ana
baab
ba
c
c
b
b
n
b
ccc
ab
log
log
log
loglog
loglog)(log
log




Dr. AMIT KUMAR @JUET
Standard notations and common functions
• Logarithms:
b
a
ca
aa
a
b
ac
bb
bb
log
1
log
log)/1(log
loglog



Dr. AMIT KUMAR @JUET
Standard notations and common functions
• Factorials
For the Stirling approximation:



















ne
n
nn
n
1
12!
0n
)lg()!lg(
)2(!
)(!
nnn
n
non
n
n



Dr. AMIT KUMAR @JUET

More Related Content

What's hot (20)

DOC
Branch and bound
Nv Thejaswini
 
PPTX
Ai 8 puzzle problem
Sanad Bhowmik
 
PPTX
Asymptotic Notations
Rishabh Soni
 
PPT
Asymptotic notations
Ehtisham Ali
 
PPTX
Knowledge representation
Dr. Jasmine Beulah Gnanadurai
 
PPTX
Depth first search and breadth first searching
Kawsar Hamid Sumon
 
PPTX
The n Queen Problem
Sukrit Gupta
 
PPT
13. Query Processing in DBMS
koolkampus
 
PPTX
Regular expressions
Ratnakar Mikkili
 
PPT
Binary search tree(bst)
Hossain Md Shakhawat
 
PPTX
Constraint satisfaction Problem Artificial Intelligence
naeembisma
 
PPT
Amortized Analysis of Algorithms
sathish sak
 
PPTX
Problem solving agents
Megha Sharma
 
PPTX
Concurrency Control in Distributed Systems.pptx
MArshad35
 
PPT
Classical Planning
ahmad bassiouny
 
PPTX
Planning
Amar Jukuntla
 
PPT
Asymptotic notation
Dr Shashikant Athawale
 
PPTX
Classical relations and fuzzy relations
Baran Kaynak
 
PPTX
Asymptotic notations
Nikhil Sharma
 
PPT
Operating System: Deadlock
InteX Research Lab
 
Branch and bound
Nv Thejaswini
 
Ai 8 puzzle problem
Sanad Bhowmik
 
Asymptotic Notations
Rishabh Soni
 
Asymptotic notations
Ehtisham Ali
 
Knowledge representation
Dr. Jasmine Beulah Gnanadurai
 
Depth first search and breadth first searching
Kawsar Hamid Sumon
 
The n Queen Problem
Sukrit Gupta
 
13. Query Processing in DBMS
koolkampus
 
Regular expressions
Ratnakar Mikkili
 
Binary search tree(bst)
Hossain Md Shakhawat
 
Constraint satisfaction Problem Artificial Intelligence
naeembisma
 
Amortized Analysis of Algorithms
sathish sak
 
Problem solving agents
Megha Sharma
 
Concurrency Control in Distributed Systems.pptx
MArshad35
 
Classical Planning
ahmad bassiouny
 
Planning
Amar Jukuntla
 
Asymptotic notation
Dr Shashikant Athawale
 
Classical relations and fuzzy relations
Baran Kaynak
 
Asymptotic notations
Nikhil Sharma
 
Operating System: Deadlock
InteX Research Lab
 

Similar to Algorithm analysis insertion sort and asymptotic notations (20)

PPT
Algorithm
Syam Kumar
 
PPT
Algorithm
Anirban Sarkar
 
PPT
Algorithm in Computer, Sorting and Notations
Abid Kohistani
 
PPT
algorithm_lec_1eregdsgdfgdgdfgdfgdfg.ppt
partho5958
 
PPTX
Unit 1.pptx
DeepakYadav656387
 
PPTX
Algorithms : Introduction and Analysis
Dhrumil Patel
 
PPT
Stacks queues lists
Luis Goldster
 
PPT
Stack squeues lists
James Wong
 
PPT
Stacks queues lists
Young Alista
 
PPT
Stacksqueueslists
Fraboni Ec
 
PPT
Stacks queues lists
Tony Nguyen
 
PPT
Stacks queues lists
Harry Potter
 
PDF
Data structures and algorithms
Adriano Patrick Cunha
 
PDF
Design & Analysis of Algorithms Lecture Notes
FellowBuddy.com
 
PDF
Analysis of Algorithms
Amna Saeed
 
PDF
Introduction to algorithms
Dr Sandeep Kumar Poonia
 
PPTX
asymptotic analysis and insertion sort analysis
Anindita Kundu
 
PDF
DAA - chapter 1.pdf
ASMAALWADEE2
 
Algorithm
Syam Kumar
 
Algorithm
Anirban Sarkar
 
Algorithm in Computer, Sorting and Notations
Abid Kohistani
 
algorithm_lec_1eregdsgdfgdgdfgdfgdfg.ppt
partho5958
 
Unit 1.pptx
DeepakYadav656387
 
Algorithms : Introduction and Analysis
Dhrumil Patel
 
Stacks queues lists
Luis Goldster
 
Stack squeues lists
James Wong
 
Stacks queues lists
Young Alista
 
Stacksqueueslists
Fraboni Ec
 
Stacks queues lists
Tony Nguyen
 
Stacks queues lists
Harry Potter
 
Data structures and algorithms
Adriano Patrick Cunha
 
Design & Analysis of Algorithms Lecture Notes
FellowBuddy.com
 
Analysis of Algorithms
Amna Saeed
 
Introduction to algorithms
Dr Sandeep Kumar Poonia
 
asymptotic analysis and insertion sort analysis
Anindita Kundu
 
DAA - chapter 1.pdf
ASMAALWADEE2
 
Ad

More from Amit Kumar Rathi (20)

PDF
Hybrid Systems using Fuzzy, NN and GA (Soft Computing)
Amit Kumar Rathi
 
PDF
Fundamentals of Genetic Algorithms (Soft Computing)
Amit Kumar Rathi
 
PDF
Fuzzy Systems by using fuzzy set (Soft Computing)
Amit Kumar Rathi
 
PDF
Fuzzy Set Theory and Classical Set Theory (Soft Computing)
Amit Kumar Rathi
 
PDF
Associative Memory using NN (Soft Computing)
Amit Kumar Rathi
 
PDF
Back Propagation Network (Soft Computing)
Amit Kumar Rathi
 
PDF
Fundamentals of Neural Network (Soft Computing)
Amit Kumar Rathi
 
PDF
Introduction to Soft Computing (intro to the building blocks of SC)
Amit Kumar Rathi
 
PDF
Topological sorting
Amit Kumar Rathi
 
PDF
String matching, naive,
Amit Kumar Rathi
 
PDF
Shortest path algorithms
Amit Kumar Rathi
 
PDF
Sccd and topological sorting
Amit Kumar Rathi
 
PDF
Red black trees
Amit Kumar Rathi
 
PDF
Recurrence and master theorem
Amit Kumar Rathi
 
PDF
Rabin karp string matcher
Amit Kumar Rathi
 
PDF
Minimum spanning tree
Amit Kumar Rathi
 
PDF
Merge sort analysis
Amit Kumar Rathi
 
PDF
Loop invarient
Amit Kumar Rathi
 
PDF
Linear sort
Amit Kumar Rathi
 
PDF
Heap and heapsort
Amit Kumar Rathi
 
Hybrid Systems using Fuzzy, NN and GA (Soft Computing)
Amit Kumar Rathi
 
Fundamentals of Genetic Algorithms (Soft Computing)
Amit Kumar Rathi
 
Fuzzy Systems by using fuzzy set (Soft Computing)
Amit Kumar Rathi
 
Fuzzy Set Theory and Classical Set Theory (Soft Computing)
Amit Kumar Rathi
 
Associative Memory using NN (Soft Computing)
Amit Kumar Rathi
 
Back Propagation Network (Soft Computing)
Amit Kumar Rathi
 
Fundamentals of Neural Network (Soft Computing)
Amit Kumar Rathi
 
Introduction to Soft Computing (intro to the building blocks of SC)
Amit Kumar Rathi
 
Topological sorting
Amit Kumar Rathi
 
String matching, naive,
Amit Kumar Rathi
 
Shortest path algorithms
Amit Kumar Rathi
 
Sccd and topological sorting
Amit Kumar Rathi
 
Red black trees
Amit Kumar Rathi
 
Recurrence and master theorem
Amit Kumar Rathi
 
Rabin karp string matcher
Amit Kumar Rathi
 
Minimum spanning tree
Amit Kumar Rathi
 
Merge sort analysis
Amit Kumar Rathi
 
Loop invarient
Amit Kumar Rathi
 
Linear sort
Amit Kumar Rathi
 
Heap and heapsort
Amit Kumar Rathi
 
Ad

Recently uploaded (20)

PDF
Dimensions of Societal Planning in Commonism
StefanMz
 
PPTX
How to Handle Salesperson Commision in Odoo 18 Sales
Celine George
 
PPT
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
PDF
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
PDF
Governor Josh Stein letter to NC delegation of U.S. House
Mebane Rash
 
PPTX
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
PDF
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
PPTX
care of patient with elimination needs.pptx
Rekhanjali Gupta
 
PDF
Geographical diversity of India short notes by sandeep swamy
Sandeep Swamy
 
PDF
Geographical Diversity of India 100 Mcq.pdf/ 7th class new ncert /Social/Samy...
Sandeep Swamy
 
PDF
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
PPTX
Quarter 1_PPT_PE & HEALTH 8_WEEK 3-4.pptx
ronajadolpnhs
 
PDF
Characteristics, Strengths and Weaknesses of Quantitative Research.pdf
Thelma Villaflores
 
PDF
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
PDF
Isharyanti-2025-Cross Language Communication in Indonesian Language
Neny Isharyanti
 
PPTX
PPT-Q1-WEEK-3-SCIENCE-ERevised Matatag Grade 3.pptx
reijhongidayawan02
 
PPTX
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
PPTX
STAFF DEVELOPMENT AND WELFARE: MANAGEMENT
PRADEEP ABOTHU
 
PDF
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
PDF
Aprendendo Arquitetura Framework Salesforce - Dia 03
Mauricio Alexandre Silva
 
Dimensions of Societal Planning in Commonism
StefanMz
 
How to Handle Salesperson Commision in Odoo 18 Sales
Celine George
 
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
Governor Josh Stein letter to NC delegation of U.S. House
Mebane Rash
 
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
care of patient with elimination needs.pptx
Rekhanjali Gupta
 
Geographical diversity of India short notes by sandeep swamy
Sandeep Swamy
 
Geographical Diversity of India 100 Mcq.pdf/ 7th class new ncert /Social/Samy...
Sandeep Swamy
 
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
Quarter 1_PPT_PE & HEALTH 8_WEEK 3-4.pptx
ronajadolpnhs
 
Characteristics, Strengths and Weaknesses of Quantitative Research.pdf
Thelma Villaflores
 
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
Isharyanti-2025-Cross Language Communication in Indonesian Language
Neny Isharyanti
 
PPT-Q1-WEEK-3-SCIENCE-ERevised Matatag Grade 3.pptx
reijhongidayawan02
 
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
STAFF DEVELOPMENT AND WELFARE: MANAGEMENT
PRADEEP ABOTHU
 
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
Aprendendo Arquitetura Framework Salesforce - Dia 03
Mauricio Alexandre Silva
 

Algorithm analysis insertion sort and asymptotic notations