Asymptotic Analysis (original) (raw)

Last Updated : 07 Apr, 2025

**Given two algorithms for a task, how do we find out which one is better?

One naive way of doing this is - to implement both the algorithms and run the two programs on your computer for different inputs and see which one takes less time. There are many problems with this approach for the analysis of algorithms.

Asymptotic Analysis is the big idea that handles the above issues in analyzing algorithms. In Asymptotic Analysis, we evaluate the performance of an algorithm in terms of input size (we don't measure the actual running time). We calculate, **order of growth of time taken (or space) by an algorithm in terms of input size. For example linear search grows linearly and Binary Search grows logarithmically in terms of input size.

**For example, let us consider the search problem (searching a given item) in a sorted array.

The solution to above search problem includes:

To understand how Asymptotic Analysis solves the problems mentioned above in analyzing algorithms,

Input Size Running time on A Running time on B
10 2 sec ~ 1 h
100 20 sec ~ 1.8 h
10^6 ~ 55.5 h ~ 5.5 h
10^9 ~ 6.3 years ~ 8.3 h

Running times for this example:

Does Asymptotic Analysis always work?

Asymptotic Analysis is not perfect, but that's the best way available for analyzing algorithms. For example, say there are two sorting algorithms that take 1000nLogn and 2nLogn time respectively on a machine. Both of these algorithms are asymptotically the same (order of growth is nLogn). So, With Asymptotic Analysis, we can't judge which one is better as we ignore constants in Asymptotic Analysis. For example, asymptotically Heap Sort is better than Quick Sort, but Quick Sort takes less time in practice.

Also, in Asymptotic analysis, we always talk about input sizes larger than a constant value. It might be possible that those large inputs are never given to your software and an asymptotically slower algorithm always performs better for your particular situation. So, you may end up choosing an algorithm that is Asymptotically slower but faster for your software.