class: center, middle, dark # analyzing algorithms toddpla.github.io/analysing-algorithms --- class: center # contents ---- 1. why? 1. nth notation 1. big O 1. examples --- class: middle ### algorithm: ### a process or set of rules to be followed in calculations or other problem-solving operations, especially by a computer. .right[  ] --- class: center, middle, dark # why do we want to analyze algorithms? 🧐 ??? Problems have many solutions. With small inputs the difference in performance of these algorithms may be negligible. However, if we double the input or times it by 1000 or times it by infinity then we want to know how the performance will be effected. -- ##### scaling -- #### scaling -- ### scaling -- ## scaling -- # scaling --- class: center ## nth notation ----
https://en.wikipedia.org/wiki/Time_complexity ??? n is a function n --- .center[ ## some math 🤓 ] ---- -- ## exponential --
logarithmic
--
--
factorial
--
??? 52! ways to shuffle a pack of cards = 8.06... × 10
67
60! atoms in the universe = 8.32... × 10
81
--- class: center ## some numbers 🥳 ----
input
n!
2
n
n
2
n
log
2
(n)
1
1
1
1
1
1
0
1
2
2
4
2
2
1
1
3
6
8
9
3
1.58
1
4
24
16
16
4
2
1
5
120
32
25
5
2.32
1
6
720
64
36
6
2.58
1
7
5040
128
49
7
2.81
1
8
40320
256
64
8
3
1
9
362880
512
81
9
3.16
1
10
3628800
1024
100
10
3.32
1
--- class: center ## n = 1 ---- -- .left[ ```js function printFirstItemInSomeArray(someArray) { console.log(someArray[0]); } ```] --
test
.chart[
] --- class: center ## n = n ---- -- .left[```js function printAllItemsInSomeArrayOnce(someArray) { for (let i = 0; i < someArray.length; i++) { console.log(someArray[i]); } } ```] --
.chart[
] --- class: center ## n = n
2
---- -- .left[```js function printAllCombinationsInAnArray(someArray) { for (let i = 0; i < someArray.length; i++) { for (let j = 0; j < someArray.length; j++) { console.log(someArray[i], someArray[j]); } } } ```] --
.chart[
] --- class: center ## n = ??? ---- -- .left[```js function findInSortedArray(sortedArray, someNumber) { for (let i = 0; i < sortedArray.length; i++) { if (sortedArray[i] === someNumber) return i; } } ```] -- .left[```js const sortedArray = [6, 11, 13, 21, 26, 42, 64, 78, 82, 98]; ```] -- .left[```js console.log(findInSortedArray(sortedArray, 6)); //--> executions: 1 ```] -- .left[```js console.log(findInSortedArray(sortedArray, 21)); //--> executions: 4 ```] -- .left[```js console.log(findInSortedArray(sortedArray, 82)); //--> executions: 9 ```] --- class: center, middle, dark # o, O, Θ, Ω and ω --- class: center # o, O, Θ, Ω and ω ## Represent boundaries
little omicron
o(n)
faster
big omicron
O(n)
faster or equal
big theta
Θ(n)
equal to
big omega
Ω(n)
slower or equal
little omega
ω(n)
slower
??? Go back through each example and demonstrate the big O notation --- class: center ## n = log
2
(n) 🤯 ---- .left[ ```js function logarithmicSearch(sortedArray, item) { let left = 0; let right = sortedArray.length - 1; let middle; while (left <= right) { middle = Math.floor((left + right) / 2); if (sortedArray[middle] < item) left = middle + 1; else if (sortedArray[middle] > item) right = middle - 1; else return middle; } } ``` ]
.chart[
] --- ## summary 1. we found out that analysing algorithms is good for predicting how an algorithm will scale with increasing input 1. we looked at some commonly used nth notations including 1, n, n
2
, n! and log
2
(n) that helped us describe the different ways in which an algorithm might scale 1. we used the big O notation for helping us describe the upper bound of an algorithm 1. we compared two search algorithms that scaled differently --- class: center, middle  # thanks