You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Another more real life example is adding an element to the begining of a <<Linked List>>. You can check out the implementation <<linked-list-inserting-beginning, here>>.
42
+
// Another more real life example is adding an element to the begining of a <<Linked List>>. You can check out the implementation <<linked-list-inserting-beginning, here>>.
43
43
44
-
As you can see, in both examples (array and linked list) if the input is a collection of 10 elements or 10M it would take the same amount of time to execute. You can't get any more performance than this!
44
+
// As you can see, in both examples (array and linked list) if the input is a collection of 10 elements or 10M it would take the same amount of time to execute. You can't get any more performance than this!
45
45
46
-
==== Logarithmic
47
-
(((Logarithmic)))
48
-
(((Runtime, Logarithmic)))
49
-
Represented in Big O notation as *O(log n)*, when an algorithm has this running time it means that as the size of the input grows the number of operations grows very slowly. Logarithmic algorithms are very scalable. One example is the *binary search*.
50
-
indexterm:[Runtime, Logarithmic]
46
+
// ==== Logarithmic
47
+
// (((Logarithmic)))
48
+
// (((Runtime, Logarithmic)))
49
+
// Represented in Big O notation as *O(log n)*, when an algorithm has this running time it means that as the size of the input grows the number of operations grows very slowly. Logarithmic algorithms are very scalable. One example is the *binary search*.
50
+
// indexterm:[Runtime, Logarithmic]
51
51
52
-
[#logarithmic-example]
53
-
===== Searching on a sorted array
52
+
// [#logarithmic-example]
53
+
// ===== Searching on a sorted array
54
54
55
-
The binary search only works for sorted lists. It starts searching for an element on the middle of the array and then it moves to the right or left depending if the value you are looking for is bigger or smaller.
55
+
// The binary search only works for sorted lists. It starts searching for an element on the middle of the array and then it moves to the right or left depending if the value you are looking for is bigger or smaller.
This binary search implementation is a recursive algorithm, which means that the function `binarySearch` calls itself multiple times until the solution is found. The binary search split the array in half every time.
64
+
// This binary search implementation is a recursive algorithm, which means that the function `binarySearch` calls itself multiple times until the solution is found. The binary search split the array in half every time.
65
65
66
-
Finding the runtime of recursive algorithms is not very obvious sometimes. It requires some tools like recursion trees or the https://adrianmejia.com/blog/2018/04/24/analysis-of-recursive-algorithms/[Master Theorem]. The `binarySearch` divides the input in half each time. As a rule of thumb, when you have an algorithm that divides the data in half on each call you are most likely in front of a logarithmic runtime: _O(log n)_.
66
+
// Finding the runtime of recursive algorithms is not very obvious sometimes. It requires some tools like recursion trees or the https://adrianmejia.com/blog/2018/04/24/analysis-of-recursive-algorithms/[Master Theorem]. The `binarySearch` divides the input in half each time. As a rule of thumb, when you have an algorithm that divides the data in half on each call you are most likely in front of a logarithmic runtime: _O(log n)_.
67
67
68
-
[[linear]]
69
-
==== Linear
70
-
(((Linear)))
71
-
(((Runtime, Linear)))
72
-
Linear algorithms are one of the most common runtimes. It’s represented as *O(n)*. Usually, an algorithm has a linear running time when it iterates over all the elements in the input.
68
+
// [[linear]]
69
+
// ==== Linear
70
+
// (((Linear)))
71
+
// (((Runtime, Linear)))
72
+
// Linear algorithms are one of the most common runtimes. It’s represented as *O(n)*. Usually, an algorithm has a linear running time when it iterates over all the elements in the input.
73
73
74
-
[#linear-example]
75
-
===== Finding duplicates in an array using a map
74
+
// [#linear-example]
75
+
// ===== Finding duplicates in an array using a map
76
76
77
-
Let’s say that we want to find duplicate elements in an array. What’s the first implementation that comes to mind? Check out this implementation:
77
+
// Let’s say that we want to find duplicate elements in an array. What’s the first implementation that comes to mind? Check out this implementation:
* *Best-case scenario*: first two elements are duplicates. It only has to visit two elements.
88
-
* *Worst-case scenario*: no duplicated or duplicated are the last two. In either case, it has to visit every item on the array.
89
-
* *Average-case scenario*: duplicates are somewhere in the middle of the collection. Only, half of the array will be visited.
86
+
// .`hasDuplicates` has multiple scenarios:
87
+
// * *Best-case scenario*: first two elements are duplicates. It only has to visit two elements.
88
+
// * *Worst-case scenario*: no duplicated or duplicated are the last two. In either case, it has to visit every item on the array.
89
+
// * *Average-case scenario*: duplicates are somewhere in the middle of the collection. Only, half of the array will be visited.
90
90
91
-
As we learned before, the big O cares about the worst-case scenario, where we would have to visit every element on the array. So, we have an *O(n)* runtime.
91
+
// As we learned before, the big O cares about the worst-case scenario, where we would have to visit every element on the array. So, we have an *O(n)* runtime.
92
92
93
-
Space complexity is also *O(n)* since we are using an auxiliary data structure. We have a map that in the worst case (no duplicates) it will hold every word.
93
+
// Space complexity is also *O(n)* since we are using an auxiliary data structure. We have a map that in the worst case (no duplicates) it will hold every word.
94
94
95
-
==== Linearithmic
96
-
(((Linearithmic)))
97
-
(((Runtime, Linearithmic)))
98
-
An algorithm with a linearithmic runtime is represented as _O(n log n)_. This one is important because it is the best runtime for sorting! Let’s see the merge-sort.
95
+
// ==== Linearithmic
96
+
// (((Linearithmic)))
97
+
// (((Runtime, Linearithmic)))
98
+
// An algorithm with a linearithmic runtime is represented as _O(n log n)_. This one is important because it is the best runtime for sorting! Let’s see the merge-sort.
99
99
100
-
[#linearithmic-example]
101
-
===== Sorting elements in an array
100
+
// [#linearithmic-example]
101
+
// ===== Sorting elements in an array
102
102
103
-
The ((Merge Sort)), like its name indicates, has two functions merge and sort. Let’s start with the sort function:
103
+
// The ((Merge Sort)), like its name indicates, has two functions merge and sort. Let’s start with the sort function:
The merge function combines two sorted arrays in ascending order. Let’s say that we want to sort the array `[9, 2, 5, 1, 7, 6]`. In the following illustration, you can see what each function does.
124
+
// The merge function combines two sorted arrays in ascending order. Let’s say that we want to sort the array `[9, 2, 5, 1, 7, 6]`. In the following illustration, you can see what each function does.
125
125
126
-
.Mergesort visualization. Shows the split, sort and merge steps
How do we obtain the running time of the merge sort algorithm? The mergesort divides the array in half each time in the split phase, _log n_, and the merge function join each splits, _n_. The total work we have *O(n log n)*. There more formal ways to reach to this runtime like using the https://adrianmejia.com/blog/2018/04/24/analysis-of-recursive-algorithms/[Master Method] and https://www.cs.cornell.edu/courses/cs3110/2012sp/lectures/lec20-master/lec20.html[recursion trees].
129
+
// How do we obtain the running time of the merge sort algorithm? The mergesort divides the array in half each time in the split phase, _log n_, and the merge function join each splits, _n_. The total work we have *O(n log n)*. There more formal ways to reach to this runtime like using the https://adrianmejia.com/blog/2018/04/24/analysis-of-recursive-algorithms/[Master Method] and https://www.cs.cornell.edu/courses/cs3110/2012sp/lectures/lec20-master/lec20.html[recursion trees].
130
130
131
-
[[quadratic]]
132
-
==== Quadratic
133
-
(((Quadratic)))
134
-
(((Runtime, Quadratic)))
135
-
Running times that are quadratic, O(n^2^), are the ones to watch out for. They usually don’t scale well when they have a large amount of data to process.
131
+
// [[quadratic]]
132
+
// ==== Quadratic
133
+
// (((Quadratic)))
134
+
// (((Runtime, Quadratic)))
135
+
// Running times that are quadratic, O(n^2^), are the ones to watch out for. They usually don’t scale well when they have a large amount of data to process.
136
136
137
-
Usually, they have double-nested loops that where each one visits all or most elements in the input. One example of this is a naïve implementation to find duplicate words on an array.
137
+
// Usually, they have double-nested loops that where each one visits all or most elements in the input. One example of this is a naïve implementation to find duplicate words on an array.
138
138
139
-
[#quadratic-example]
140
-
===== Finding duplicates in an array (naïve approach)
139
+
// [#quadratic-example]
140
+
// ===== Finding duplicates in an array (naïve approach)
141
141
142
-
If you remember we have solved this problem more efficiently on the <<Linear, Linear>> section. We solved this problem before using an _O(n)_, let’s solve it this time with an _O(n^2^)_:
142
+
// If you remember we have solved this problem more efficiently on the <<Linear, Linear>> section. We solved this problem before using an _O(n)_, let’s solve it this time with an _O(n^2^)_:
As you can see, we have two nested loops causing the running time to be quadratic. How much different is a linear vs. quadratic algorithm?
152
+
// As you can see, we have two nested loops causing the running time to be quadratic. How much different is a linear vs. quadratic algorithm?
153
153
154
-
Let’s say you want to find a duplicated middle name in a phone directory book of a city of ~1 million people. If you use this quadratic solution you would have to wait for ~12 days to get an answer [big]#🐢#; while if you use the <<Linear, linear solution>> you will get the answer in seconds! [big]#🚀#
154
+
// Let’s say you want to find a duplicated middle name in a phone directory book of a city of ~1 million people. If you use this quadratic solution you would have to wait for ~12 days to get an answer [big]#🐢#; while if you use the <<Linear, linear solution>> you will get the answer in seconds! [big]#🚀#
155
155
156
-
==== Cubic
157
-
(((Cubic)))
158
-
(((Runtime, Cubic)))
159
-
Cubic *O(n^3^)* and higher polynomial functions usually involve many nested loops. As an example of a cubic algorithm is a multi-variable equation solver (using brute force):
156
+
// ==== Cubic
157
+
// (((Cubic)))
158
+
// (((Runtime, Cubic)))
159
+
// Cubic *O(n^3^)* and higher polynomial functions usually involve many nested loops. As an example of a cubic algorithm is a multi-variable equation solver (using brute force):
160
160
161
-
[#cubic-example]
162
-
===== Solving a multi-variable equation
161
+
// [#cubic-example]
162
+
// ===== Solving a multi-variable equation
163
163
164
-
Let’s say we want to find the solution for this multi-variable equation:
164
+
// Let’s say we want to find the solution for this multi-variable equation:
165
165
166
-
_3x + 9y + 8z = 79_
166
+
// _3x + 9y + 8z = 79_
167
167
168
-
A naïve approach to solve this will be the following program:
168
+
// A naïve approach to solve this will be the following program:
WARNING: This just an example, there are better ways to solve multi-variable equations.
178
+
// WARNING: This just an example, there are better ways to solve multi-variable equations.
179
179
180
-
As you can see three nested loops usually translates to O(n^3^). If you have a four variable equation and four nested loops it would be O(n^4^) and so on when we have a runtime in the form of _O(n^c^)_, where _c > 1_, we can refer as a *polynomial runtime*.
180
+
// As you can see three nested loops usually translates to O(n^3^). If you have a four variable equation and four nested loops it would be O(n^4^) and so on when we have a runtime in the form of _O(n^c^)_, where _c > 1_, we can refer as a *polynomial runtime*.
181
181
182
-
==== Exponential
183
-
(((Exponential)))
184
-
(((Runtime, Exponential)))
185
-
Exponential runtimes, O(2^n^), means that every time the input grows by one the number of operations doubles. Exponential programs are only usable for a tiny number of elements (<100) otherwise it might not finish on your lifetime. [big]#💀#
182
+
// ==== Exponential
183
+
// (((Exponential)))
184
+
// (((Runtime, Exponential)))
185
+
// Exponential runtimes, O(2^n^), means that every time the input grows by one the number of operations doubles. Exponential programs are only usable for a tiny number of elements (<100) otherwise it might not finish on your lifetime. [big]#💀#
186
186
187
-
Let’s do an example.
187
+
// Let’s do an example.
188
188
189
-
[#exponential-example]
190
-
===== Finding subsets of a set
189
+
// [#exponential-example]
190
+
// ===== Finding subsets of a set
191
191
192
-
Finding all distinct subsets of a given set can be implemented as follows:
192
+
// Finding all distinct subsets of a given set can be implemented as follows:
// <2> For each element from the input append it to the results array.
203
+
// <3> The new results array will be what it was before + the duplicated with the appended element.
204
204
205
-
//.The way this algorithm generates all subsets is:
206
-
//1. The base case is an empty element (line 13). E.g. ['']
207
-
//2. For each element from the input append it to the results array (line 16)
208
-
//3. The new results array will be what it was before + the duplicated with the appended element (line 17)
205
+
// //.The way this algorithm generates all subsets is:
206
+
// //1. The base case is an empty element (line 13). E.g. ['']
207
+
// //2. For each element from the input append it to the results array (line 16)
208
+
// //3. The new results array will be what it was before + the duplicated with the appended element (line 17)
209
209
210
-
Every time the input grows by one the resulting array doubles. That’s why it has an *O(2^n^)*.
210
+
// Every time the input grows by one the resulting array doubles. That’s why it has an *O(2^n^)*.
211
211
212
-
==== Factorial
213
-
(((Factorial)))
214
-
(((Runtime, Factorial)))
215
-
Factorial runtime, O(n!), is not scalable at all. Even with input sizes of ~10 elements, it will take a couple of seconds to compute. It’s that slow! [big]*🍯🐝*
212
+
// ==== Factorial
213
+
// (((Factorial)))
214
+
// (((Runtime, Factorial)))
215
+
// Factorial runtime, O(n!), is not scalable at all. Even with input sizes of ~10 elements, it will take a couple of seconds to compute. It’s that slow! [big]*🍯🐝*
216
216
217
-
.Factorial
218
-
****
219
-
A factorial is the multiplication of all the numbers less than itself down to 1.
217
+
// .Factorial
218
+
// ****
219
+
// A factorial is the multiplication of all the numbers less than itself down to 1.
220
220
221
-
.For instance:
222
-
- 3! = 3 x 2 x 1 = 6
223
-
- 5! = 5 x 4 x 3 x 2 x 1 = 120
224
-
- 10! = 3,628,800
225
-
- 11! = 39,916,800
226
-
****
221
+
// .For instance:
222
+
// - 3! = 3 x 2 x 1 = 6
223
+
// - 5! = 5 x 4 x 3 x 2 x 1 = 120
224
+
// - 10! = 3,628,800
225
+
// - 11! = 39,916,800
226
+
// ****
227
227
228
-
[#factorial-example]
229
-
===== Getting all permutations of a word
230
-
(((Permutations)))
231
-
(((Words permutations)))
232
-
One classic example of an _O(n!)_ algorithm is finding all the different words that can be formed with a given set of letters.
228
+
// [#factorial-example]
229
+
// ===== Getting all permutations of a word
230
+
// (((Permutations)))
231
+
// (((Words permutations)))
232
+
// One classic example of an _O(n!)_ algorithm is finding all the different words that can be formed with a given set of letters.
As you can see in the `getPermutations` function, the resulting array is the factorial of the word length.
241
+
// As you can see in the `getPermutations` function, the resulting array is the factorial of the word length.
242
242
243
-
Factorial start very slow and then it quickly becomes uncontrollable. A word size of just 11 characters would take a couple of hours in most computers!
244
-
[big]*🤯*
243
+
// Factorial start very slow and then it quickly becomes uncontrollable. A word size of just 11 characters would take a couple of hours in most computers!
244
+
// [big]*🤯*
245
245
246
-
==== Summary
246
+
// ==== Summary
247
247
248
-
We went through 8 of the most common time complexities and provided examples for each of them. Hopefully, this will give you a toolbox to analyze algorithms.
249
-
(((Tables, Intro, Common time complexities and examples)))
248
+
// We went through 8 of the most common time complexities and provided examples for each of them. Hopefully, this will give you a toolbox to analyze algorithms.
249
+
// (((Tables, Intro, Common time complexities and examples)))
250
250
251
-
// tag::table[]
252
-
.Most common algorithmic running times and their examples
253
-
[cols="2,2,5",options="header"]
254
-
|===
255
-
|Big O Notation
256
-
|Name
257
-
|Example(s)
251
+
// // tag::table[]
252
+
// .Most common algorithmic running times and their examples
0 commit comments