DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • Crafting Mazes
  • Understanding IEEE 802.11(Wi-Fi) Encryption and Authentication: Write Your Own Custom Packet Sniffer
  • How to Implement Linked Lists in Go
  • Recursive Feature Elimination in Practice

Trending

  • How to Convert XLS to XLSX in Java
  • Monolith: The Good, The Bad and The Ugly
  • Agentic AI for Automated Application Security and Vulnerability Management
  • Endpoint Security Controls: Designing a Secure Endpoint Architecture, Part 1
  1. DZone
  2. Data Engineering
  3. Data
  4. Demystifying Big O Notation

Demystifying Big O Notation

Big O Notation is probably going to be the first thing you hear about when learning about data structures and algorithms. Let's make it easy to understand.

By 
Jesse Price user avatar
Jesse Price
·
Jan. 03, 25 · Analysis
Likes (5)
Comment
Save
Tweet
Share
3.5K Views

Join the DZone community and get the full member experience.

Join For Free

Understanding how efficient your code is just by looking at it can be tricky. Thankfully, the brilliant minds before us have come up with a neat trick: Big O notation. This fancy little concept helps us measure how much time and space an algorithm will consume based on its input. 

So, why should we care? Well, as engineers, our job boils down to two things: solving problems that have never been solved before or solving problems that have been solved but in a more efficient way. Knowing Big O helps us make smarter decisions about which algorithms to use. It’s like having a cheat sheet for predicting how much time and memory your code will need, depending on the input size. Sounds good, right? Let’s break it down with a simple example: O(n), also known as linear time complexity.

O(n): A Linear Approach

Take a look at this function:

JavaScript
 
const arr = [1, 3, 5, 5, 4, 6, 12, ...];

const addAllArrayElements = (arr) =>{

  let sum = 0;

  for(let i=0; i < arr.length; i++){

    sum += arr[i];

  }

  return sum;

}


Here, we have a simple function that takes an array of numbers and adds them all together. Now, let’s talk Big O. The for loop in this example runs once for each element in the array, which means the time taken grows directly with the size of the array. If there are n elements in the array, the function runs n times. Hence, we call this O(n) — linear time complexity.

Sure, you might point out that adding a value to the sum variable takes some time, too. And you’re right! But in Big O terms, we ignore those small details (like constants) because they don’t significantly change how the function behaves as the input size grows.


As you can see, the execution time increases linearly with the input size. This is great when working with small arrays, but if the input grows, you might want to rethink your approach. 

The Power of Loops

Let’s move on to a slightly more complicated example. The key to recognizing Big O complexity lies in loops — they’re the most reliable indicator of how an algorithm scales with input size. Here's a new example:

JavaScript
 
const arr = [1, 3, 5, 5, 4, 6, 12, ...];

const addAllArrayElementsTwice = (arr) =>{

  let sum = 0;

  for(let i=0; i < arr.length; i++){

    sum += arr[i];

  }

  for(let i=0; i < arr.length; i++){

    sum += arr[i];

  }

  return sum;

}


In this case, we’re looping through the array twice. One loop adds all the elements, and the second one adds them all over again. So, what’s the time complexity here? O(2n). But before you get excited thinking you’ve figured it out — hold up! We don’t care about constants in Big O. That means O(2n) is effectively just O(n).

Here’s a good tip: when analyzing an algorithm, think of the process as a whole. Even though we have two loops, it’s still linear, so the time complexity stays O(n).

Nested Loops: O(n²)

Now, let’s crank things up a bit. What happens if we add another nested loop inside the first one?

JavaScript
 
const arr = [1, 3, 5, 5, 4, 6, 12, ...];

const addAllArrayElementsTwice = (arr) =>{

  let sum = 0;

  for(let i=0; i < arr.length; i++){

    sum += arr[i];

    for(let j=0; j < arr.length; j++){

      sum += arr[j];

    }

  }

  return sum;

}


Here, the function does something interesting: for each element in the array, it loops through the array again and adds every element to the sum. This gives us O(n²) time complexity, also known as quadratic time complexity. The outer loop runs n times, and for each outer loop iteration, the inner loop runs n times. So, the total work done grows quadratically with the size of the input.

Want to make it worse? Add another nested loop, and you’re looking at O(n³). The more nested loops, the higher the exponent.


Other Common Time Complexities

There are plenty of other important complexities you’ll encounter as you dive deeper into algorithms. Some of the most common include:

  • O(log n): This is usually seen in algorithms that divide the input in half at each step, like binary search.
  • O(n log n): You’ll often see this complexity in efficient sorting algorithms like Merge Sort or Quick Sort, where the input is divided into smaller chunks (log n) and then processed linearly (n).

Here's a quick reference to visualize the different complexities:


Wrapping It Up

Big O is a powerful tool for understanding how an algorithm behaves as the input grows. It allows us to make smarter decisions about which algorithms to use based on how they perform with large datasets. Whether it’s O(n), O(n²), or something more complex, knowing the time complexity can help us choose the right approach for solving problems.

Keep an eye on loops and nesting, and soon you’ll start seeing how Big O helps you predict algorithm performance with just a quick glance and make sure to explore more on your own when it comes to Big O Notation.

Data structure Computer science

Opinions expressed by DZone contributors are their own.

Related

  • Crafting Mazes
  • Understanding IEEE 802.11(Wi-Fi) Encryption and Authentication: Write Your Own Custom Packet Sniffer
  • How to Implement Linked Lists in Go
  • Recursive Feature Elimination in Practice

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!