Open In App

What does Big O – O(N) complexity mean?

Last Updated : 26 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Big O notation, typically represented as O(N) is a concept, in computer science and mathematics that allows us to analyze and describe the efficiency of algorithms. It provides a way to measure how the runtime of an algorithm or function changes as the size of the input (N) increases. In this article, we will delve into the notion of O(N) complexity, its meaning and also provide C++ examples to aid in understanding.

What does Big O Notation mean?

Big O notation is a representation used to indicate the bound of an algorithm’s time complexity relative to its input size. It enables us to make approximations about how an algorithm performance will behave as the input size grows significantly. The “O” in Big O stands for “order ” while the value within parentheses indicates the growth rate of the algorithm.

In the case of O(N), we refer to it as complexity. This implies that the execution time of the algorithm increases proportionally with respect, to the size of the input. If we double our input size we can expect twice as much execution time. Linear complexity is one of the encountered complexities when working with algorithms.

Big O-O(N) Meaning

The concept of O(N) complexity can be understood as the running time of an algorithm being directly tied to the size of the input. In terms as the input size grows so does the number of operations or iterations performed by the algorithm in a fashion. Think of it as a basic “for” loop that goes through each element in the input.

Here’s a simple analogy: Imagine you have a list of numbers and you want to find a number, in the list. In the worst-case scenario, you may need to go through each element of the list one by one. This process would require O(N) time because, for an input of size N, you perform N checks.

Let me provide an example in C++ to illustrate the complexity of O(N). Let’s say we want to find the value in an array of integers.

Step-by-Step Algorithm: Finding Maximum in an Array

  • Begin with an array of integers called “arr.”
  • Set up a variable called “max“. Assign it the value of the element in arr (i.e. max = arr[0]).
  • Go through each element of the array using a loop counter called “i” starting from 1 up to arr. size() – 1.
  • During each iteration compare the element (arr[i]) with the maximum value (max). If arr[i] is greater than max update max, with arr[i]s value.
  • Continue this loop until all elements have been examined.
  • Once the loop finishes the variable ‘max‘ will store the value found in the array.
  • We can then return the value stored in ‘max‘ as our result.

In this example, the findMax function goes through each number in the array to determine which one is the largest. As we add elements to the array we will need to perform several iterations. Hence we can classify this algorithm’s time complexity as O(N) where N represents the size of our input (the number of elements, in the array).

C++




#include <iostream>
#include <vector>
using namespace std;
 
int findMax(const std::vector<int>& arr)
{
 
    // Initialize max to the first element
    int max = arr[0];
 
    for (int i = 1; i < arr.size(); i++) {
        if (arr[i] > max) {
 
            // Update max if a larger element
            // is found
            max = arr[i];
        }
    }
 
    return max;
}
 
// Drivers code
int main()
{
    vector<int> numbers = { 5, 12, 9, 2, 17, 6 };
 
    int result = findMax(numbers);
    cout << "The maximum value is: " << result << endl;
 
    return 0;
}


Java




import java.util.ArrayList;
import java.util.List;
 
public class Main {
    public static int findMax(List<Integer> arr)
    {
        // Initialize max to the first element
        int max = arr.get(0);
 
        for (int i = 1; i < arr.size(); i++) {
            if (arr.get(i) > max) {
                // Update max if a larger element is found
                max = arr.get(i);
            }
        }
 
        return max;
    }
 
    public static void main(String[] args)
    {
        List<Integer> numbers = new ArrayList<>();
        numbers.add(5);
        numbers.add(12);
        numbers.add(9);
        numbers.add(2);
        numbers.add(17);
        numbers.add(6);
 
        int result = findMax(numbers);
        System.out.println("The maximum value is: "
                           + result);
    }
}


Python3




# Function to find the maximum value in a list
def find_max(arr):
    # Initialize max to the first element
    max_val = arr[0]
 
    # Iterate through the list to find the maximum element
    for num in arr[1:]:
        if num > max_val:
            # Update max_val if a larger element is found
            max_val = num
 
    return max_val
 
# Driver code
def main():
    # Example list of numbers
    numbers = [5, 12, 9, 2, 17, 6]
 
    # Call the find_max function to get the result
    result = find_max(numbers)
 
    # Print the maximum value
    print("The maximum value is:", result)
 
if __name__ == "__main__":
    main()


C#




using System;
using System.Collections.Generic;
 
public class MaximumFinder
{
    static int FindMax(List<int> arr)
    {
        // Initialize max to the first element
        int max = arr[0];
 
        for (int i = 1; i < arr.Count; i++)
        {
            if (arr[i] > max)
            {
                // Update max if a larger element is found
                max = arr[i];
            }
        }
 
        return max;
    }
 
    static void Main()
    {
        List<int> numbers = new List<int> { 5, 12, 9, 2, 17, 6 };
 
        int result = FindMax(numbers);
        Console.WriteLine("The maximum value is: " + result);
    }
}


Javascript




function findMax(arr) {
    // Initialize max to the first element
    let max = arr[0];
 
    for (let i = 1; i < arr.length; i++) {
        if (arr[i] > max) {
            // Update max if a larger element is found
            max = arr[i];
        }
    }
 
    return max;
}
 
// Driver code
let numbers = [5, 12, 9, 2, 17, 6];
 
let result = findMax(numbers);
console.log("The maximum value is: " + result);


Output

The maximum value is: 17

In this example the findMax function goes through the array of numbers to locate the value. As the number of elements, in the array increases the function needs to iterate times. Hence we can determine that this algorithm has a time complexity of O(N).

Importance of O(N) Complexity:

Regarding this code understanding the significance of O(N) complexity is crucial:

  1. Linear Time Complexity: The findMax function traverses each element in the vector once performing a fixed amount of work for each element. As the size of the input vector (arr) grows, both comparisons and updates increase proportionally. This linear relationship is represented by O(N) where N denotes the number of elements in the vector.
  2. Predictable Performance: Knowing that this algorithm exhibits O(N) complexity allows us to make predictions, about its performance. If we double the input size it will approximately take long to find the maximum value. This predictability proves valuable when estimating how well an algorithm can handle datasets.
  3. Algorithm Selection: Understanding O(N) complexity assists us in selecting an algorithm for a given task. When dealing with situations where the size of the input is anticipated to be substantial it may prove advantageous to opt for an algorithm with a complexity of O(N) as opposed to algorithms, with complexities.

Conclusion:

In essence, O(N) complexity signifies that the algorithms execution time increases in a fashion as the input size grows. This property proves valuable when devising algorithms and forecasting their performance across different applications.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads