SlideShare a Scribd company logo
1 of 73
Algorithm Complexity
&
Big-O Analysis
What is good code?
What is good code?
1. Readable
What is good code?
1. Readable
2. Scalable
What is good code?
1. Readable
2. Scalable
• Speed
• Memory
What is good code?
1. Readable
2. Scalable (Big-O)
• Speed
• Memory
What is good code?
(Big-O)
Big-O Notation
Big-O Notation
Big-O Notation
myValue = “Hello world” // O(1)
Big-O Notation
myValue = “Hello world” // O(1)
i input.length
myValue i // O(n)
Complexity
Complexity
Space Complexity
Space Complexity
Space Complexity
Space Complexity
Space Complexity
Space Complexity
O(2n + 13)
Space Complexity
O(n)
Space Complexity
Complexity
Complexity
Time Complexity
Time Complexity
Time Complexity
Time Complexity
Time Complexity
Time Complexity
O(4n + 3)
Time Complexity
O(n)
Time Complexity
Constant Time: O(1)
def
return
Time Complexity
Logarithmic Time: O(log n)
def
return
Time Complexity
Linear Time: O(n)
def
return
Time Complexity
Quadratic Time: O(n^2)
def
return result
Big-O Complexity Chart
Big-O Cheat Sheet
Linear Quadratic
Linear Quadratic
Linear Quadratic
Linear Quadratic
Linear Quadratic
Linear Quadratic
Example: Find Matching Pair
Problem:
Example: Find Matching Pair
8
[1,2,3,9]
Example: Find Matching Pair
8
[1,2,3,9] False
Example: Find Matching Pair
8
[1,2,3,9] False
[1,2,4,4]
Example: Find Matching Pair
8
[1,2,3,9] False
[1,2,4,4] True
Example: Find Matching Pair
8
[1,2,3,9] False
[1,2,4,4] True
Example: Find Matching Pair
8
[1,2,3,9] False
[1,2,4,4] True
Scalability Matters
Scalability Matters
Scalability Matters
Time Complexity
Read the documentation
Read the documentation
a
Read the documentation
Read the documentation
.count == // O(n)
Read the documentation
.isEmpty // O(1)
*Prefer HashSet instead of Array
HashSet/Dictionary Array/List
*Prefer HashSet instead of Array
HashSet/Dictionary Array/List
: Set< > = [1, 2, 3, 4, 5]
: Array< > = [1, 2, 3, 4, 5]
*Prefer HashSet instead of Array
HashSet/Dictionary Array/List
: Set< > = [1, 2, 3, 4, 5]
: Array< > = [1, 2, 3, 4, 5]
*Prefer HashSet instead of Array
HashSet/Dictionary Array/List
: Set< > = [1, 2, 3, 4, 5]
: Array< > = [1, 2, 3, 4, 5]
O(1)
Memoization
Memoization
Memoization
Memoization
32 .
Memoization
Memoization
Memoization
??
Memoization
0,08
Memoization
0,08
32
~400000 times faster ✅
Practice on HackerRank, LeetCode etc..
References
https://developer.apple.com/videos/play/wwdc2018/223
https://www.freecodecamp.org/news/big-o-notation-why-it-matters-and-why-it-doesnt-1674cfa8a23c
Introduction to Algorithms - MIT OpenCourseWare
Thank You

More Related Content

What's hot

Asymptotic notations
Asymptotic notationsAsymptotic notations
Asymptotic notations
Nikhil Sharma
 
Algorithm analysis
Algorithm analysisAlgorithm analysis
Algorithm analysis
sumitbardhan
 
Time and space complexity
Time and space complexityTime and space complexity
Time and space complexity
Ankit Katiyar
 

What's hot (20)

Complexity analysis - The Big O Notation
Complexity analysis - The Big O NotationComplexity analysis - The Big O Notation
Complexity analysis - The Big O Notation
 
Time andspacecomplexity
Time andspacecomplexityTime andspacecomplexity
Time andspacecomplexity
 
Quick Sort
Quick SortQuick Sort
Quick Sort
 
Asymptotic notations
Asymptotic notationsAsymptotic notations
Asymptotic notations
 
Chapter 09 design and analysis of algorithms
Chapter 09  design and analysis of algorithmsChapter 09  design and analysis of algorithms
Chapter 09 design and analysis of algorithms
 
Bubble sort
Bubble sortBubble sort
Bubble sort
 
Algorithm analysis
Algorithm analysisAlgorithm analysis
Algorithm analysis
 
Np complete
Np completeNp complete
Np complete
 
02 order of growth
02 order of growth02 order of growth
02 order of growth
 
Time and space complexity
Time and space complexityTime and space complexity
Time and space complexity
 
03 Analysis of Algorithms: Probabilistic Analysis
03 Analysis of Algorithms: Probabilistic Analysis03 Analysis of Algorithms: Probabilistic Analysis
03 Analysis of Algorithms: Probabilistic Analysis
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
 
Asymptotic Notation and Complexity
Asymptotic Notation and ComplexityAsymptotic Notation and Complexity
Asymptotic Notation and Complexity
 
Algorithm big o
Algorithm big oAlgorithm big o
Algorithm big o
 
AI 7 | Constraint Satisfaction Problem
AI 7 | Constraint Satisfaction ProblemAI 7 | Constraint Satisfaction Problem
AI 7 | Constraint Satisfaction Problem
 
Time and Space Complexity
Time and Space ComplexityTime and Space Complexity
Time and Space Complexity
 
Introduction to Algorithms Complexity Analysis
Introduction to Algorithms Complexity Analysis Introduction to Algorithms Complexity Analysis
Introduction to Algorithms Complexity Analysis
 
Data Structure: Algorithm and analysis
Data Structure: Algorithm and analysisData Structure: Algorithm and analysis
Data Structure: Algorithm and analysis
 
Asymptotic notation
Asymptotic notationAsymptotic notation
Asymptotic notation
 
Algorithms Lecture 7: Graph Algorithms
Algorithms Lecture 7: Graph AlgorithmsAlgorithms Lecture 7: Graph Algorithms
Algorithms Lecture 7: Graph Algorithms
 

Similar to Algorithm Complexity & Big-O Analysis

Radix Sorting With No Extra Space
Radix Sorting With No Extra SpaceRadix Sorting With No Extra Space
Radix Sorting With No Extra Space
gueste5dc45
 
Stack squeues lists
Stack squeues listsStack squeues lists
Stack squeues lists
James Wong
 
Stacksqueueslists
StacksqueueslistsStacksqueueslists
Stacksqueueslists
Fraboni Ec
 
Stacks queues lists
Stacks queues listsStacks queues lists
Stacks queues lists
Tony Nguyen
 
19 algorithms-and-complexity-110627100203-phpapp02
19 algorithms-and-complexity-110627100203-phpapp0219 algorithms-and-complexity-110627100203-phpapp02
19 algorithms-and-complexity-110627100203-phpapp02
Muhammad Aslam
 
AlgorithmTime ComplexitySpace ComplexityFeaturesLinear Searc.pdf
AlgorithmTime ComplexitySpace ComplexityFeaturesLinear Searc.pdfAlgorithmTime ComplexitySpace ComplexityFeaturesLinear Searc.pdf
AlgorithmTime ComplexitySpace ComplexityFeaturesLinear Searc.pdf
rdtraders2007
 
VRP2013 - Comp Aspects VRP
VRP2013 - Comp Aspects VRPVRP2013 - Comp Aspects VRP
VRP2013 - Comp Aspects VRP
Victor Pillac
 
19. algorithms and-complexity
19. algorithms and-complexity19. algorithms and-complexity
19. algorithms and-complexity
showkat27
 

Similar to Algorithm Complexity & Big-O Analysis (20)

Time_Complexity.pptx
Time_Complexity.pptxTime_Complexity.pptx
Time_Complexity.pptx
 
Asymptotic notations
Asymptotic notationsAsymptotic notations
Asymptotic notations
 
Radix Sorting With No Extra Space
Radix Sorting With No Extra SpaceRadix Sorting With No Extra Space
Radix Sorting With No Extra Space
 
LogMap: Large-scale, Logic-based and Interactive Ontology Matching
LogMap: Large-scale, Logic-based and Interactive Ontology MatchingLogMap: Large-scale, Logic-based and Interactive Ontology Matching
LogMap: Large-scale, Logic-based and Interactive Ontology Matching
 
Data Structure and Algorithms
Data Structure and Algorithms Data Structure and Algorithms
Data Structure and Algorithms
 
Asymptotic Notations
Asymptotic NotationsAsymptotic Notations
Asymptotic Notations
 
Stack squeues lists
Stack squeues listsStack squeues lists
Stack squeues lists
 
Stacks queues lists
Stacks queues listsStacks queues lists
Stacks queues lists
 
Stacksqueueslists
StacksqueueslistsStacksqueueslists
Stacksqueueslists
 
Stacks queues lists
Stacks queues listsStacks queues lists
Stacks queues lists
 
Stacks queues lists
Stacks queues listsStacks queues lists
Stacks queues lists
 
Stacks queues lists
Stacks queues listsStacks queues lists
Stacks queues lists
 
18103010 algorithm complexity (iterative)
18103010 algorithm complexity (iterative)18103010 algorithm complexity (iterative)
18103010 algorithm complexity (iterative)
 
19 algorithms-and-complexity-110627100203-phpapp02
19 algorithms-and-complexity-110627100203-phpapp0219 algorithms-and-complexity-110627100203-phpapp02
19 algorithms-and-complexity-110627100203-phpapp02
 
AlgorithmTime ComplexitySpace ComplexityFeaturesLinear Searc.pdf
AlgorithmTime ComplexitySpace ComplexityFeaturesLinear Searc.pdfAlgorithmTime ComplexitySpace ComplexityFeaturesLinear Searc.pdf
AlgorithmTime ComplexitySpace ComplexityFeaturesLinear Searc.pdf
 
Basic data structures part I
Basic data structures part IBasic data structures part I
Basic data structures part I
 
VRP2013 - Comp Aspects VRP
VRP2013 - Comp Aspects VRPVRP2013 - Comp Aspects VRP
VRP2013 - Comp Aspects VRP
 
Lecture1
Lecture1Lecture1
Lecture1
 
19. algorithms and-complexity
19. algorithms and-complexity19. algorithms and-complexity
19. algorithms and-complexity
 
TimeComplexity.pptx
TimeComplexity.pptxTimeComplexity.pptx
TimeComplexity.pptx
 

Recently uploaded

Verification of thevenin's theorem for BEEE Lab (1).pptx
Verification of thevenin's theorem for BEEE Lab (1).pptxVerification of thevenin's theorem for BEEE Lab (1).pptx
Verification of thevenin's theorem for BEEE Lab (1).pptx
chumtiyababu
 
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
AldoGarca30
 
DeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakesDeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakes
MayuraD1
 
Hospital management system project report.pdf
Hospital management system project report.pdfHospital management system project report.pdf
Hospital management system project report.pdf
Kamal Acharya
 
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Kandungan 087776558899
 

Recently uploaded (20)

COST-EFFETIVE and Energy Efficient BUILDINGS ptx
COST-EFFETIVE  and Energy Efficient BUILDINGS ptxCOST-EFFETIVE  and Energy Efficient BUILDINGS ptx
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
 
Verification of thevenin's theorem for BEEE Lab (1).pptx
Verification of thevenin's theorem for BEEE Lab (1).pptxVerification of thevenin's theorem for BEEE Lab (1).pptx
Verification of thevenin's theorem for BEEE Lab (1).pptx
 
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced LoadsFEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
 
Wadi Rum luxhotel lodge Analysis case study.pptx
Wadi Rum luxhotel lodge Analysis case study.pptxWadi Rum luxhotel lodge Analysis case study.pptx
Wadi Rum luxhotel lodge Analysis case study.pptx
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
Employee leave management system project.
Employee leave management system project.Employee leave management system project.
Employee leave management system project.
 
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
 
DeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakesDeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakes
 
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptxA CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
 
Hospital management system project report.pdf
Hospital management system project report.pdfHospital management system project report.pdf
Hospital management system project report.pdf
 
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptxOrlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
 
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptxHOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
 
Online food ordering system project report.pdf
Online food ordering system project report.pdfOnline food ordering system project report.pdf
Online food ordering system project report.pdf
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the start
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torque
 
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
 
Computer Networks Basics of Network Devices
Computer Networks  Basics of Network DevicesComputer Networks  Basics of Network Devices
Computer Networks Basics of Network Devices
 
Work-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxWork-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptx
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
 
Online electricity billing project report..pdf
Online electricity billing project report..pdfOnline electricity billing project report..pdf
Online electricity billing project report..pdf
 

Algorithm Complexity & Big-O Analysis

Editor's Notes

  1. space (amount of memory)
  2. https://www.bigocheatsheet.com/
  3.  time complexity (number of operations).
  4. https://tr.surveymonkey.com/r/R3FHYQB