26 down vote favorite 12c++ |
I've been learning more about Big O Notation and how to calculate it based on how an algorithm is written. I came across an interesting set of "rules" for calculating an algorithms Big O notation and I wanted to see if I'm on the right track or way off.ide Big O Notation: Noop function(n) { For(var a = 0; i <= n; i++) { // It's N because it's just a single loop // Do stuff } } Big O Notation: N2this function(n, b) { For(var a = 0; a <= n; a++) { For(var c = 0; i <= b; c++) { // It's N squared because it's two nested loops // Do stuff } } } Big O Notation: 2Nrest function(n, b) { For(var a = 0; a <= n; a++) { // Do stuff } For(var c = 0; i <= b; c++) { // It's 2N the loops are outside each other // Do stuff } } Big O Notation: NLogNcode function(n) { n.sort(); // The NLogN comes from the sort? For(var a = 0; i <= n; i++) { // Do stuff } } |