SlideShare a Scribd company logo
1 of 24
Big O Notation
outline
 Performance vs Complexity
 Algorithm Efficiency
 Big O Notation
Performance vs Complexity
• Performance:
 How much time/memory/disk/… is used by the algorithm
 Depends on the machine, compiler, code,…
• Complexity:
 How do the resource requirements of a program/algorithm scale
Complexity affects performance
but not the other way around
If I have an algorithm with high complexity, it will take longer time to run.
However, if I have a very slow program, that does not mean that my algorithm is very complex;
that could be all other kind of issues that made the program run slow.
Complexity of an Algorithm
 We are going to count the number of the steps that are taken during the
execution
 Assumption (simplification):
 Each step costs the same amount
 +, -, return a value, access an array element, ….
Complexity of an Algorithm (cont.)
• Example 1:
• private int getLastElement (int [] numbers)
• {
return numbers[numbers.length -1];
}
Number of steps: 4
Constant time function (independent of array size)
Notes: Let’s look at each step that need to be executed. It is always 4 steps to access the last element,
no matter the number of elements.
Complexity of an Algorithm (cont.)
• Example 2:
• private static int sum(int [] numbers)
• {
int sum = 0;
for (int i = 0; i < number.length; i++){
sum = sum + number[i];
}
return sum;
}
Outside loop: 3 + 2 Number of steps:6n + 5
Linear time functionLoop body: 6
INSTRUCTOR-NOTES:This time we use a loop.
We will start by the steps that will be executed only once and the are outside the loop.
Then we count the steps that are going to be repeated over and over again.
Algorithm Efficiency
 Let’s look at the following algorithm for initializing the values in an array:
final int N = 500;
int [] counts = new int[N];
for (int i=0; i<counts.length; i++)
counts[i] = 0;
 The length of time the algorithm takes to execute depends on the value of N
Algorithm Efficiency (cont.)
 In that algorithm, we have one loop that processes all of the elements in the array
 Intuitively:
– If N was half of its value, we would expect the algorithm to take half the time
– If N was twice its value, we would expect the algorithm to take twice the time
 That is true and we say that the algorithm efficiency relative to N is linear
Algorithm Efficiency (cont.)
 Let’s look at another algorithm for initializing the values in a different array:
final int N = 500;
int [] [] counts = new int[N][N];
for (int i=0; i<counts.length; i++)
for (int j=0; j<counts[i].length; j++)
counts[i][j] = 0;
 The length of time the algorithm takes to execute still depends on the value of N
Algorithm Efficiency (cont.)
 However, in the second algorithm, we have two nested loops to process the
elements in the two dimensional array
 Intuitively:
– If N is half its value, we would expect the algorithm to take one quarter the time
– If N is twice its value, we would expect the algorithm to take quadruple the time
 That is true and we say that the algorithm efficiency relative to N is quadratic
Big O notation
Why do we need Big-O?
 In computer science, we use Big-O to classify algorithms based on their scalability.
 Basically, it tells you how fast a function grows or declines.
 The letter O is used because the rate of growth of a function is also called its order.
 It also allows us to estimate the worst case running time of an algorithm as a
function of the input size.
Big O notation
 Examples:
– We can say that the first algorithm is O(n)
– We can say that the second algorithm is O(n2)
 For any algorithm that has a function g(n) of the parameter n that describes its
length of time to execute, we can say the algorithm is O(g(n))
 We only include the fastest growing term and ignore any multiplying by or adding of
constants (which makes sense because those depend on the particular hardware the
program is run on)
Big O notation (cont.)
Growth Functions:
Eight functions O(n) that occur frequently in the analysis of algorithms (in order of
increasing rate of growth relative to n):
– Constant  1
– Logarithmic  log n
– Linear  n
– Log Linear  n log n
– Quadratic  n2
– Cubic  n3
– Exponential  2n
– Exhaustive Search  n!
Big O notation (cont.)
Growth Rates Compared:
Big O notation (cont.)
Big O notation (cont.)
 The growth list is useful because of the following fact:
 if a function f(n) is a sum of functions, one of which grows faster than the
others, then the faster growing one determines the order of f(n).
 Example:
 If f(n) = 10 log(n) + 5 (log(n))3 + 7 n + 3 n 2 + 6 n 3,
 then f(n) = O(n 3).
Big O notation (cont.)
Big-O for a Problem:
 O(g(n)) for a problem means there is some O(g(n)) algorithm that solves the
problem
 Don’t assume that the specific algorithm that you are currently using is the
best solution for the problem
 There may be other correct algorithms that grow at a smaller rate with
increasing n
 Many times, the goal is to find an algorithm with the smallest possible
growth rate
Big O notation (cont.)
Role of Data Structures:
 That brings up the topic of the structure of the data on which the algorithm
operates
 If we are using an algorithm manually on some amount of data, we
intuitively try to organize the data in a way that minimizes the number of
steps that we need to take
 Publishers offer dictionaries with the words listed in alphabetical order to
minimize the length of time it takes us to look up a word
Big O notation (cont.)
Role of Data Structures:
 We can do the same thing for algorithms in our computer programs
 Example: Finding a numeric value in a list
 If we assume that the list is unordered, we must search from the beginning
to the end
 On average, we will search half the list
 Worst case, we will search the entire list
 Algorithm is O(n), where n is size of array
Big O notation (cont.)
Example:
int [] list = {7, 2, 9, 5, 6, 4};
for (int i=0; i<list.length, i++)
if (value == list[i])
statement; // found it
// didn’t find it
Big O notation (cont.)
 If we assume that the list is ordered, we can still search the entire list
from the beginning to the end to determine if we have a match
 But, we do not need to search that way
 Because the values are in numerical order, we can use a binary search
algorithm
 Algorithm is O(logn), where n is size of array
Big O notation (cont.)
Role of Data Structures:
 The difference in the structure of the data between an unordered list and an
ordered list can be used to reduce algorithm Big-O
 This is the role of data structures and why we study them
 We need to be as clever in organizing our data efficiently as we are in
figuring out an algorithm for processing it efficiently
Big O notation (cont.)
Example:
 Find an order for
 f(x) = 7 x5 + 5 x3 – x + 4
 O(x5)
Big O notation (cont.)
Sorting Algorithms Big-O:
http://bigocheatsheet.com/

More Related Content

What's hot

What's hot (20)

Big O Notation
Big O NotationBig O Notation
Big O Notation
 
Analysis of algorithm
Analysis of algorithmAnalysis of algorithm
Analysis of algorithm
 
Introduction to numpy
Introduction to numpyIntroduction to numpy
Introduction to numpy
 
Numpy tutorial
Numpy tutorialNumpy tutorial
Numpy tutorial
 
Data structures question paper anna university
Data structures question paper anna universityData structures question paper anna university
Data structures question paper anna university
 
Algorithm analysis
Algorithm analysisAlgorithm analysis
Algorithm analysis
 
Introduction to algorithms
Introduction to algorithmsIntroduction to algorithms
Introduction to algorithms
 
sum of subset problem using Backtracking
sum of subset problem using Backtrackingsum of subset problem using Backtracking
sum of subset problem using Backtracking
 
Introduction to NumPy
Introduction to NumPyIntroduction to NumPy
Introduction to NumPy
 
Pandas
PandasPandas
Pandas
 
Algorithm analysis (All in one)
Algorithm analysis (All in one)Algorithm analysis (All in one)
Algorithm analysis (All in one)
 
Performance analysis and randamized agoritham
Performance analysis and randamized agorithamPerformance analysis and randamized agoritham
Performance analysis and randamized agoritham
 
Asymptotic notation
Asymptotic notationAsymptotic notation
Asymptotic notation
 
Python programming : Arrays
Python programming : ArraysPython programming : Arrays
Python programming : Arrays
 
Time andspacecomplexity
Time andspacecomplexityTime andspacecomplexity
Time andspacecomplexity
 
Algorithms Lecture 1: Introduction to Algorithms
Algorithms Lecture 1: Introduction to AlgorithmsAlgorithms Lecture 1: Introduction to Algorithms
Algorithms Lecture 1: Introduction to Algorithms
 
Selection sort and insertion sort
Selection sort and insertion sortSelection sort and insertion sort
Selection sort and insertion sort
 
5 csp
5 csp5 csp
5 csp
 
Knapsack Problem
Knapsack ProblemKnapsack Problem
Knapsack Problem
 
Asymptotic notations
Asymptotic notationsAsymptotic notations
Asymptotic notations
 

Viewers also liked

Data structures and Big O notation
Data structures and Big O notationData structures and Big O notation
Data structures and Big O notationMuthiah Abbhirami
 
Asymptotic notations
Asymptotic notationsAsymptotic notations
Asymptotic notationsEhtisham Ali
 
Divide and Conquer - Part 1
Divide and Conquer - Part 1Divide and Conquer - Part 1
Divide and Conquer - Part 1Amrinder Arora
 
Analysis Of Algorithms I
Analysis Of Algorithms IAnalysis Of Algorithms I
Analysis Of Algorithms ISri Prasanna
 
02 asymptotic-notation-and-recurrences
02 asymptotic-notation-and-recurrences02 asymptotic-notation-and-recurrences
02 asymptotic-notation-and-recurrencesNoushadur Shoukhin
 
Amortized complexity
Amortized complexityAmortized complexity
Amortized complexityparamita30
 
03 Analysis of Algorithms: Probabilistic Analysis
03 Analysis of Algorithms: Probabilistic Analysis03 Analysis of Algorithms: Probabilistic Analysis
03 Analysis of Algorithms: Probabilistic AnalysisAndres Mendez-Vazquez
 
QUESTION BANK FOR ANNA UNNIVERISTY SYLLABUS
QUESTION BANK FOR ANNA UNNIVERISTY SYLLABUSQUESTION BANK FOR ANNA UNNIVERISTY SYLLABUS
QUESTION BANK FOR ANNA UNNIVERISTY SYLLABUSJAMBIKA
 
Analysis of algorithms
Analysis of algorithmsAnalysis of algorithms
Analysis of algorithmsGanesh Solanke
 
Divide and Conquer - Part II - Quickselect and Closest Pair of Points
Divide and Conquer - Part II - Quickselect and Closest Pair of PointsDivide and Conquer - Part II - Quickselect and Closest Pair of Points
Divide and Conquer - Part II - Quickselect and Closest Pair of PointsAmrinder Arora
 
Introduction to Algorithms and Asymptotic Notation
Introduction to Algorithms and Asymptotic NotationIntroduction to Algorithms and Asymptotic Notation
Introduction to Algorithms and Asymptotic NotationAmrinder Arora
 
Time and space complexity
Time and space complexityTime and space complexity
Time and space complexityAnkit Katiyar
 

Viewers also liked (20)

Data structures and Big O notation
Data structures and Big O notationData structures and Big O notation
Data structures and Big O notation
 
Algorithm big o
Algorithm big oAlgorithm big o
Algorithm big o
 
Big o
Big oBig o
Big o
 
Time complexity
Time complexityTime complexity
Time complexity
 
Asymptotic notations
Asymptotic notationsAsymptotic notations
Asymptotic notations
 
Divide and Conquer - Part 1
Divide and Conquer - Part 1Divide and Conquer - Part 1
Divide and Conquer - Part 1
 
Complexity
ComplexityComplexity
Complexity
 
Analysis of algo
Analysis of algoAnalysis of algo
Analysis of algo
 
Algorithum Analysis
Algorithum AnalysisAlgorithum Analysis
Algorithum Analysis
 
Analysis Of Algorithms I
Analysis Of Algorithms IAnalysis Of Algorithms I
Analysis Of Algorithms I
 
02 asymptotic-notation-and-recurrences
02 asymptotic-notation-and-recurrences02 asymptotic-notation-and-recurrences
02 asymptotic-notation-and-recurrences
 
Amortized complexity
Amortized complexityAmortized complexity
Amortized complexity
 
P versus NP
P versus NPP versus NP
P versus NP
 
03 Analysis of Algorithms: Probabilistic Analysis
03 Analysis of Algorithms: Probabilistic Analysis03 Analysis of Algorithms: Probabilistic Analysis
03 Analysis of Algorithms: Probabilistic Analysis
 
13 Amortized Analysis
13 Amortized Analysis13 Amortized Analysis
13 Amortized Analysis
 
QUESTION BANK FOR ANNA UNNIVERISTY SYLLABUS
QUESTION BANK FOR ANNA UNNIVERISTY SYLLABUSQUESTION BANK FOR ANNA UNNIVERISTY SYLLABUS
QUESTION BANK FOR ANNA UNNIVERISTY SYLLABUS
 
Analysis of algorithms
Analysis of algorithmsAnalysis of algorithms
Analysis of algorithms
 
Divide and Conquer - Part II - Quickselect and Closest Pair of Points
Divide and Conquer - Part II - Quickselect and Closest Pair of PointsDivide and Conquer - Part II - Quickselect and Closest Pair of Points
Divide and Conquer - Part II - Quickselect and Closest Pair of Points
 
Introduction to Algorithms and Asymptotic Notation
Introduction to Algorithms and Asymptotic NotationIntroduction to Algorithms and Asymptotic Notation
Introduction to Algorithms and Asymptotic Notation
 
Time and space complexity
Time and space complexityTime and space complexity
Time and space complexity
 

Similar to 9 big o-notation

TIME EXECUTION OF DIFFERENT SORTED ALGORITHMS
TIME EXECUTION   OF  DIFFERENT SORTED ALGORITHMSTIME EXECUTION   OF  DIFFERENT SORTED ALGORITHMS
TIME EXECUTION OF DIFFERENT SORTED ALGORITHMSTanya Makkar
 
DA lecture 3.pptx
DA lecture 3.pptxDA lecture 3.pptx
DA lecture 3.pptxSayanSen36
 
Aad introduction
Aad introductionAad introduction
Aad introductionMr SMAK
 
Algorithm Analysis.pdf
Algorithm Analysis.pdfAlgorithm Analysis.pdf
Algorithm Analysis.pdfMemMem25
 
Theory of algorithms final
Theory of algorithms final Theory of algorithms final
Theory of algorithms final Dgech
 
Computational Complexity.pptx
Computational Complexity.pptxComputational Complexity.pptx
Computational Complexity.pptxEnosSalar
 
Data Structures- Part2 analysis tools
Data Structures- Part2 analysis toolsData Structures- Part2 analysis tools
Data Structures- Part2 analysis toolsAbdullah Al-hazmy
 
Data Structures and Algorithm Analysis
Data Structures  and  Algorithm AnalysisData Structures  and  Algorithm Analysis
Data Structures and Algorithm AnalysisMary Margarat
 
Data Structures and Algorithms Lecture 2: Analysis of Algorithms, Asymptotic ...
Data Structures and Algorithms Lecture 2: Analysis of Algorithms, Asymptotic ...Data Structures and Algorithms Lecture 2: Analysis of Algorithms, Asymptotic ...
Data Structures and Algorithms Lecture 2: Analysis of Algorithms, Asymptotic ...TechVision8
 
VCE Unit 01 (2).pptx
VCE Unit 01 (2).pptxVCE Unit 01 (2).pptx
VCE Unit 01 (2).pptxskilljiolms
 
Module 1_ Introduction.pptx
Module 1_ Introduction.pptxModule 1_ Introduction.pptx
Module 1_ Introduction.pptxnikshaikh786
 

Similar to 9 big o-notation (20)

TIME EXECUTION OF DIFFERENT SORTED ALGORITHMS
TIME EXECUTION   OF  DIFFERENT SORTED ALGORITHMSTIME EXECUTION   OF  DIFFERENT SORTED ALGORITHMS
TIME EXECUTION OF DIFFERENT SORTED ALGORITHMS
 
Unit ii algorithm
Unit   ii algorithmUnit   ii algorithm
Unit ii algorithm
 
DA lecture 3.pptx
DA lecture 3.pptxDA lecture 3.pptx
DA lecture 3.pptx
 
Aad introduction
Aad introductionAad introduction
Aad introduction
 
Algorithm Analysis.pdf
Algorithm Analysis.pdfAlgorithm Analysis.pdf
Algorithm Analysis.pdf
 
Theory of algorithms final
Theory of algorithms final Theory of algorithms final
Theory of algorithms final
 
Ds
DsDs
Ds
 
Computational Complexity.pptx
Computational Complexity.pptxComputational Complexity.pptx
Computational Complexity.pptx
 
Data Structures- Part2 analysis tools
Data Structures- Part2 analysis toolsData Structures- Part2 analysis tools
Data Structures- Part2 analysis tools
 
Data Structures and Algorithm Analysis
Data Structures  and  Algorithm AnalysisData Structures  and  Algorithm Analysis
Data Structures and Algorithm Analysis
 
Data Structures and Algorithms Lecture 2: Analysis of Algorithms, Asymptotic ...
Data Structures and Algorithms Lecture 2: Analysis of Algorithms, Asymptotic ...Data Structures and Algorithms Lecture 2: Analysis of Algorithms, Asymptotic ...
Data Structures and Algorithms Lecture 2: Analysis of Algorithms, Asymptotic ...
 
Analysis of algorithms
Analysis of algorithmsAnalysis of algorithms
Analysis of algorithms
 
DATA STRUCTURE.pdf
DATA STRUCTURE.pdfDATA STRUCTURE.pdf
DATA STRUCTURE.pdf
 
DATA STRUCTURE
DATA STRUCTUREDATA STRUCTURE
DATA STRUCTURE
 
VCE Unit 01 (2).pptx
VCE Unit 01 (2).pptxVCE Unit 01 (2).pptx
VCE Unit 01 (2).pptx
 
Unit 1.pptx
Unit 1.pptxUnit 1.pptx
Unit 1.pptx
 
Chapter two
Chapter twoChapter two
Chapter two
 
Design & Analysis Of Algorithm
Design & Analysis Of AlgorithmDesign & Analysis Of Algorithm
Design & Analysis Of Algorithm
 
Algoritmos
AlgoritmosAlgoritmos
Algoritmos
 
Module 1_ Introduction.pptx
Module 1_ Introduction.pptxModule 1_ Introduction.pptx
Module 1_ Introduction.pptx
 

More from irdginfo

Quicksort Presentation
Quicksort PresentationQuicksort Presentation
Quicksort Presentationirdginfo
 
10 merge sort
10 merge sort10 merge sort
10 merge sortirdginfo
 
8 elementary sorts-bubble
8 elementary sorts-bubble8 elementary sorts-bubble
8 elementary sorts-bubbleirdginfo
 
8 elementary sorts-shell
8 elementary sorts-shell8 elementary sorts-shell
8 elementary sorts-shellirdginfo
 
8 elementary sorts-insertion
8 elementary sorts-insertion8 elementary sorts-insertion
8 elementary sorts-insertionirdginfo
 
8 elementary sorts-selection
8 elementary sorts-selection8 elementary sorts-selection
8 elementary sorts-selectionirdginfo
 
7 searching injava-binary
7 searching injava-binary7 searching injava-binary
7 searching injava-binaryirdginfo
 
6 arrays injava
6 arrays injava6 arrays injava
6 arrays injavairdginfo
 
5 data structures-hashtable
5 data structures-hashtable5 data structures-hashtable
5 data structures-hashtableirdginfo
 
5 data structures-tree
5 data structures-tree5 data structures-tree
5 data structures-treeirdginfo
 
5 data structures-stack
5 data structures-stack5 data structures-stack
5 data structures-stackirdginfo
 
5 data structures-arraysandlinkedlist
5 data structures-arraysandlinkedlist5 data structures-arraysandlinkedlist
5 data structures-arraysandlinkedlistirdginfo
 
4 character encoding-unicode
4 character encoding-unicode4 character encoding-unicode
4 character encoding-unicodeirdginfo
 
4 character encoding-ascii
4 character encoding-ascii4 character encoding-ascii
4 character encoding-asciiirdginfo
 
4 character encoding
4 character encoding4 character encoding
4 character encodingirdginfo
 
3 number systems-floatingpoint
3 number systems-floatingpoint3 number systems-floatingpoint
3 number systems-floatingpointirdginfo
 
2 number systems-scientificnotation
2 number systems-scientificnotation2 number systems-scientificnotation
2 number systems-scientificnotationirdginfo
 
1 number systems-hex
1 number systems-hex1 number systems-hex
1 number systems-hexirdginfo
 
1 number systems-unsignedsignedintegers
1 number systems-unsignedsignedintegers1 number systems-unsignedsignedintegers
1 number systems-unsignedsignedintegersirdginfo
 
1 number systems-octal
1 number systems-octal1 number systems-octal
1 number systems-octalirdginfo
 

More from irdginfo (20)

Quicksort Presentation
Quicksort PresentationQuicksort Presentation
Quicksort Presentation
 
10 merge sort
10 merge sort10 merge sort
10 merge sort
 
8 elementary sorts-bubble
8 elementary sorts-bubble8 elementary sorts-bubble
8 elementary sorts-bubble
 
8 elementary sorts-shell
8 elementary sorts-shell8 elementary sorts-shell
8 elementary sorts-shell
 
8 elementary sorts-insertion
8 elementary sorts-insertion8 elementary sorts-insertion
8 elementary sorts-insertion
 
8 elementary sorts-selection
8 elementary sorts-selection8 elementary sorts-selection
8 elementary sorts-selection
 
7 searching injava-binary
7 searching injava-binary7 searching injava-binary
7 searching injava-binary
 
6 arrays injava
6 arrays injava6 arrays injava
6 arrays injava
 
5 data structures-hashtable
5 data structures-hashtable5 data structures-hashtable
5 data structures-hashtable
 
5 data structures-tree
5 data structures-tree5 data structures-tree
5 data structures-tree
 
5 data structures-stack
5 data structures-stack5 data structures-stack
5 data structures-stack
 
5 data structures-arraysandlinkedlist
5 data structures-arraysandlinkedlist5 data structures-arraysandlinkedlist
5 data structures-arraysandlinkedlist
 
4 character encoding-unicode
4 character encoding-unicode4 character encoding-unicode
4 character encoding-unicode
 
4 character encoding-ascii
4 character encoding-ascii4 character encoding-ascii
4 character encoding-ascii
 
4 character encoding
4 character encoding4 character encoding
4 character encoding
 
3 number systems-floatingpoint
3 number systems-floatingpoint3 number systems-floatingpoint
3 number systems-floatingpoint
 
2 number systems-scientificnotation
2 number systems-scientificnotation2 number systems-scientificnotation
2 number systems-scientificnotation
 
1 number systems-hex
1 number systems-hex1 number systems-hex
1 number systems-hex
 
1 number systems-unsignedsignedintegers
1 number systems-unsignedsignedintegers1 number systems-unsignedsignedintegers
1 number systems-unsignedsignedintegers
 
1 number systems-octal
1 number systems-octal1 number systems-octal
1 number systems-octal
 

Recently uploaded

31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...Nguyen Thanh Tu Collection
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Projectjordimapav
 
Sulphonamides, mechanisms and their uses
Sulphonamides, mechanisms and their usesSulphonamides, mechanisms and their uses
Sulphonamides, mechanisms and their usesVijayaLaxmi84
 
How to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 DatabaseHow to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 DatabaseCeline George
 
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptxDecoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptxDhatriParmar
 
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITWQ-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITWQuiz Club NITW
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptxmary850239
 
CLASSIFICATION OF ANTI - CANCER DRUGS.pptx
CLASSIFICATION OF ANTI - CANCER DRUGS.pptxCLASSIFICATION OF ANTI - CANCER DRUGS.pptx
CLASSIFICATION OF ANTI - CANCER DRUGS.pptxAnupam32727
 
ARTERIAL BLOOD GAS ANALYSIS........pptx
ARTERIAL BLOOD  GAS ANALYSIS........pptxARTERIAL BLOOD  GAS ANALYSIS........pptx
ARTERIAL BLOOD GAS ANALYSIS........pptxAneriPatwari
 
Indexing Structures in Database Management system.pdf
Indexing Structures in Database Management system.pdfIndexing Structures in Database Management system.pdf
Indexing Structures in Database Management system.pdfChristalin Nelson
 
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnvESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnvRicaMaeCastro1
 
Expanded definition: technical and operational
Expanded definition: technical and operationalExpanded definition: technical and operational
Expanded definition: technical and operationalssuser3e220a
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfJemuel Francisco
 
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...DhatriParmar
 
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...DhatriParmar
 
How to Fix XML SyntaxError in Odoo the 17
How to Fix XML SyntaxError in Odoo the 17How to Fix XML SyntaxError in Odoo the 17
How to Fix XML SyntaxError in Odoo the 17Celine George
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfVanessa Camilleri
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptxmary850239
 

Recently uploaded (20)

31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Project
 
Sulphonamides, mechanisms and their uses
Sulphonamides, mechanisms and their usesSulphonamides, mechanisms and their uses
Sulphonamides, mechanisms and their uses
 
How to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 DatabaseHow to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 Database
 
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptxINCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
 
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptxDecoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
 
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITWQ-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx
 
CLASSIFICATION OF ANTI - CANCER DRUGS.pptx
CLASSIFICATION OF ANTI - CANCER DRUGS.pptxCLASSIFICATION OF ANTI - CANCER DRUGS.pptx
CLASSIFICATION OF ANTI - CANCER DRUGS.pptx
 
prashanth updated resume 2024 for Teaching Profession
prashanth updated resume 2024 for Teaching Professionprashanth updated resume 2024 for Teaching Profession
prashanth updated resume 2024 for Teaching Profession
 
ARTERIAL BLOOD GAS ANALYSIS........pptx
ARTERIAL BLOOD  GAS ANALYSIS........pptxARTERIAL BLOOD  GAS ANALYSIS........pptx
ARTERIAL BLOOD GAS ANALYSIS........pptx
 
Indexing Structures in Database Management system.pdf
Indexing Structures in Database Management system.pdfIndexing Structures in Database Management system.pdf
Indexing Structures in Database Management system.pdf
 
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnvESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
 
Expanded definition: technical and operational
Expanded definition: technical and operationalExpanded definition: technical and operational
Expanded definition: technical and operational
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
 
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
 
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
 
How to Fix XML SyntaxError in Odoo the 17
How to Fix XML SyntaxError in Odoo the 17How to Fix XML SyntaxError in Odoo the 17
How to Fix XML SyntaxError in Odoo the 17
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdf
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx
 

9 big o-notation

  • 2. outline  Performance vs Complexity  Algorithm Efficiency  Big O Notation
  • 3. Performance vs Complexity • Performance:  How much time/memory/disk/… is used by the algorithm  Depends on the machine, compiler, code,… • Complexity:  How do the resource requirements of a program/algorithm scale Complexity affects performance but not the other way around If I have an algorithm with high complexity, it will take longer time to run. However, if I have a very slow program, that does not mean that my algorithm is very complex; that could be all other kind of issues that made the program run slow.
  • 4. Complexity of an Algorithm  We are going to count the number of the steps that are taken during the execution  Assumption (simplification):  Each step costs the same amount  +, -, return a value, access an array element, ….
  • 5. Complexity of an Algorithm (cont.) • Example 1: • private int getLastElement (int [] numbers) • { return numbers[numbers.length -1]; } Number of steps: 4 Constant time function (independent of array size) Notes: Let’s look at each step that need to be executed. It is always 4 steps to access the last element, no matter the number of elements.
  • 6. Complexity of an Algorithm (cont.) • Example 2: • private static int sum(int [] numbers) • { int sum = 0; for (int i = 0; i < number.length; i++){ sum = sum + number[i]; } return sum; } Outside loop: 3 + 2 Number of steps:6n + 5 Linear time functionLoop body: 6 INSTRUCTOR-NOTES:This time we use a loop. We will start by the steps that will be executed only once and the are outside the loop. Then we count the steps that are going to be repeated over and over again.
  • 7. Algorithm Efficiency  Let’s look at the following algorithm for initializing the values in an array: final int N = 500; int [] counts = new int[N]; for (int i=0; i<counts.length; i++) counts[i] = 0;  The length of time the algorithm takes to execute depends on the value of N
  • 8. Algorithm Efficiency (cont.)  In that algorithm, we have one loop that processes all of the elements in the array  Intuitively: – If N was half of its value, we would expect the algorithm to take half the time – If N was twice its value, we would expect the algorithm to take twice the time  That is true and we say that the algorithm efficiency relative to N is linear
  • 9. Algorithm Efficiency (cont.)  Let’s look at another algorithm for initializing the values in a different array: final int N = 500; int [] [] counts = new int[N][N]; for (int i=0; i<counts.length; i++) for (int j=0; j<counts[i].length; j++) counts[i][j] = 0;  The length of time the algorithm takes to execute still depends on the value of N
  • 10. Algorithm Efficiency (cont.)  However, in the second algorithm, we have two nested loops to process the elements in the two dimensional array  Intuitively: – If N is half its value, we would expect the algorithm to take one quarter the time – If N is twice its value, we would expect the algorithm to take quadruple the time  That is true and we say that the algorithm efficiency relative to N is quadratic
  • 11. Big O notation Why do we need Big-O?  In computer science, we use Big-O to classify algorithms based on their scalability.  Basically, it tells you how fast a function grows or declines.  The letter O is used because the rate of growth of a function is also called its order.  It also allows us to estimate the worst case running time of an algorithm as a function of the input size.
  • 12. Big O notation  Examples: – We can say that the first algorithm is O(n) – We can say that the second algorithm is O(n2)  For any algorithm that has a function g(n) of the parameter n that describes its length of time to execute, we can say the algorithm is O(g(n))  We only include the fastest growing term and ignore any multiplying by or adding of constants (which makes sense because those depend on the particular hardware the program is run on)
  • 13. Big O notation (cont.) Growth Functions: Eight functions O(n) that occur frequently in the analysis of algorithms (in order of increasing rate of growth relative to n): – Constant  1 – Logarithmic  log n – Linear  n – Log Linear  n log n – Quadratic  n2 – Cubic  n3 – Exponential  2n – Exhaustive Search  n!
  • 14. Big O notation (cont.) Growth Rates Compared:
  • 15. Big O notation (cont.)
  • 16. Big O notation (cont.)  The growth list is useful because of the following fact:  if a function f(n) is a sum of functions, one of which grows faster than the others, then the faster growing one determines the order of f(n).  Example:  If f(n) = 10 log(n) + 5 (log(n))3 + 7 n + 3 n 2 + 6 n 3,  then f(n) = O(n 3).
  • 17. Big O notation (cont.) Big-O for a Problem:  O(g(n)) for a problem means there is some O(g(n)) algorithm that solves the problem  Don’t assume that the specific algorithm that you are currently using is the best solution for the problem  There may be other correct algorithms that grow at a smaller rate with increasing n  Many times, the goal is to find an algorithm with the smallest possible growth rate
  • 18. Big O notation (cont.) Role of Data Structures:  That brings up the topic of the structure of the data on which the algorithm operates  If we are using an algorithm manually on some amount of data, we intuitively try to organize the data in a way that minimizes the number of steps that we need to take  Publishers offer dictionaries with the words listed in alphabetical order to minimize the length of time it takes us to look up a word
  • 19. Big O notation (cont.) Role of Data Structures:  We can do the same thing for algorithms in our computer programs  Example: Finding a numeric value in a list  If we assume that the list is unordered, we must search from the beginning to the end  On average, we will search half the list  Worst case, we will search the entire list  Algorithm is O(n), where n is size of array
  • 20. Big O notation (cont.) Example: int [] list = {7, 2, 9, 5, 6, 4}; for (int i=0; i<list.length, i++) if (value == list[i]) statement; // found it // didn’t find it
  • 21. Big O notation (cont.)  If we assume that the list is ordered, we can still search the entire list from the beginning to the end to determine if we have a match  But, we do not need to search that way  Because the values are in numerical order, we can use a binary search algorithm  Algorithm is O(logn), where n is size of array
  • 22. Big O notation (cont.) Role of Data Structures:  The difference in the structure of the data between an unordered list and an ordered list can be used to reduce algorithm Big-O  This is the role of data structures and why we study them  We need to be as clever in organizing our data efficiently as we are in figuring out an algorithm for processing it efficiently
  • 23. Big O notation (cont.) Example:  Find an order for  f(x) = 7 x5 + 5 x3 – x + 4  O(x5)
  • 24. Big O notation (cont.) Sorting Algorithms Big-O: http://bigocheatsheet.com/

Editor's Notes

  1. Complexity affects performance but not the other way around means: If I have an algorithm with high complexity, it will take longer time to run. However, if I have a very slow program, that does not mean that my algorithm is very complex; that could be all other kind of issues that made the program run slow.
  2. This time we use a loop. We will start by the steps that will be executed only once and the are outside the loop. Then we count the steps that are going to be repeated over and over again.