{"id":81060,"date":"2021-06-17T09:00:51","date_gmt":"2021-06-17T03:30:51","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=81060"},"modified":"2021-06-17T09:00:51","modified_gmt":"2021-06-17T03:30:51","slug":"data-structures-asymptotic-analysis","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/data-structures-asymptotic-analysis\/","title":{"rendered":"Data Structures Asymptotic Analysis"},"content":{"rendered":"<p>In this article, we are going to learn about the concept of asymptotic analysis and various asymptotic notations used for the analysis of any algorithm.<\/p>\n<h3>Defining asymptotic analysis<\/h3>\n<p>Whenever we write an algorithm for a particular computational problem, we design the algorithm in such a way that it gives the best results in terms of running time as well as memory used.<\/p>\n<p>While checking the running time for any algorithm, we want three things:<\/p>\n<p>1. The algorithm should be machine-independent.<\/p>\n<p>2. It should work on all possible inputs.<\/p>\n<p>3. It should be general and not programming language-specific.<\/p>\n<p>However, we never do accurate analysis, rather we do approximate analysis. We are interested in the increase in the running time with the increase in the input size. Thus, we are only interested in the \u2018growth of the function\u2019, which we can measure\/analyse through asymptotic analysis.<\/p>\n<p>1. Asymptotic analysis is a general methodology to compare or to find the efficiency of any algorithm.<\/p>\n<p>2. We measure the efficiency in terms of the growth of the function. The growth of any function depends on how much the running time is increasing with the increase in the size of the input.<\/p>\n<h3>Assumptions for Asymptotic Analysis<\/h3>\n<p>Asymptotic analysis is based upon certain assumptions such as:<\/p>\n<p>Let Rt be the running time of an algorithm, N be the input size. Then,<\/p>\n<p>1. Rt \u2265 0 and N\u22650 i.e. both running time and input size should be positive numbers.<\/p>\n<p>2. The input size is very large while analyzing any algorithm through asymptotic analysis i.e. N\u2192\u221e<\/p>\n<p>3. The analysis is both hardware independent and software independent.<\/p>\n<p>4. For better understanding, we do a high-level representation of the algorithm i.e. pseudo-code or English like instructions.<\/p>\n<p>5. We focus on primitive operations i.e. those operations which are independent of software such as assignment, comparison, arithmetic operations, etc in terms of input size.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<p>Let us try to get a brief idea behind the concept of asymptotic analysis with the help of an example.<\/p>\n<p>1. Suppose we wish to calculate the value of 1 + 2 + 3+ 4+ &#8212;&#8212; + n<\/p>\n<p>2. There could be two possible ways to do that.<\/p>\n<p>3. One of the ways is to write the formula directly as shown:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">int n, sum=0;\nsum = (n * (n+1))\/2;<\/pre>\n<p>4. Another way is iteratively finding the sum as shown:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">int n, sum=0;\n for(i=1; i&lt;=n; i++){\n     sum = sum + i;\n }\n<\/pre>\n<p>5. In the first method, where we are using the direct formula, every time the number of steps is the same because, for whatever value of n, the formula will run in a single step.<\/p>\n<p>6. Thus, we can say that method 1, where we are using the formula to find the sum, is taking constant time because it is completed in a constant number of steps.<\/p>\n<p>7. However, in the 2nd method, the number of steps executed by the program will depend on the value of n because for loop is running n times.<\/p>\n<p>8. This is why the number of steps in the 2nd method is not constant but linearly dependent on n.<\/p>\n<p>9. From here, we can conclude that the computation time taken by the second method is more than the first method.<br \/>\nThis is the basis of our analysis.<\/p>\n<h3>How to find the time complexity?<\/h3>\n<p>1. It is not practically feasible to sit and calculate the time taken by an algorithm to run on a system.<\/p>\n<p>2. However, we know that the running time of an algorithm depends on the size of the input. So, we can use this input size to analyse the time taken by an algorithm.<\/p>\n<p>3. Let us again take the above example of finding the sum of first n natural numbers iteratively.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">int n, sum=0;\n    for(i=1; i&lt;=n; i++){\n        sum = sum + i;\n    }\n<\/pre>\n<p>4. In this code, if the value of n is 10, the algorithm will take 10 units of time.<\/p>\n<p>5. But for the same code, if the value of n is 1000, the algorithm will take 1000 units of time for execution.<\/p>\n<p>6. Therefore, if the input is n, then the time complexity for any algorithm is a function of n, say T(n).<\/p>\n<h3>How to calculate the value of T(n)<\/h3>\n<p>1. Let T(n) be a function that denotes the time complexity for an algorithm.<\/p>\n<p>2. Then, to find the value of T(n), we need to check for various values of n.<\/p>\n<p>3. Usually, finding the value of T(n) for smaller problems is easy as compared to the larger ones.<\/p>\n<p>4. For example, suppose a function is T(n) = 3n<sup>2<\/sup> +5n + 8<\/p>\n<p>5. Then for smaller values of n, such as for n=1,2,3,4\u2026.9, the value of T(n) depends on the whole equation.<\/p>\n<p>6. However, whenever we do asymptotic analysis, we always consider n to be very large (as described in the assumptions).<\/p>\n<p>7. For larger values of n i.e. n\u2192\u221e, the value T(n) depends only on the highest power and hardly depends upon other values in the equation.<\/p>\n<p>8. In the above example, for higher values of n, T(n) will come out to be T(n) = 3n2.<\/p>\n<p>9. Usually, we discard the value of constant, which is 3 here.<\/p>\n<p>10. So we can conclude that the value of T(n)=3n2 +5n + 8 is of the order of n2<\/p>\n<p>Usually, there are three different categories depending on the time taken by an algorithm:<\/p>\n<h4>1. Worst case<\/h4>\n<p>It describes the scenario in which the algorithm takes the highest amount of time.<\/p>\n<h4>2. Best case<\/h4>\n<p>It describes the case in which the running time of the algorithm is minimum.<\/p>\n<h4>3. Average case<\/h4>\n<p>It lies between the best case and worst case. Precisely, it takes the average time that an algorithm will take to execute.<\/p>\n<h3>Asymptotic Notations<\/h3>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2021\/06\/commonly-used-Asymptotic-Notations.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-81107\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2021\/06\/commonly-used-Asymptotic-Notations.jpg\" alt=\"commonly used Asymptotic Notations\" width=\"1050\" height=\"400\" \/><\/a><\/p>\n<p>The three most commonly used notations for finding the running time or the time complexity of an algorithm are:<\/p>\n<p>1. Big Oh notation(O)<\/p>\n<p>2. Omega notation(\u03a9)<\/p>\n<p>3. Theta notation(\u03b8)<\/p>\n<h4>1. Big Oh notation(O)<\/h4>\n<p>a. This notation describes the upper bound of the running time for an algorithm.<\/p>\n<p>b. Big Oh notation ensures that the function never grows faster than its upper bound.<\/p>\n<p>c. Thus, it measures the performance of an algorithm by describing the order of growth for a function and by giving the least upper bound for that function.<\/p>\n<p>d. Big Oh measures the worst-case time complexity for the given algorithm i.e., it measures the longest time an algorithm can execute.<\/p>\n<h5>Computing O(n)<\/h5>\n<p>a. Let there be two functions- f(n) and g(n) defined for positive integers as shown:<\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2021\/06\/TechVidvan-DS-Asymptotic-analysis-graph001.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-81108\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2021\/06\/TechVidvan-DS-Asymptotic-analysis-graph001.jpg\" alt=\"Big Oh Asymptotic Notation\" width=\"800\" height=\"628\" \/><\/a><\/p>\n<p>b. Then f(n) = O(g(n)) as growth of g(n) is faster than that of f(n).<\/p>\n<p>c. f(n) = O(g(n)) is read as f(n) is equal to Big Oh of g(n).<\/p>\n<p>d. Mathematically, we can say that f(n) \u2264 c.g(n) for constants c and n0 where c&gt;0 and n&gt;n0.<\/p>\n<p>e. This implies that the function g(n) is an upper bound on the function f(n) i.e. f(n) cannot grow faster than g(n).<\/p>\n<p>Let us try to understand this concept with the help of an example:<\/p>\n<p><strong>For Example:<\/strong><\/p>\n<p>1. Let f(n) = 3n+2 and g(n) = n. We need to check whether f(n) = O(g(n)) or not.<\/p>\n<p>2. For f(n) to be equal to O(g(n)), both the functions f(n) and g(n) must satisfy the following condition: f(n) \u2264 c.g(n).<\/p>\n<p>3. To check if the condition holds, replace f(n) by 3n+2 and g(n) by n i.e. 3n+2 &lt;= c.n<\/p>\n<p>4. Let c=7 and n=1, then 3(1)+2 &lt;= 7*1 \u21d2 5&lt;=7 which is true.<\/p>\n<p>Thus, f(n) &lt;= c.g(n) is true for n=1.<\/p>\n<p>5. For n=2,<br \/>\n3(2)+2 &lt;= 7*2 \u21d2 8 &lt;= 14 which is again true.<\/p>\n<p>6. Therefore, f(n) &lt;=c.g(n) is true for n=2 as well.<\/p>\n<p>7. From here, we can conclude that f(n) &lt;= c.g(n) holds for all values of n, given that c=7.<\/p>\n<p>8. Thus, we can say that the above equation always holds for some constant c and some constant n0.<\/p>\n<p>9. So, f(n) is equal to Big Oh of g(n) i.e. f(n) = O(g(n)) or we can say that c.g(n) is an upper bound on function f(n).<\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2021\/06\/TechVidvan-DS-Asymptotic-analysis-graph002.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-81109\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2021\/06\/TechVidvan-DS-Asymptotic-analysis-graph002.jpg\" alt=\" DS Asymptotic analysis\" width=\"800\" height=\"628\" \/><\/a><\/p>\n<p>Big Oh notation always gives the worst-case time complexity for a function or algorithm as it is based on comparison with the upper bound of a function.<\/p>\n<h4>2. Omega notation(\u03a9)<\/h4>\n<p>1. It is the opposite of Big-Oh notation.<\/p>\n<p>2. Omega notation always describes the best-case scenario for a particular algorithm.<\/p>\n<p>3. Omega represents the lower bound of running time for an algorithm.<\/p>\n<p>4. It will measure the least amount of time that an algorithm can take for its execution in the best case.<\/p>\n<h5>Computing omega:<\/h5>\n<p>1. Let f(n) and g(n) be two functions defined on a set of natural numbers.<\/p>\n<p>2. Then, f(n) = \u03a9(g(n)) only if the growth of f(n) is faster than the growth of g(n).<\/p>\n<p>3. Mathematically, its equation will be f(n) \u2265 c.g(n) where c and n0 are constants such that c&gt;0 and n&gt;n0.<\/p>\n<p>Let us understand this notation with the help of an example:<\/p>\n<p>1. Let f(n) = 2n+3, g(n) = n, we need to find whether f(n)= \u03a9 (g(n)) or not.<\/p>\n<p>2. For f(n) to be equal to the omega of g(n), it must satisfy the condition: f(n)\u2265 c.g(n)<\/p>\n<p>3. Replace f(n) by 2n+3 and g(n) by n 2n+3 \u2265 c*n<\/p>\n<p>4. Let c=1 i.e. value of constant c=1.<\/p>\n<p>5. For n=1,<br \/>\n2(1) + 3 \u2265 1(1) .\u21d2 5 \u2265 1 which is true.<\/p>\n<p>6. Similarly, for n=2,<br \/>\n2(2)+3 \u2265 1(2) \u21d2 7 \u2265 2 which is again true.<\/p>\n<p>7. If we check for further values, the equation holds for every value of n given that c=1.<\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2021\/06\/TechVidvan-DS-Asymptotic-analysis-graph03.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-81110\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2021\/06\/TechVidvan-DS-Asymptotic-analysis-graph03.jpg\" alt=\"Omega Notation\" width=\"800\" height=\"628\" \/><\/a><\/p>\n<p>Here, the function g(n) is the lower bound of the function f(n) and thus it gives the running time for the function f(n) in the best possible case.<\/p>\n<h4>3. Theta notation(\u0398)<\/h4>\n<p>1. While Big Oh gives the worst-case analysis and omega gives the best case analysis, theta lies somewhere in between.<\/p>\n<p>2. Theta notation gives the average case analysis for a function or algorithm.<\/p>\n<p>3. In real-world problems, not every time the algorithm will have either the best or worst-case scenario.<\/p>\n<p>4. Practically, an algorithm is always performing in some average case i.e. the algorithm keeps on fluctuating.<\/p>\n<h5>Computing Theta<\/h5>\n<p>1. Let there be two functions: f(n) and g(n) defined on a set of positive integers.<\/p>\n<p>2. Then, f(n) = \u0398(g(n)) if<br \/>\nc1.g(n)\u2264 f(n)\u2264 c2.g(n) where c1, c2 are constants.<\/p>\n<p>3. In theta notation, two limits bind the function i.e. both the upper limit as well as the lower limit. This is what makes the theta notation return the average case for an algorithm.<\/p>\n<p>4. Thus, the condition f(n)= \u03b8g(n) becomes true only when c1.g(n) is less than or equal to f(n) and c2.g(n) is greater than or equal to f(n).<\/p>\n<p>Let us understand the Theta notation with the help of an example:<\/p>\n<p>1. Let f(n) = 2n+3 and g(n) = n.<\/p>\n<p>2. Replace f(n) with 2n+3 and g(n) with n in the equation c1.g(n)\u2264 f(n)\u2264 c2.g(n).<\/p>\n<p>Then the equation will be c1.n \u22642n+3\u2264 c2.n<\/p>\n<p>3. To ensure that the equation holds, let c1=1 and c2=5.<\/p>\n<p>4. Thus, for n=1,<br \/>\nthe equation will be 1(1) \u2264 2(1)+3 \u2264 5(1) \u21d2 1 \u2264 5 \u2264 5 which is true.<\/p>\n<p>5. For n=2,<br \/>\nThe equation will be 1(2) \u2264 2(2)+3 \u2264 5(2) \u21d2 2 \u2264 7 \u2264 10 which is again true.<\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2021\/06\/TechVidvan-DS-Asymptotic-analysis-graph004.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-81112\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2021\/06\/TechVidvan-DS-Asymptotic-analysis-graph004.jpg\" alt=\"Asymptotic analysis\" width=\"800\" height=\"550\" \/><\/a><\/p>\n<p>6. Hence, we can say that the equation holds for all the values of n given that c1=1 and c2=5.<\/p>\n<p>7. Therefore, we can say that for the above example, f(n) is equal to \u03b8(g(n)). And it gives average-case complexity for f(n).<\/p>\n<p><strong>Example:<\/strong><\/p>\n<p>Let us consider the following example for more understanding:<\/p>\n<p>Assume we have to reverse the string: \u201cTechVidvan\u201d. The traditional algorithm for it is:<\/p>\n<p>1: START<\/p>\n<p>2: Declare two variables str1 and str2.<\/p>\n<p>3: Initialize the variable str1 with \u201cTechVidvan\u201d<\/p>\n<p>4: Traverse to the last character of str1<\/p>\n<p>5: Beginning from the end, take each character from str1 and append it to str2.<\/p>\n<p>6: Display str2.<\/p>\n<p>7: END<\/p>\n<p>In this algorithm, every time we execute step 5, a new string gets created instead of appending to the original string. This is why the complexity of the above algorithm is O(n2).<\/p>\n<p>However, in JAVA, we don\u2019t need to reallocate the string to str2. In JAVA, the function StringBuffer.reverse() reverses the string without reallocation. Therefore, reversing a string in JAVA takes O(1) time.<\/p>\n<h3>Need for asymptotic analysis<\/h3>\n<p>In simple words, we need asymptotic analysis to find the best possible algorithm for our problem.<\/p>\n<p>Let us understand this with the help of an example:<\/p>\n<p>Suppose we wish to search an element inside an array. There could be multiple ways to do this. Two of the ways are- Linear search and Binary search.<\/p>\n<ul>\n<li>Execution time for linear search: O(n)<\/li>\n<li>Execution time for binary search: O(log n)<\/li>\n<\/ul>\n<p>Here, n = input size.<\/p>\n<p>Since n &gt; (log n), therefore, according to asymptotic analysis, the linear search must take more time to execute rather than binary search.<\/p>\n<p>But if we run linear search on a faster machine\/system, it might take lesser time in comparison to binary search as shown:<\/p>\n<table>\n<tbody>\n<tr>\n<td rowspan=\"6\"><b>Linear search on Machine-A<\/b><\/p>\n<p><span style=\"font-weight: 400\">(109 instructions per second)<\/span><\/p>\n<p><span style=\"font-weight: 400\">For n=10,000 ( ten thousand)<\/span><\/p>\n<p><span style=\"font-weight: 400\">Execution time = n\/10<\/span><span style=\"font-weight: 400\">9<\/span><\/p>\n<p><span style=\"font-weight: 400\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0= 10,000\/10<\/span><span style=\"font-weight: 400\">9<\/span><\/p>\n<p><span style=\"font-weight: 400\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0= 10<\/span><span style=\"font-weight: 400\">-5<\/span><span style=\"font-weight: 400\"> \u00a0 (i.e. <\/span><b>Faster<\/b><span style=\"font-weight: 400\">)<\/span><\/p>\n<p><span style=\"font-weight: 400\">For n = 10000000 (ten million)<\/span><\/p>\n<p><span style=\"font-weight: 400\">Execution time = n\/10<\/span><span style=\"font-weight: 400\">9<\/span><\/p>\n<p><span style=\"font-weight: 400\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0= 10000000\/10<\/span><span style=\"font-weight: 400\">9<\/span><\/p>\n<p><span style=\"font-weight: 400\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0= 10<\/span><span style=\"font-weight: 400\">-2<\/span><span style=\"font-weight: 400\">\u00a0 (i.e. <\/span><b>Slower<\/b><span style=\"font-weight: 400\">)<\/span><\/td>\n<td rowspan=\"6\"><b>Binary search on Machine-B<\/b><\/p>\n<p><span style=\"font-weight: 400\">(106 instructions per second)<\/span><\/p>\n<p><span style=\"font-weight: 400\">For n= 10,000 ( ten thousand),<\/span><\/p>\n<p><span style=\"font-weight: 400\">Execution time = log n\/10<\/span><span style=\"font-weight: 400\">6<\/span><\/p>\n<p><span style=\"font-weight: 400\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0= log 10,000\/10<\/span><span style=\"font-weight: 400\">6<\/span><\/p>\n<p><span style=\"font-weight: 400\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0= 4 * 10<\/span><span style=\"font-weight: 400\">-6<\/span><span style=\"font-weight: 400\">\u00a0 (i.e. <\/span><b>Slower<\/b><span style=\"font-weight: 400\">)<\/span><\/p>\n<p><span style=\"font-weight: 400\">For n = 10000000 (ten million)<\/span><\/p>\n<p><span style=\"font-weight: 400\">Execution time = log n\/10<\/span><span style=\"font-weight: 400\">9<\/span><\/p>\n<p><span style=\"font-weight: 400\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0= log 10000000\/10<\/span><span style=\"font-weight: 400\">9<\/span><\/p>\n<p><span style=\"font-weight: 400\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0= 7 * 10<\/span><span style=\"font-weight: 400\">-9<\/span><span style=\"font-weight: 400\">\u00a0 (i.e. <\/span><b>Faster<\/b><span style=\"font-weight: 400\">)<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>The above observation shows that linear search gives faster results than binary search for lower inputs. However, for larger input sizes, binary search is faster.<\/p>\n<p>This is what asymptotic analysis does for us i.e. it helps to calculate time complexity as close to the actual value as possible.<\/p>\n<h3>Need for three different notations<\/h3>\n<p>1. Every function has some upper and lower limit of values.<\/p>\n<p>2. The three different asymptotic analyses give results in different scenarios.<\/p>\n<p>3. We use Big-Oh analysis for worst-case, omega notation for best case analysis and theta for the average-case.<\/p>\n<p>4. Suppose, we wish to search an element inside an array.<\/p>\n<p>5. If we find it in the first position, we have encountered the best case. Thus, the omega will come into use, and this code\u2019s complexity will be \u03a9(1).<\/p>\n<p>6. If we find the element at the last position, we have encountered the worst case, because we will have to traverse the whole array to reach there. In such a case, the complexity of code will be O(n).<\/p>\n<p>7. If the element lies somewhere around the middle of the array, we will measure its complexity in terms of \u03b8(n).<\/p>\n<p>8. This is why we need three different analyses suitable for different cases.<\/p>\n<p>9. Also, having three analyses ensures that the algorithm does not go beyond the upper and lower limits and works within these limits.<\/p>\n<h3>Some more Asymptotic Notations<\/h3>\n<p>Apart from the Big-Oh, Omega and Theta notations, there are two more less-common notations as follows:<\/p>\n<h4>1. Little \u2018o\u2019 notation (o)<\/h4>\n<p>a. The Big-Oh notation gives us the tightest upper bound for a function, whereas this little-o gives the loose upper bound for the function f(n).<\/p>\n<p>b. Big-Oh gives the exact order of growth, whereas little-o gives the estimation and not the actual order of growth.<\/p>\n<p>For example, Let there be two functions f(n) and g(n).<\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2021\/06\/TechVidvan-DS-Asymptotic-analysis-graph005.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-81111\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2021\/06\/TechVidvan-DS-Asymptotic-analysis-graph005.jpg\" alt=\"Theta Notation\" width=\"800\" height=\"628\" \/><\/a><\/p>\n<p>Then, f(n) will be o(g(n)) i.e. f(n) = o(g(n)) for a given constant \u2018k\u2019 such that k&gt;1<br \/>\nIf 0 \u2264 f(n) \u2264 c.g(n)<\/p>\n<p>where, c = a constant and c&gt;0<\/p>\n<p>From here, we can say that after the point n=k in the graph, the function f(n) never grows faster than g(n). Thus, we are calculating the approximate growth of a function rather than the exact growth.<\/p>\n<h4>2. Little \u03c9 notation<\/h4>\n<p>a. Just like the omega notation(\u03a9), the little \u03c9 notation provides the lower bound for a function.<\/p>\n<p>b. However, the \u03a9 notation provides the tightest lower bound and the little-omega provides a loose lower bound for the function.<\/p>\n<p>c. Just like little-o notation, \u03c9 gives the estimation of growth and not the actual growth for a function f(n).<br \/>\nFor example, consider the graphs f(n) and g(n) as shown below:<\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2021\/06\/TechVidvan-DS-Asymptotic-analysis-graph006.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-81113\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2021\/06\/TechVidvan-DS-Asymptotic-analysis-graph006.jpg\" alt=\"Asymptotic Notation\" width=\"800\" height=\"628\" \/><\/a><\/p>\n<p>Then, f(n) = \u03c9(g(n)) if for a given constant \u2018c\u2019 such that c&gt;0 and \u2018k\u2019 such that k&gt;=1<br \/>\nf(n) &gt; c.g(n) &gt;=0.<\/p>\n<h3>Advantages of Asymptotic notations<\/h3>\n<p>1. Asymptotic notations help us represent the upper and lower bound of execution time in the form of mathematical equations.<\/p>\n<p>2. It is not feasible to manually measure the performance of the algorithm as it changes with the input size. In such a case, asymptotic notations help us give a general insight into execution time for an algorithm.<\/p>\n<p>3. Asymptotic analysis is so far the most efficient and the best way to analyse algorithms against the actual running time.<\/p>\n<p>4. Asymptotic analysis helps us in performing our task with fewer efforts.<\/p>\n<h3>Common asymptotic notations<\/h3>\n<p>1. These are some most commonly used asymptotic analysis.<\/p>\n<p>2. The table below shows them in the increasing order of their running time i.e. O(1) takes less execution time than O(log n) and so on.<\/p>\n<table style=\"height: 467px\" width=\"420\">\n<tbody>\n<tr>\n<td><span style=\"font-weight: 400\">Constant\u00a0<\/span><\/td>\n<td><span style=\"font-weight: 400\">O(1)<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">Logarithmic\u00a0<\/span><\/td>\n<td><span style=\"font-weight: 400\">O(log n)<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">Linear<\/span><\/td>\n<td><span style=\"font-weight: 400\">O(n)<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">n log n<\/span><\/td>\n<td><span style=\"font-weight: 400\">O(n log n)<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">Quadratic\u00a0<\/span><\/td>\n<td><span style=\"font-weight: 400\">O(n<\/span><span style=\"font-weight: 400\">2<\/span><span style=\"font-weight: 400\">)<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">Cubic\u00a0<\/span><\/td>\n<td><span style=\"font-weight: 400\">O(n<\/span><span style=\"font-weight: 400\">3<\/span><span style=\"font-weight: 400\">)<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">Polynomial\u00a0<\/span><\/td>\n<td><span style=\"font-weight: 400\">n<\/span><span style=\"font-weight: 400\">O(1)\u00a0<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">Exponential\u00a0<\/span><\/td>\n<td><span style=\"font-weight: 400\">2<\/span><span style=\"font-weight: 400\">O(n)\u00a0<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Here, I have given only worst-case complexities for common types of equations because we are mostly interested in how an algorithm will behave in the worst-case.<\/p>\n<h3>Time complexity of some most commonly used algorithms<\/h3>\n<p>Let us see the worst-case time complexities of some important searching and sorting algorithms:<\/p>\n<h4>1. Searching techniques:<\/h4>\n<table>\n<tbody>\n<tr>\n<td><b>Algorithm<\/b><\/td>\n<td><b>Worst-case complexity<\/b><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">Linear search<\/span><\/td>\n<td><span style=\"font-weight: 400\">O(n)<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">Binary search<\/span><\/td>\n<td><span style=\"font-weight: 400\">O(log n)<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4>2. Sorting techniques:<\/h4>\n<table style=\"height: 475px\" width=\"424\">\n<tbody>\n<tr>\n<td><b>Algorithm<\/b><\/td>\n<td><b>Worst-case complexity<\/b><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">Insertion sort\u00a0<\/span><\/td>\n<td><span style=\"font-weight: 400\">O(n<\/span><span style=\"font-weight: 400\">2<\/span><span style=\"font-weight: 400\">)<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">Selection sort<\/span><\/td>\n<td><span style=\"font-weight: 400\">O(n<\/span><span style=\"font-weight: 400\">2<\/span><span style=\"font-weight: 400\">)<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">Bubble sort<\/span><\/td>\n<td><span style=\"font-weight: 400\">O(n<\/span><span style=\"font-weight: 400\">2<\/span><span style=\"font-weight: 400\">)<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">Merge sort<\/span><\/td>\n<td><span style=\"font-weight: 400\">O(n logn)<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">Quicksort<\/span><\/td>\n<td><span style=\"font-weight: 400\">O(n<\/span><span style=\"font-weight: 400\">2<\/span><span style=\"font-weight: 400\">)<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">Radix sort<\/span><\/td>\n<td><span style=\"font-weight: 400\">O(nk)<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">Bucket sort<\/span><\/td>\n<td><span style=\"font-weight: 400\">O(n<\/span><span style=\"font-weight: 400\">2<\/span><span style=\"font-weight: 400\">)<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>Conclusion<\/h3>\n<p>Without asymptotic analysis, we can\u2019t judge which algorithm is the most suitable for us.<\/p>\n<p>Suppose we wish to calculate the sum of first n natural numbers. One algorithm does that in O(1) time and the other algorithm takes O(n) time in the worst-case. Having their asymptotic analysis, we surely know that the first algorithm will be faster and more efficient.<\/p>\n<p>This is why the study of asymptotic analysis is the fundamental step to learn the working of algorithms.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article, we are going to learn about the concept of asymptotic analysis and various asymptotic notations used for the analysis of any algorithm. Defining asymptotic analysis Whenever we write an algorithm for&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":81106,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3555],"tags":[3559,3560,3561,3562],"class_list":["post-81060","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-data-structure","tag-advantages-of-asymptotic-notations","tag-asymptotic-analysis","tag-asymptotic-notations","tag-data-structures-asymptotic-analysis"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Data Structures Asymptotic Analysis - TechVidvan<\/title>\n<meta name=\"description\" content=\"Asymptotic analysis is a general method to compare or to find the efficiency of any algorithm. Learn about different asymptotic notations.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/techvidvan.com\/tutorials\/data-structures-asymptotic-analysis\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Data Structures Asymptotic Analysis - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"Asymptotic analysis is a general method to compare or to find the efficiency of any algorithm. Learn about different asymptotic notations.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/data-structures-asymptotic-analysis\/\" \/>\n<meta property=\"og:site_name\" content=\"TechVidvan\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/TechVidvan\/\" \/>\n<meta property=\"article:published_time\" content=\"2021-06-17T03:30:51+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/06\/DS-Asymptotic-Analysis-1.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:site\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"TechVidvan Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"15 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Data Structures Asymptotic Analysis - TechVidvan","description":"Asymptotic analysis is a general method to compare or to find the efficiency of any algorithm. Learn about different asymptotic notations.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/techvidvan.com\/tutorials\/data-structures-asymptotic-analysis\/","og_locale":"en_US","og_type":"article","og_title":"Data Structures Asymptotic Analysis - TechVidvan","og_description":"Asymptotic analysis is a general method to compare or to find the efficiency of any algorithm. Learn about different asymptotic notations.","og_url":"https:\/\/techvidvan.com\/tutorials\/data-structures-asymptotic-analysis\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2021-06-17T03:30:51+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/06\/DS-Asymptotic-Analysis-1.jpg","type":"image\/jpeg"}],"author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@vidvantech","twitter_site":"@vidvantech","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"15 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/data-structures-asymptotic-analysis\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/data-structures-asymptotic-analysis\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22"},"headline":"Data Structures Asymptotic Analysis","datePublished":"2021-06-17T03:30:51+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/data-structures-asymptotic-analysis\/"},"wordCount":3000,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/data-structures-asymptotic-analysis\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/06\/DS-Asymptotic-Analysis-1.jpg","keywords":["Advantages of Asymptotic Notations","Asymptotic Analysis","Asymptotic Notations","Data Structures Asymptotic Analysis"],"articleSection":["Data Structure Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/data-structures-asymptotic-analysis\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/data-structures-asymptotic-analysis\/","url":"https:\/\/techvidvan.com\/tutorials\/data-structures-asymptotic-analysis\/","name":"Data Structures Asymptotic Analysis - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/data-structures-asymptotic-analysis\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/data-structures-asymptotic-analysis\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/06\/DS-Asymptotic-Analysis-1.jpg","datePublished":"2021-06-17T03:30:51+00:00","description":"Asymptotic analysis is a general method to compare or to find the efficiency of any algorithm. Learn about different asymptotic notations.","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/data-structures-asymptotic-analysis\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/data-structures-asymptotic-analysis\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/data-structures-asymptotic-analysis\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/06\/DS-Asymptotic-Analysis-1.jpg","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/06\/DS-Asymptotic-Analysis-1.jpg","width":1200,"height":628,"caption":"DS Asymptotic Analysis"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/data-structures-asymptotic-analysis\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Data Structures Asymptotic Analysis"}]},{"@type":"WebSite","@id":"https:\/\/techvidvan.com\/tutorials\/#website","url":"https:\/\/techvidvan.com\/tutorials\/","name":"TechVidvan Blogs","description":"","publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/techvidvan.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/techvidvan.com\/tutorials\/#organization","name":"TechVidvan","url":"https:\/\/techvidvan.com\/tutorials\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","width":200,"height":50,"caption":"TechVidvan"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/TechVidvan\/","https:\/\/x.com\/vidvantech"]},{"@type":"Person","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22","name":"TechVidvan Team","description":"The TechVidvan Team delivers practical, beginner-friendly tutorials on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our experts are here to help you upskill and excel in today\u2019s tech industry."}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/81060","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/comments?post=81060"}],"version-history":[{"count":0,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/81060\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/81106"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=81060"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=81060"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=81060"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}