Skip to main content

Big O notation

[PLACEHOLDER]

 Big O notation is a mathematical notation used for describing the limiting behaviour of a function.

As a developer (programmer), you could use Big O to understand the performance of an algorithm. By using this concept a developer can evaluate a function’s run-time based on the input(s) passed. 

A developer can also use it to compare algorithms' efficiency within the same domain. This comparison can well be for evaluating the performance. The algorithm that yields the lower Big O value is identified as the optimal one.

Photo by Anni Roenkae from Pexels

What is the "O" in Big O? “The letter O is used because the growth rate of  a function is also referred to as the order of the function” - wikipedia (https://en.wikipedia.org/wiki/Big_O_notation).

In the list below you can see the various functions:


Notation Name
O ( 1 ) Constant
O ( log log n ) Double logarithmic
O ( log n ) Logarithmic
O (( log ⁡ n ) ^c ) Polylogarithmic
O ( n ) Linear
O ( n log ∗ ⁡ n ) n log-star n
O ( n log ⁡ n ) = O ( log ⁡ n ! ) Linearithmic, Loglinear, Quasilinear, or "n log n"
O ( n ^2 ) Quadratic
O ( n ^c ) Polynomial or Algebraic
O ( c ^n ) Exponential
O ( n ! ) Factorial

Let us go through four (4) of the functions from the table above. This is to simplify the explanation of the concept, as well as to showcase why the Big O is useful for developers when creating, refactoring, reviewing algorithms.

O (1)

The constant time is relative to the input is a good place to be when working on your algorithms.  
function ArrayConsoleWrite(x){
  let arrayOfX = [11,12,13,14]
  console.log(arrayOfX[x])
   return arrayOfX[x]
}

O (n)

In the linear function, time of execution will grow as “n” grows (meaning as many steps it needs to take).

function DummyWhileLoop(){
  const varx = [11,12,13,14]  // length is 4
  let x = 2    // valid values are from 0 to 3, which are the positions of the array
  while (x < varx.length)  {
     console.log(varx[x])
     x++
  }
}


In this example the closer x is to the length of the array the faster it will resolve. The worst case will be if every single item of the array requires to get printed.  

O (n^2)

In the case of the quadratic function, time grows exponentially. You can see this type of issue in algorithms that have nesting loops, as an example.

Another type of problem that can be evaluated with the quadratic function is in the Shortest Path algorithm for solving a maze, in particular if the Dijkstra's Algorithm is applied.

Photo by Mitchell Luo on Unsplash

Note: For a more detailed explanation of the Shorter Path algorithm go to the links in our reference section at the end of this article.

O (n!)

The factorial function is the one you want to avoid, as it is the indicator of worst performance. The algorithm will go through the permutation, meaning all the possible combinations/paths.

For occasions where this path cannot be avoided the distributed computing will come to the rescue. Cloud services can become handy to help out in order to scale accordingly, providing the power needed.

An example would be the algorithm for the TSP (Travel Salesman Problem) when solving it by using brute-force search.


TSP problem

In summary

Big O is commonly used in worst case analysis. This means to describe the worst case running time of an algorithm. In contrast, Big Omega is used to describe the best case running time.

 


References

Trending posts

Democratizing AI

Democratizing AI is all about empowering others to use it, by making it available to them. Audiences, such as marketers in a company, will be able to access AI capabilities as part of their MarTech solutions, without the need of being technical. It could also be schools, where the younger generations are learning how to use it in responsible, secure, innovative, and creative ways. This is the year where companies, after discovery phases and teams experimenting, are looking to activate and take advantage of the AI advances. Generated with Microsoft Designer   And so, questions emerge, such as “What to democratize when leveraging AI?” There are common scenarios, as well as specific ones, that will depend on the company, and the industry they belong to. A common scenario, seen in many industries, when democratizing data is the data visualization and reporting . In digital marketing, as an example, data scientists and data analysts can automate reporting, making them available to the c...

SLA-SLO-SLI and DevOps metrics

Companies are in need of the metrics that will allow them to stay in business by making sure they meet the expectations of their customers. The name of the game is higher customer satisfaction by winning their trust and loyalty. To do so, you want to provide good products and services. Therefore you need to find ways to monitor performance, drive continuous improvements and deliver the quality expected by the consumer in this highly competitive market. Photos from AlphaTradeZone via Pexel and Spacejoy via Unsplash SLAs, SLOs and SLIs are a good way to achieve the above. They allow clients and vendors to be on the same page when it comes to expected system performance. If we go one level deeper, vendors/providers work on NFRs (Non-Functional Requirements) when working on their solutions. NFRs define the quality attributes of a system. I bring them up because the relationship between them and the SLAs is that they provide, in a way, foundational aspects for the SLA-SLO-SL...

Take a break on zero emission day 2024

 Do you know how much you contribute to the daily emissions in your city? How much does the city you live in contribute within your country? How much does your country contribute to the emissions on our planet? Do you know its impact? Do you know why we have a zero emission day? Photo by Pixabay via Pexels Let us start by getting our acronyms right, shall we? You may have heard the term GHG emissions, wondering what that means. GHG stands for Green House Gas. These gases are part of the cause of the rising temperature on Earth. What is interesting about them  is that they absorb infrared radiation resulting in the greenhouse effect. Within the greenhouse gases you find carbon dioxide, methane, nitrous oxide, ozone, water vapour. The vast majority of carbon dioxide emissions by humans come from the burning of fossil fuels. Key sectors to consider for GHG Fuel Exploitation Power Industry Transport Waste Agriculture Buildings Industry combustion and processes Top GHG emissions...

Effective framework to resolve conflict in the Workplace

 Conflicts are a part of our daily lives and are often unavoidable at work. Therefore, it's essential to have the tools to effectively manage conflicts and leverage them to our advantage - to spur new ideas, challenge and strengthen our beliefs, and evolve our perspectives when necessary. However, conflicts often trigger our fight-or-flight response and can cause chronic stress and mental fatigue and diminish our productivity. Having the right tools can help us face conflicts confidently.  AI Generated with Microsoft Copilot + Designer by Beolle   Recently, I took a course from Harvard ManageMentor® * to enhance my conflict resolution skills. I summarized the key takeaways from the course in the framework below to help you better prepare for resolving conflicts. The framework consists of six (6) parts Identify the type of conflict   Identify your own and your counterpart's conflict styles   Determine how you want to address the conflict   Prepare to resolve...

This blog uses cookies to improve your browsing experience. Simple analytics might be in place for pageviews purposes. They are harmless and never personally identify you.

Agreed