How the efficiency between same purpose algorithms are judged

When you first started programming, the primary concern was figuring out an algorithm (or function, when put into practice) that would accomplish the task at hand. As your skills progressed, you started working on larger projects and studying concepts that would prepare you for a career in software engineering. One of the first concepts you would inevitably come across is asymptotic notation, or what is colloquially known as Big O Notation.

Image for post

In short, Big O Notation describes how long it takes a function to execute (runtime) as the size of an input becomes arbitrarily large. Big O can be represented mathematically as O(n), where “O” is growth rate (or order) of the function, and “n” is the size of the input. Translated into English, the runtime grows on the order of the input, or in the case of say O(n²), the order of the square of the size of the input.

This is a very important concept that will come up not only in your technical interviews, but also during your career when implementing solutions to handle large datasets. In this post I’ll give a brief overview on Big O analysis, simplifications, and calculations.

Big O Analysis

When using Big O to analyze an algorithm (asymptotic analysis), it should be noted that it primarily concerns itself with the worst and average case scenarios. For example, the runtime of an algorithm which sequentially searches a data set for a value that happens to be last, would be the worst case scenario.

Figuring out the worst case scenario is safe and thus never underestimated, though sometimes it may be overly pessimistic. Ultimately whether you analyze for worst or average case will depend on the use of your algorithm. For the typical problem the average case of an algorithm may be suitable, for cryptographic problems it’s usually best to seek out the worst case.

Heuristics for Calculating Big O

When calculating Big O, there are a couple of shortcuts that can help you

expedite the process:

  • Arithmetic, assignment, accessing an element in a array/object (by index/key) are all constant time, O(1)
  • In a loop, the runtime is the loop itself multiplied by whatever is in the loop

Keep these in mind for when you calculate the Big O for an algorithm I’ll provide towards the end.

#programming #big-o-notation #algorithms #algorithms

An Overview of Big-O Notation
1.50 GEEK