Big O Simply Explained
- 时间:2020-10-07 14:14:07
- 分类:网络文摘
- 阅读:96 次
Big O Notation is a must known concept in software engineer algorithm complexity analysis. You will be asked the Big O during your software engineer interview, for sure.
Big O notation is a simplification and approximation for assessing algorithmic efficiency for memory and time based on the algorithm inputs. The important bit there is “inputs”. You use the bigO to try and figure out how an algorithm will scale with large inputs. Often people use “n” as a variable for big O, but this is really just a variable that needs to be defined, perhaps a variable for one of your inputs. In this case “n” is n = s.length. If you have multiple inputs in which you are looping on, your big O needs to factor those in. In your example, the only variable we care about on the loop is “s.length”.
Often in the real world when assessing algorithms you really care about constant O(1), logarithmic O(logn), linear O(n) and exponential O(n^x). Usually any exponential function just doesn’t scale. It is fine to use for things where n is small or maybe not a critical function in something that needs to be performant but its worth considering. I haven’t seen in many cases where exponential algorithms are broken down more finitely but I am sure it does happen on some systems where a complex graph problem is the core problem of a solution.
With that said, big O is strictly a way to categorize and bucketize algorithms. There are some algorithms with quite complex big O notations if there is multiple inputs being looped on in some form or manner. If you think about it from a math perspective, if you have a 2d chart where x axis is your input “n” and the y axis is time. What shape shape does your algorithm make on the chart with different “n” inputs.
If your algorithm is constant, logarithmic, linear, or exponential, it always makes the same shape. O(n) and O(50000n) make the same shape. So we classify these as being the same, linear.
–EOF (The Ultimate Computing & Technology Blog) —
推荐阅读:此菜肴脆嫩爽口肉香浓郁且色香味俱全,为冬季百吃不厌的佳肴 枸杞子吃法正确才能更好吸收营养,但人在出现状况时最好别吃它 土豆是一种非常普通的蔬菜,但其营养保健价值令人难以置信 大家别忘了喝碗营养丰富的腊八粥,它对女性朋友的好处尤其多 经常吃一点柚子好处多,柚子皮的作用也不少,以后别再浪费啦 牛奶是常见的营养饮品,如果选择不对,既浪费钱还影响健康 香蕉对身体健康有很多好处,教你用香蕉做一道美味粥吧 香菇与洋葱搭配在一起营养全面,使得保健功效会更好 分数的运算古代的分数除法 巧用份数解决问题
- 评论列表
-
- 添加评论