CJCoding With Joseph
15free / daySign inCreate account

Data Structures & Algorithms

Big O Notation

Big O Notation

Big O notation is a way to measure how efficient an algorithm is. It shows how the time or memory an algorithm uses grows as the amount of input data increases. This helps us understand how well an algorithm will perform, especially with large amounts of data.

 

Big O - A

What is Big O Notation?

Big O notation describes the maximum amount of time an algorithm can take to run, giving us the worst-case scenario. It focuses on the most important part of the runtime, ignoring smaller details like constant factors and less significant terms. This simplification makes it easier to compare the efficiency of different algorithms by looking at their main growth rate.

 

Big O - B

Constant Time O(1)

An algorithm runs in constant time if its runtime does not depend on the size of the input. This means that whether you have 1 or 100 elements in the array, the time it takes to run the code remains the same.

int getFirstElement(int arr[]) {
    return arr[0]; // Always takes the same amount of time
}

In this example, the function getFirstElement retrieves the first element of an array. No matter how large the array is, whether it's 1 or 1,000 elements, the time it takes to access the first element is always the same. This makes the runtime O(1), or constant time.

Members only

Keep reading this lesson

You've reached the end of the free preview. Unlock the rest of this lesson plus 42 more lessons in Data Structures & Algorithms.

  • Unlimited coding questions
  • Unlimited access to all classroom material
  • Weekly live call for questions and guidance
  • 3,000 AI tutor tokens / month
  • Personal SQL database (3 MB)
  • Progress tracking

Cancel any time.