@JoeKarlsson1
!
1
Big-O
Notation
www.joekarlsson.com
A Gentle Introduction
Slide 2
Slide 2 text
@JoeKarlsson1
!
2
Joe Karlsson
@JOEKARLSSON1
@JOEKARLSSON
JOEKARLSSON.COM
Slide 3
Slide 3 text
@JoeKarlsson1
!
3
How is this going to work?
Slide 4
Slide 4 text
@JoeKarlsson1
!
4
What is Big-O?
Slide 5
Slide 5 text
@JoeKarlsson1
!
5
Not that big O
Slide 6
Slide 6 text
@JoeKarlsson1
!
6
Measures how an app
will scale when you increase the
amount of things it operates on
Slide 7
Slide 7 text
@JoeKarlsson1
!
7
Or
“How slow is thing gonna go?”
Slide 8
Slide 8 text
@JoeKarlsson1
!
8
Why should you care?
Slide 9
Slide 9 text
@JoeKarlsson1
!
9
Why should you care?
•Two types of estimation in software
•Machine independent
•Covers all instances of a problem
•Allows us to compare algorithms for
a problem
Slide 10
Slide 10 text
@JoeKarlsson1
!
10
But most importantly
INTERVIEWS!
@JoeKarlsson1
!
13
•We are talking about really, really
big numbers of elements
•We are assuming worst case
scenarios for algorithms
•Big O can be really hard!
Slide 14
Slide 14 text
@JoeKarlsson1
!
14
Constant Time
(1)
Slide 15
Slide 15 text
@JoeKarlsson1
!
15
O(1): Constant Time
No matter how large the input
is, the time taken to run
doesn’t change
Slide 16
Slide 16 text
@JoeKarlsson1
!
16
Elements
Time
Big O Complexity Graph
(1)
Slide 17
Slide 17 text
@JoeKarlsson1
!
17
MRW my
code runs
in O(1)
Excellent!
Slide 18
Slide 18 text
@JoeKarlsson1
!
18
O(1): Constant Time
Slide 19
Slide 19 text
@JoeKarlsson1
!
19
O(1): Constant Time
Slide 20
Slide 20 text
@JoeKarlsson1
!
20
Logarithmic Time
(logn)
Slide 21
Slide 21 text
@JoeKarlsson1
!
21
O(log n): Logarithmic Time
Any algorithm that cuts the
problem in half each time
Slide 22
Slide 22 text
@JoeKarlsson1
!
22
Elements
Time
Big O Complexity Graph
(logn)
(1)
Slide 23
Slide 23 text
@JoeKarlsson1
!
23
MRW my code
runs in
O(log n)
Excellent!
Slide 24
Slide 24 text
@JoeKarlsson1
!
24
Slide 25
Slide 25 text
@JoeKarlsson1
!
25
Linear Time
(n)
Slide 26
Slide 26 text
@JoeKarlsson1
!
26
O(n): Linear Time
The larger the input,
The longer it takes, in an
even tradeoff.
Slide 27
Slide 27 text
@JoeKarlsson1
!
27
O(n): Linear Time
Every time you double the
number of elements, the
operation will take twice as
long
Slide 28
Slide 28 text
@JoeKarlsson1
!
28
Elements
Time
Big O Complexity Graph
(n)
(logn)
(1)
Slide 29
Slide 29 text
@JoeKarlsson1
!
29
MRW my code
runs in
O(n)
Good
Slide 30
Slide 30 text
@JoeKarlsson1
!
30
Slide 31
Slide 31 text
@JoeKarlsson1
!
31
Lin Log Time
(n log n)
Slide 32
Slide 32 text
@JoeKarlsson1
!
32
This one is kinda confusing…
Slide 33
Slide 33 text
@JoeKarlsson1
!
33
O(n log(n)): LinLog Time
Perform an O(log n) operation
for each item in your input
Slide 34
Slide 34 text
@JoeKarlsson1
!
34
O(n log(n)): LinLog Time
Most efficient sorting
algorithms are an example of
this
Slide 35
Slide 35 text
@JoeKarlsson1
!
35
Sorting Algorithm Run Times
Slide 36
Slide 36 text
@JoeKarlsson1
!
36
Merge
Sort
OK
Slide 37
Slide 37 text
@JoeKarlsson1
!
37
Slide 38
Slide 38 text
@JoeKarlsson1
!
38
Elements
Time
Big O Complexity Graph
(logn)
(1)
(n log n)
(n)
Slide 39
Slide 39 text
@JoeKarlsson1
!
39
MRW my code
runs in
O(n log n)
OK
Slide 40
Slide 40 text
@JoeKarlsson1
!
40
Quadratic Time
(n2)
Slide 41
Slide 41 text
@JoeKarlsson1
!
41
O(n^2): Quadratic Time
Basically any
double nested for loop
Slide 42
Slide 42 text
@JoeKarlsson1
!
42
Slide 43
Slide 43 text
@JoeKarlsson1
!
43
Elements
Time
(logn)
(1)
(n log n)
(n)
Big O Complexity Graph
(n2)
Slide 44
Slide 44 text
@JoeKarlsson1
!
44
MRW my code
runs in
O(n^2)
BAD
Slide 45
Slide 45 text
@JoeKarlsson1
!
45
Exponential Time
(2n)
Slide 46
Slide 46 text
@JoeKarlsson1
!
46
O(2^n): Exponential Time
The time taken will double
with each additional element
in the input data set
Slide 47
Slide 47 text
@JoeKarlsson1
!
47
Slide 48
Slide 48 text
@JoeKarlsson1
(logn)
(1)
(n log n)
(n)
(n2)
!
48
Elements
Time
Big O Complexity Graph
(2n)
Slide 49
Slide 49 text
@JoeKarlsson1
!
49
MRW my code
runs in
O(2^n)
VERY BAD
Slide 50
Slide 50 text
@JoeKarlsson1
!
50
But wait, it gets worse…
Slide 51
Slide 51 text
@JoeKarlsson1
!
51
Slide 52
Slide 52 text
@JoeKarlsson1
!
52
Factorial Time
(n!)
Slide 53
Slide 53 text
@JoeKarlsson1
!
53
O(n!): Factorial Time
4! === (4*3*2*1)
Slide 54
Slide 54 text
@JoeKarlsson1
!
54
O(n!): Factorial Time
Involves doing something for
all possible permutations
Slide 55
Slide 55 text
@JoeKarlsson1
!
55
O(n!) Examples
• Brute force password crackers
• Traveling Salesman Problem
Slide 56
Slide 56 text
@JoeKarlsson1
!
56
Traveling
Salesman
Problem
Slide 57
Slide 57 text
@JoeKarlsson1
(logn)
(1)
(n log n)
(n)
(n2)
(2n)
!
57
Elements
Time
Big O Complexity Graph
(n!)
Slide 58
Slide 58 text
@JoeKarlsson1
!
58
MRW my code
runs in
O(n!)
THE WORST
Slide 59
Slide 59 text
@JoeKarlsson1
!
59
How do I get better at Big O?
• Figure out the run time as your write
functions.
• When reviewing PRs by other devs
• Practice!
Slide 60
Slide 60 text
@JoeKarlsson1
!
60
• Big O Notation allows us to compare
algorithms for a problem
• Big O Can be confusing
Slide 61
Slide 61 text
@JoeKarlsson1
(logn)
(1)
(n log n)
(n)
(n2)
(2n)
!
61
Elements
Time
Big O Complexity Graph
(n!)
Slide 62
Slide 62 text
@JoeKarlsson1
!
62
Questions?
Slide 63
Slide 63 text
@JoeKarlsson1
!
63
Live long
and prosper
Slide 64
Slide 64 text
@JoeKarlsson1
!
64
Additional Resources
• Plain English Explanation of Big O:
• https://stackoverflow.com/questions/487258/plain-english-explanation-of-big-o
• Big O [Wikipedia]:
• https://en.wikipedia.org/wiki/Big_O_notation
• Big O Cheatsheet
• http://bigocheatsheet.com/
Slide 65
Slide 65 text
@JoeKarlsson1
!
65
Let’s Stay In Touch
@JOEKARLSSON1
@JOEKARLSSON
JOEKARLSSON.COM