SlideShare a Scribd company logo
Algorithms
Abid Kohistani
GC Madyan Swat
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.
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.
The problem of sorting
Insertion Sort
Example of Insertion Sort
Example of Insertion Sort
Example of Insertion Sort
Example of Insertion Sort
Example of Insertion Sort
Example of Insertion Sort
Example of Insertion Sort
Example of Insertion Sort
Example of Insertion Sort
Example of Insertion Sort
Example of Insertion Sort
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
• reliability
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!
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.
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.
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.
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)(
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
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
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
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 
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
)(
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.
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
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
Ω-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
-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 
Asymptotic notation
Graphic examples of and . ,, O
2
2
22
1 3
2
1
ncnnnc 
21
3
2
1
c
n
c 
)(3
2
1
,7 22
0 nnnn 
Example 1.
Show that
We must find c1 and c2 such that
Dividing bothsides by n2 yields
For
)(3
2
1
)( 22
nnnnf 
Theorem
• For any two functions and , we have
if and only if
)(ng
))(()( ngnf 
)(nf
)).(()(and))(()( ngnfngOnf 
Because :
)2(5223 nnn 
)2(5223 nOnn 
Example 2.
)2(5223)( nnnnf 
Example 3.
610033,3forsince)(61003 2222
 nnncnOnn
Example 3.
3when61003,1forsince)(61003
610033,3forsince)(61003
2332
2222


nnnncnOnn
nnncnOnn
Example 3.
cnncncnOnn
nnnncnOnn
nnncnOnn



when3,anyforsince)(61003
3when61003,1forsince)(61003
610033,3forsince)(61003
22
2332
2222
Example 3.
100when610032,2forsince)(61003
when3,anyforsince)(61003
3when61003,1forsince)(61003
610033,3forsince)(61003
2222
22
2332
2222




nnnncnnn
cnncncnOnn
nnnncnOnn
nnncnOnn
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
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
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
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








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
Standard notations and common functions
• Floors and ceilings
    11  xxxxx
Standard notations and common functions
• Logarithms:
)lg(lglglg
)(loglog
logln
loglg 2
nn
nn
nn
nn
kk
e




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




Standard notations and common functions
• Logarithms:
b
a
ca
aa
a
b
ac
bb
bb
log
1
log
log)/1(log
loglog



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




More Related Content

PPT
Algorithm
Syam Kumar
 
PPT
Algorithm
Anirban Sarkar
 
PDF
Analysis and design of algorithms part2
Deepak John
 
PPTX
Algorithms - "Chapter 2 getting started"
Ra'Fat Al-Msie'deen
 
PDF
Anlysis and design of algorithms part 1
Deepak John
 
PPT
Design and analysis of Algorithm By Dr. B. J. Mohite
Zeal Education Society, Pune
 
PPTX
asymptotic analysis and insertion sort analysis
Anindita Kundu
 
PDF
Algorithms Lecture 2: Analysis of Algorithms I
Mohamed Loey
 
Algorithm
Syam Kumar
 
Algorithm
Anirban Sarkar
 
Analysis and design of algorithms part2
Deepak John
 
Algorithms - "Chapter 2 getting started"
Ra'Fat Al-Msie'deen
 
Anlysis and design of algorithms part 1
Deepak John
 
Design and analysis of Algorithm By Dr. B. J. Mohite
Zeal Education Society, Pune
 
asymptotic analysis and insertion sort analysis
Anindita Kundu
 
Algorithms Lecture 2: Analysis of Algorithms I
Mohamed Loey
 

What's hot (20)

PPT
Algorithm And analysis Lecture 03& 04-time complexity.
Tariq Khan
 
PPT
02 order of growth
Hira Gul
 
PPTX
Lecture 5: Asymptotic analysis of algorithms
Vivek Bhargav
 
PDF
Algorithms lecture 3
Mimi Haque
 
PDF
Lecture 4 asymptotic notations
jayavignesh86
 
PPT
Fundamentals of the Analysis of Algorithm Efficiency
Saranya Natarajan
 
PPT
Algorithm analysis
sumitbardhan
 
PPTX
Complexity analysis in Algorithms
Daffodil International University
 
ODP
Intro to Sorting + Insertion Sort
Nicholas Case
 
PPT
Introduction to Algorithms
Venkatesh Iyer
 
PDF
Introduction to Algorithms Complexity Analysis
Dr. Pankaj Agarwal
 
PPTX
Analysis of algorithn class 2
Kumar
 
PDF
Asymptotic Analysis
sonugupta
 
PPTX
Unit i basic concepts of algorithms
sangeetha s
 
PPT
01 intro to algorithm--updated 2015
Hira Gul
 
PPT
Analysis Of Algorithms I
Sri Prasanna
 
PPT
Slide2
Thiti Sununta
 
PDF
2 chapter2 algorithm_analysispart1
SSE_AndyLi
 
PPT
Basic terminologies & asymptotic notations
Rajendran
 
PPTX
Unit 1
Abha Damani
 
Algorithm And analysis Lecture 03& 04-time complexity.
Tariq Khan
 
02 order of growth
Hira Gul
 
Lecture 5: Asymptotic analysis of algorithms
Vivek Bhargav
 
Algorithms lecture 3
Mimi Haque
 
Lecture 4 asymptotic notations
jayavignesh86
 
Fundamentals of the Analysis of Algorithm Efficiency
Saranya Natarajan
 
Algorithm analysis
sumitbardhan
 
Complexity analysis in Algorithms
Daffodil International University
 
Intro to Sorting + Insertion Sort
Nicholas Case
 
Introduction to Algorithms
Venkatesh Iyer
 
Introduction to Algorithms Complexity Analysis
Dr. Pankaj Agarwal
 
Analysis of algorithn class 2
Kumar
 
Asymptotic Analysis
sonugupta
 
Unit i basic concepts of algorithms
sangeetha s
 
01 intro to algorithm--updated 2015
Hira Gul
 
Analysis Of Algorithms I
Sri Prasanna
 
2 chapter2 algorithm_analysispart1
SSE_AndyLi
 
Basic terminologies & asymptotic notations
Rajendran
 
Unit 1
Abha Damani
 
Ad

Similar to Algorithm in Computer, Sorting and Notations (20)

PDF
Algorithm analysis insertion sort and asymptotic notations
Amit Kumar Rathi
 
PPTX
Algorithms & Complexity Calculation
Akhil Kaushik
 
PPT
algorithm_lec_1eregdsgdfgdgdfgdfgdfg.ppt
partho5958
 
PPTX
Searching Algorithms
Afaq Mansoor Khan
 
PDF
Analysis and Algorithms: basic Introduction
ssuseraf8b2f
 
PPTX
Data Structure Algorithm -Algorithm Complexity
zeeshanhaidermazhar7
 
PDF
Data Structure & Algorithms - Introduction
babuk110
 
PPTX
Unit 1.pptx
DeepakYadav656387
 
PPTX
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_25-07-2022_Fu...
AntareepMajumder
 
PPTX
complexity big oh notation notation.pptx
narifmsit18seecs
 
PPTX
2. Introduction to Algorithm.pptx
RahikAhmed1
 
PPTX
Algorithm for the DAA agscsnak javausmagagah
RaviPandey598038
 
PDF
DSA
rrupa2
 
PDF
Advanced Datastructures and algorithms CP4151unit1b.pdf
Sheba41
 
PPTX
DAA-Unit1.pptx
NishaS88
 
PPTX
complex computer systems and its applications.pptx
narifmsit18seecs
 
PPTX
BCSE202Lkkljkljkbbbnbnghghjghghghghghghghgh
shivapatil54
 
PPTX
Unit 1, ADA.pptx
jinkhatima
 
PPTX
Analysis of Algorithm full version 2024.pptx
rajesshs31r
 
PPTX
Design Analysis of Alogorithm 1 ppt 2024.pptx
rajesshs31r
 
Algorithm analysis insertion sort and asymptotic notations
Amit Kumar Rathi
 
Algorithms & Complexity Calculation
Akhil Kaushik
 
algorithm_lec_1eregdsgdfgdgdfgdfgdfg.ppt
partho5958
 
Searching Algorithms
Afaq Mansoor Khan
 
Analysis and Algorithms: basic Introduction
ssuseraf8b2f
 
Data Structure Algorithm -Algorithm Complexity
zeeshanhaidermazhar7
 
Data Structure & Algorithms - Introduction
babuk110
 
Unit 1.pptx
DeepakYadav656387
 
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_25-07-2022_Fu...
AntareepMajumder
 
complexity big oh notation notation.pptx
narifmsit18seecs
 
2. Introduction to Algorithm.pptx
RahikAhmed1
 
Algorithm for the DAA agscsnak javausmagagah
RaviPandey598038
 
DSA
rrupa2
 
Advanced Datastructures and algorithms CP4151unit1b.pdf
Sheba41
 
DAA-Unit1.pptx
NishaS88
 
complex computer systems and its applications.pptx
narifmsit18seecs
 
BCSE202Lkkljkljkbbbnbnghghjghghghghghghghgh
shivapatil54
 
Unit 1, ADA.pptx
jinkhatima
 
Analysis of Algorithm full version 2024.pptx
rajesshs31r
 
Design Analysis of Alogorithm 1 ppt 2024.pptx
rajesshs31r
 
Ad

More from Abid Kohistani (9)

PPTX
Polymorphism in C# Function overloading in C#
Abid Kohistani
 
PPTX
Exception Handling in C#
Abid Kohistani
 
PPTX
Access Modifiers in C# ,Inheritance and Encapsulation
Abid Kohistani
 
PPTX
OOP in C# Classes and Objects.
Abid Kohistani
 
PPTX
Loops in C# for loops while and do while loop.
Abid Kohistani
 
PPTX
Conditions In C# C-Sharp
Abid Kohistani
 
PPTX
C# Operators. (C-Sharp Operators)
Abid Kohistani
 
PPTX
data types in C-Sharp (C#)
Abid Kohistani
 
PPTX
Methods In C-Sharp (C#)
Abid Kohistani
 
Polymorphism in C# Function overloading in C#
Abid Kohistani
 
Exception Handling in C#
Abid Kohistani
 
Access Modifiers in C# ,Inheritance and Encapsulation
Abid Kohistani
 
OOP in C# Classes and Objects.
Abid Kohistani
 
Loops in C# for loops while and do while loop.
Abid Kohistani
 
Conditions In C# C-Sharp
Abid Kohistani
 
C# Operators. (C-Sharp Operators)
Abid Kohistani
 
data types in C-Sharp (C#)
Abid Kohistani
 
Methods In C-Sharp (C#)
Abid Kohistani
 

Recently uploaded (20)

PDF
Make GenAI investments go further with the Dell AI Factory - Infographic
Principled Technologies
 
PDF
DevOps & Developer Experience Summer BBQ
AUGNYC
 
PPTX
Comunidade Salesforce São Paulo - Desmistificando o Omnistudio (Vlocity)
Francisco Vieira Júnior
 
PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
PDF
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
 
PDF
Doc9.....................................
SofiaCollazos
 
PDF
SparkLabs Primer on Artificial Intelligence 2025
SparkLabs Group
 
PDF
Shreyas_Phanse_Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
SHREYAS PHANSE
 
PDF
Chapter 2 Digital Image Fundamentals.pdf
Getnet Tigabie Askale -(GM)
 
PPTX
Smart Infrastructure and Automation through IoT Sensors
Rejig Digital
 
PPTX
C Programming Basics concept krnppt.pptx
Karan Prajapat
 
PDF
The Evolution of KM Roles (Presented at Knowledge Summit Dublin 2025)
Enterprise Knowledge
 
PDF
solutions_manual_-_materials___processing_in_manufacturing__demargo_.pdf
AbdullahSani29
 
PPTX
The-Ethical-Hackers-Imperative-Safeguarding-the-Digital-Frontier.pptx
sujalchauhan1305
 
PDF
Revolutionize Operations with Intelligent IoT Monitoring and Control
Rejig Digital
 
PDF
NewMind AI Monthly Chronicles - July 2025
NewMind AI
 
PDF
CIFDAQ'S Market Insight: BTC to ETH money in motion
CIFDAQ
 
PDF
This slide provides an overview Technology
mineshkharadi333
 
PDF
Software Development Methodologies in 2025
KodekX
 
PDF
NewMind AI Weekly Chronicles - July'25 - Week IV
NewMind AI
 
Make GenAI investments go further with the Dell AI Factory - Infographic
Principled Technologies
 
DevOps & Developer Experience Summer BBQ
AUGNYC
 
Comunidade Salesforce São Paulo - Desmistificando o Omnistudio (Vlocity)
Francisco Vieira Júnior
 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
 
Doc9.....................................
SofiaCollazos
 
SparkLabs Primer on Artificial Intelligence 2025
SparkLabs Group
 
Shreyas_Phanse_Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
SHREYAS PHANSE
 
Chapter 2 Digital Image Fundamentals.pdf
Getnet Tigabie Askale -(GM)
 
Smart Infrastructure and Automation through IoT Sensors
Rejig Digital
 
C Programming Basics concept krnppt.pptx
Karan Prajapat
 
The Evolution of KM Roles (Presented at Knowledge Summit Dublin 2025)
Enterprise Knowledge
 
solutions_manual_-_materials___processing_in_manufacturing__demargo_.pdf
AbdullahSani29
 
The-Ethical-Hackers-Imperative-Safeguarding-the-Digital-Frontier.pptx
sujalchauhan1305
 
Revolutionize Operations with Intelligent IoT Monitoring and Control
Rejig Digital
 
NewMind AI Monthly Chronicles - July 2025
NewMind AI
 
CIFDAQ'S Market Insight: BTC to ETH money in motion
CIFDAQ
 
This slide provides an overview Technology
mineshkharadi333
 
Software Development Methodologies in 2025
KodekX
 
NewMind AI Weekly Chronicles - July'25 - Week IV
NewMind AI
 

Algorithm in Computer, Sorting and Notations