Algorithms — Big O Notation

Algorithms — Big O Notation

Big O Notation is a mathematical function that helps us analyze how complex an algorithm is in time and space. It matters when we build an application for millions of users.

We usually implement different algorithms to solve one problem and measure how efficient one is with respect to the other ones.

Time and Space Complexity

Time complexity is related to how many steps take the algorithm.

Space complexity is related to how efficient the algorithm is using the memory and disk.

Both terms depend on the input size, the number of items in the input. We can analyze the complexity based on three cases:

· Best case or Big Omega?Ω(n): Usually, the algorithm executes independently of the input size in one step.

· Average case or Big Theta?Θ(n): When the input size is random.

· Worst-case or Big O Notation?O(n): Gives us an upper bound on the runtime for any input, and it gives us a kind of guarantee that the algorithm will never take any longer with new input size.

Order of Growth of Common Algorithms


The order of growth is related to how the runtime of an algorithm increases when the input size increases without limit and tells us how efficient the algorithm is. We can compare the relative performance of alternative algorithms. The Figure below shows Big O Notations of common algorithms.

No alt text provided for this image

Big O Notations examples:

O(1) — Constant

It does not matter if the input contains 1000 or 1 million items. The code always executes in one step.

No alt text provided for this image

O(N) — Linear

Our algorithm runs in O(N) time if the number of steps depends on the number of items included in the input.


No alt text provided for this image

O(N2) — Quadratic

When we have two loops nested in our code, we say it runs in quadratic time O(N2). For example, when a 2D matrix is initialized in a tic-tac-toe game.

No alt text provided for this image


O(N3) — Cubic

We say that our algorithm runs in Cubic time when the code includes at the most three nested loops. For example, given N integers, how many triples sum to precisely zero? One approach (not the best) is to use three nested loops.

No alt text provided for this image


O(LogN) — Logarithmic

This kind of algorithm produces a growth curve that peaks at the beginning and slowly flattens out as the size of the input increase.

Log28 = 3

Log216 = 4

Log232 = 5

The?binary search?uses at most LogN key compares to search in a sorted array of size N. With eight elements, it takes three comparisons, with 16 elements takes four comparisons, with 32 elements takes five comparisons, and so on.

How to Find the Time Complexity of an Algorithm

To find the Big O complexity of an algorithm follows the following rules:


· Ignore the lower order terms

· Drop the leading constants

Example: If the time complexity of an algorithm is 2n3 + 4n + 3.

Its Big O complexity simplifies to O(n3).

Example: Given the following algorithm:

No alt text provided for this image

First, we split the code into individual operations and then compute how many times it is being executed, as is shown in the following table.

No alt text provided for this image

Now, we need to sum how many times each operation is executing.

Time complexity = 1 + 1 + N + N + N + N + 1 => 4N + 3

Big O complexity = O(N)

Why Big O Notation ignores Constants?

Big O Notation describes how many steps are required relative to the number of data elements. And it serves as a way to classify the long-term growth rate of algorithms.


For instance, O(N) will be faster than O(N2) for all amounts of data, as shown in the figure below.


No alt text provided for this image

O(N) is faster than O(N2) for all amounts of data

Now, if we compare O(100N) with O(N2), we can see that O(N2) is faster than O(100N) for some amounts of data, as shown in the figure below.

No alt text provided for this image


O(N2) is faster than O(100N) for some amounts of data

But after a point, O(100N) becomes faster and remains faster for all increasing amounts of data from that point onward. And that is the reason why Big O Notation ignores constants. Because of this, O(100N) is written as O(N).














Orlando Garcia

Senior UI Software Engineer @ CBORD | Angular, React, Ionic, Stencil

10 个月

Excellent Post, me sirve machismo para mi investigación, dar créditos, saludos

回复
Hadeel Alfoqahaa

Senior Software Engineer at Beyond limits | Full stack engineer | Payment integration | Microservices | Ecommerce

3 年

Your posts are Recapping & Updating my informations. You deserve a big thanks ??

回复
Ali Gojayev

Principal Software Engineer at Azercell

3 年

Big O Notation is all about mathematics ??

回复
Baraa Abualrub

Software Development Engineer II @ Expedia Group

3 年

Thanks for sharing Omar!? One note I might add here is that there is no relation between (best, worst and average cases) and (O, omega and theta). Cases describe Big O time complexity (Big O since industry use it mainly, it can describe Ω or Θ) for a particular input or scenario. O, Ω and Θ describe the upper, lower and tight bounds for the runtime.

要查看或添加评论,请登录

Omar Ismail的更多文章

  • OAuth Grant Types (Authorization Code Grant)

    OAuth Grant Types (Authorization Code Grant)

    The authorization code grant type is used to obtain both access tokens and refresh tokens. The grant type uses the…

  • What is Apache Kafka?

    What is Apache Kafka?

    Reference : https://www.qlik.

    2 条评论
  • Multi-Tenant Architecture in a Nutshell

    Multi-Tenant Architecture in a Nutshell

    Thanks to the original writer and article :…

  • Microservices Communication!

    Microservices Communication!

    Thanks To: https://medium.com/design-microservices-architecture-with-patterns/microservices-communications-f319f8d76b71…

    2 条评论
  • What Are the New Features of SpringBoot3 ?

    What Are the New Features of SpringBoot3 ?

    Thanks to : https://medium.com/javarevisited/what-are-the-new-features-of-springboot3-6ddba9af664 1.

    1 条评论
  • OAuth 2.0!

    OAuth 2.0!

    Thanks to the original writer : https://medium.com/@isharaaruna OAuth2.

    2 条评论
  • How to Draw a Technical Architecture Diagram

    How to Draw a Technical Architecture Diagram

    Thanks to the original writer and article : https://levelup.gitconnected.

    2 条评论
  • Event Sourcing Versus Event-Driven Architecture

    Event Sourcing Versus Event-Driven Architecture

    Thanks to the original writer and article :…

  • Best Practices For Your API Versioning Strategy

    Best Practices For Your API Versioning Strategy

    API versioning is critical. But do you know all of the API versioning best practices? Is your API versioning strategy…

    1 条评论
  • Enterprise Architecture Tools

    Enterprise Architecture Tools

    Thanks to the original writer and article : https://medium.com/geekculture/enterprise-architecture-tools-b8165c8c9d7…

社区洞察

其他会员也浏览了