Partial preview of the text
Download cheetsheet cheetsheet and more Cheat Sheet Mathematics in PDF only on Docsity!
Big-O Notation @komranahmedse Algorithm consumes more memory Tells how well an algorithm performs as the input size grows called Space Complexity 7 < Algorithm takes more time this growth affects our algorithms in two ways Possible Complexities called Time Complexity Possible algorithmic complexities can be one oft means linear complexity 0) Complexity grows lineorly over time - higher the ow =1 umber of inputs, higher the complexity. 002) = 2g. looping over all the items of an array 06) = 3 0C4) = for Clet i= 0; 7 <= my ite re ce M1 do something bots =n 3 means quadratic complexity 2 on") Complexity squares the number of inputs 0a) =) 2g. loop within a loop 002) = 4 003) = 4 for (let i= 0; 1 <= my itt) E 004) = 6 for Cet y = 0; y <= n5 yt) E . 1 do something O(n) =n? 3 z oc) Mane constant comply ow =1 No matter the input size, complexity remains the some 002) =1 eg. accessing element at index from an array 0G) =) 004) = let 0 = fl, 2, 3, 4, SI [= consolesogColtD); OG) = 1 nny means exponential complexity ow =) O(2") complexity doubles with each addition to the input dotoset 0(2) = 4 2g, looping over all possible combinations of an array 0G) = 8 004) = 16 function fbonacciln) € ee if Gn c= 1) return mn; 20) = 6 return fbonacci( ~ 2) + bonacciCw-t); ~ ; O(n) = 2 means logarithmic complexity O(log 1) complexity goes up linearly while the input goes up exponentially eg. here is the example log 2 oto) =1 for (let i = |; 7 <=; . 0(200) = 2 " 0(300) = 3 console JogC hello"); 3 0(400) = 4 0(s00) = 5 0(600) = 6 another famous example is binary search O(n) = log n AN ABCDEFGHITKLMNOPQRSTUVIKYZ What if my algorithm has multiple complexities? In that case toke the worst case and that would be your algorithmic complexity. O(n) + OGn?) == OCr*)