Introduction to Big O Notation in Data Structure


Introduction

One of the most essential mathematical notations in computer science for determining an algorithm's effectiveness is the Big O notation. The length of time, memory, other resources, as well as a change in input size required to run an algorithm can all be used to evaluate how effective it is. Data structure's Big O Notation provides information about an algorithm's performance under various conditions. In other words, it provides the worst-case complexity or upper-bound runtime of an algorithm.

Big O Notation in Data Structure

A change in input size can affect how well an algorithm performs. Asymptotic notations, such as Big O notation, are useful in this situation. When the input goes toward a particular or limiting value, asymptotic notations can be used to represent how long an algorithm will run.

Algebraic terms are used to indicate algorithmic complexity using the Big O Notation within data structures. It determines the time and memory required to run an algorithm for a given input value and represents the upper bound of an algorithm's runtime.

A mathematical notation called Big O is named after the phrase "order of the function," which refers to the growth of functions. It is a member of the Asymptotic Notations family and is also known as Landau's Symbol.

Mathematical Explanation

Consider the functions f(n) & g(n), where f and g have unbounded definitions on the collection of positive real numbers. Every big value of n has a strict positive value for g(n).

If the function f (n) CG(n) for all n >= n0 for a constant c>0 and a natural number n0, the function is said to be O(g) (read as big- oh of g).

The following can be written:

Where n goes to infinity (n ), f(n) = O(g(n)).

The expression above can be expressed succinctly as:

f(n) = O(g(n)).

Analysis of Algorithm

The following describes the general step-by-step process for Big-O runtime analysis

  • Determine the input and what n stands for.
  • Describe the algorithm's highest limit of operations in terms of n.
  • Remove all but the terms with the highest order.
  • Eliminate all the consistent elements.

The following are some of the Big-O notation analysis's beneficial characteristics

  • If f(n) = CG(n), then O(f(n)) = O(g(n)) for a constant c > 0, respectively is for Constant Multiplication.

  • If f(n) = f1(n) + f2(n) + — + FM(n) and fi(n) fi+1(n) i=1, 2, --, m, then the Summation Function is: Hence, O(f(n)) = O(max(f1(n), f2(n), -, fm(n))

  • If f(n) = log an and g(n) = log bn, then the Logarithmic Function is O(f(n)) = O(g(n)) .

  • If f(n) = g(n), then

    f(n) = a0 + a1.n + a2.n2 + — + am.nm if polynomial function, then O(f(n)) = O(nm) (nm).

We must compute and analyze the very worst runtime complexities of an algorithm to evaluate and assess its performance. The quickest runtime for an algorithm is O(1), also known as Constant Running Time, and it takes the same amount of time regardless of the quantity of the input. Despite being the optimal runtime for an algorithm, Constant Running Time is rarely achieved because the duration relies on the size of n inputs.

Examples of typical algorithms with high runtime complexity

  • Linear Search Runtime Complexity: O (n)
  • Binary Search Runtime Complexity - O (log n)
  • Bubble sorting, insertion sorting, selection sorting, and bucket sorting have runtime complexity of O(nc).
  • Exponential algorithms like the Tower of Hanoi have runtime complexity of O(cn).
  • Heap Sort and Merge Sort Runtime Complexity in O (n log n).

Analyzing Space Complexity

Determining an algorithm's space complexity is also crucial. This is because the space complexity of an algorithm shows how much memory it requires. We contrast the algorithm's worst-case space complexities. Functions are categorized using the Big O notation according to how quickly they expand; many functions with the same rate of growth could be written using the same notation.

Since a function's order is also referred to as its development rate, the symbol O is used. A function's development rate is typically only constrained by the upper bound in a large O notation representation of the function.

The following actions must be taken first before Big O notation may analyze the Space complexity

  • Program implementation for a specific algorithm.
  • It is necessary to know the amount of input n to determine how much storage every item will hold.

Some Typical Algorithms' Space Complexity

  • Space Complexity is O for linear search, binary search, bubble sort, selection sort, heap sort, and insertion sort (1).
  • Space complexity for the radix sort is O(n+k).
  • Space complexity for quick sorting is O (n).
  • Space complexity for a merge sort is O (log n).

Let us Explore Some Examples:

void linearTimeComplex(int a[], int s)
{
   for (int i = 0; i < s; i++)
   {
      printf("%d
"
, a[i]); } }

This function executes in O(n) time, sometimes known as "linear time," where n is just the array's size in items. We must print 10 times if the array contains 10 elements. We must print 1000 times if there are 1000 items and the complexity we get is O(n).

void quadraTimeComplex(int a[], int s)
{
   for (int i = 0; i < s; i++)
   {
      for (int j = 0; j < s; j++)
      {
         printf("%d = %d
"
, a[i], a[j]); } } }

We are layering two loops here. When there are n items in our array, the outer loop iterates n times, the inner loop iterates n times for every iteration of an outer loop, and the result is n2 total prints. We must print 100 times if the array contains 10 elements. We must print 1000000 times if there are 1000 items. So, this function takes O(n2) time to complete, and we get complexity as O(n^2).

void constTimeComplex(int a[])
{
    printf("First array element = %d",a[0]);
}

In relation to its input, this function executes in O(1) time, sometimes known as "constant time." There need only be one step for this method, regardless of whether the input array contains 1 item or 1,000 things.

Conclusion

Big O Notation is particularly helpful in understanding algorithms if we work with big data. The tool helps programmers to determine the scalability of an algorithm or count the steps necessary to produce outputs based on the data the programme utilizes. If users are attempting to run our code to increase its efficiency, the Big O Notation in Data Structures can be particularly helpful.

Updated on: 21-Dec-2022

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements