 Hello friends. I am Sanjay Gupta. I welcome you on Sanjay Gupta Tech School. So in my previous video, I explained you algorithm analysis. So I discussed with you space complexity and time complexity. So there you understood how we can measure space complexity and through three different ways we can identify time complexity that are best case, average case and worst case. So after watching that video, now you can understand algorithm efficiency with this video. So starting my explanation for algorithm efficiency. So explaining first point which says if a function is linear without any loops or recursion, the efficiency of that algorithm or the running time of that algorithm can be given as the number of instructions it contains. So however, if an algorithm contains certain loops or recursive function, then the efficiency of the algorithm may vary depending on the number of loops and the running time of each loop in the algorithm. So first paragraph is saying like we don't have any loop or recursion. Second paragraph or second point is saying we have loop or recursion. So accordingly we can identify the efficiency of algorithm based on number of steps. Those are available in that algorithm. So if we don't have any loop, then efficiency will be different. But if we have loops or recursion, then efficiency will be different. So as per third point, the efficiency of an algorithm is expressed in terms of the number of elements that has to be processed. So this is also important, a number of elements. So if n is the number of elements, then the efficiency can be stated as function of n equals to efficiency. So n is the number of elements on which we are going to apply algorithm. And in that algorithm, if you are having loops, then its efficiency will be different. And if you are not using any loop, then its efficiency will be different. So here in first point, if you focus again, so it is saying the efficiency of the algorithm or the running time of that algorithm can be given as the number of instructions it contains. But for the algorithm which contains loops or recursion. So for that, different cases to determine efficiency of algorithms are available here. So here you can see in each point loop is available. So we can have linear loops, we can have logarithmic loops, we can have nested loops like linear logarithmic loops, quadratic loops and dependent quadratic loops. So based on these six points, now I'm going to explain how you can identify your algorithm efficiency. So first point is linear loops. So in case of linear loop, here you can see the first point to calculate the efficiency of an algorithm that has a single loop. We need to first determine the number of times the statements in the loop will be executed. So with this example, you will understand how we can identify the efficiency of linear loops. So this loop will repeat 100 times and 100 times the statements will be executed. So here 100 is the loop factor. Hence the general formula in the case of linear loops may be given as function of n equals to n. So if we need to process n elements, so its efficiency will be equals to n. Then here you can see example number 2, here increment is done with 2 and condition is 100, so 0 to 100 but increment is done by 2. So here the number of iterations is just half the number of loop factor. So here the efficiency can be given as function of n equals to n by 2. So as per the iteration of loop, we are identifying the efficiency of the algorithm. So in first case, efficiency is n, in second case it is n by 2 and in both the case function of n is performing. So this way we can identify the linear loops efficiency based on the number of times loops are executing. Then our next case is logarithmic loops. So in linear loops the loop update either adds or subtracts. However in logarithmic loops the loop controlling variable is either multiplied or divide during each iteration of the loop. So here you can see loop is iterating 100 times but it seems like it will be iterating 100 times but here if you see this increment part. So here i into equals to 2 is mentioned, so it means i equals to i into 2. So every time it will be i will be multiplied by 2. So in the above loop in which the loop controlling variable i is multiplied by 2. So after each iteration of the loop, the loop will be executed only less than 10 times and not 100 times. So therefore we can conclude that the efficiency of loops in which iterations divide or multiply the loop controlling variables can be given as log n. So in previous case you saw where loop was executing 100 times so it was equals to n because we are considering n as 100. So its efficiency will be n but in second case loop was repeating n by 2 times. So number of elements was 100 but loop was iterating n by 2 times so its efficiency was n by 2. Here again n is 100 but its loop is iterating less than 10 times so its efficiency will be log n. So in the case where loop is loop iteration variable is multiplied or divided. So in that case you can use this kind of efficiency like log n. So this way I hope you understood two scenarios linear loops and logarithmic loops. Now third is nested loops. So loops that contain loops are known as nested loops. In order to analyze nested loops we need to determine the number of iterations each loop completes. The total is then obtained as the product of number of iterations in the inner loop and the number of iterations in the outer loop. So this is the formula we need to multiply inner loop iteration with outer loop iteration so that total number of iterations can be calculated. Here you need to note one thing. In this case we analyze the efficiency of the algorithm based on whether it is a linear logarithmic, quadratic or dependent quadratic nested loops. So this was about nested loops. Now we are going to understand these three types. Linear logarithmic, quadratic and dependent quadratic loops. So first is linear logarithmic. So I already explained you linear and logarithmic separately. If I go back so this was linear and this was logarithmic. Now we are going to combine both. So consider the following code in which the loop controlling variable of the inner loop is multiplied after each iteration. So the number of iterations in the inner loop is log 10. This inner loop is controlled by an outer loop which it trades 10 times. So therefore according to the formula the number of iterations for this code can be given as 10 log 10. So here you can see outer loop is iterating as linear loop and inner loop is iterating as logarithmic loop. And together we can say this is as linear logarithmic loop. So outer loop is iterating n time and inner loop is iterating log n time. So the efficiency of such loops can be given as n log n. So function of n equals to n log n so n will be the elements. So if I go back so here you can see n is the number of elements then the efficiency can be stated as so everywhere we are using f of n as same and efficiencies are different as per the number of loops and how we are incrementing or how we are controlling the loop variable. So accordingly the efficiency is measured. So this was about linear logarithmic loop. Now next one is quadratic loop. So in a quadratic loop the number of iterations in the inner loop is equal to number of iteration in the outer loop. We can also say like both the loops are immediate so this becomes quadratic loop. So consider the following code in which the outer loop executes 10 times and for each iteration of the outer loop the inner loop also executes 10 times. So therefore the efficiency here is 100. So the generalized formula for quadratic loops can be given as n square. So if both the loops are executing a similar number of time they are iterating similar number of time and they are implemented as nested. So then efficiency will be n square. So for n input the efficiency will be n square because we are implementing this kind of loop. So number of inputs are same here as well f of n but efficiency is different because you are applying different kind of loop here. So this way I hope you are understanding if you are using a particular type of loop in your algorithm then according to that loop your algorithm efficiency will be calculated. So let's say to solve any problem if you are using two methods in one method you are using linear loop one loop and in another method let's say you are using two nested loops. So in both the cases your algorithm efficiency will be different and you need to use whose efficiency is less. Like whose efficiency is best. So let's say linear loop is providing efficiency as n and quadratic loop is providing efficiency as n square. So obviously you need to focus on the n because that is more efficient or more to be used in your algorithms as compared to n square algorithm efficiency. And last one is dependent quadratic loops. So here you will see one loop will be dependent upon the another loop. So in the dependent quadratic loop the number of iterations in the inner loop is dependent on the outer loop. So I hope you implemented these kind of loops by implementing patterns and see language. So consider the code given below which shows such as such an example. So first is iterating 10 times and second is dependent upon the value of high. So in this code the inner loop will execute just once in the first iteration twice in the second iteration twice in the third iteration and so on. So in a loop number of rotations are totally dependent upon the value of five that is controlling the first loop. So in this way the number of iterations can be calculated as. So this is the iteration for inner loop 1, 2, 3, 4, 5 up to 10 and total is 55. So if we calculate the average of this loop so it will be 55 upon 10 equals to 5.5. So we will observe that it is equal to the number of iteration in the outer loop plus 1 divided by 2. So this way in general terms the inner loop iterates n plus 1 by 2. So n is the number of rotation of outer loop we are adding 1 and dividing the whole by 2. So therefore the efficiency of such a code can be given as so for n number of inputs your algorithm is giving this efficiency that is n into n plus 1 by 2. So here first n is indicating number of iteration for outer loop and it is multiplied with n plus 1 by 2 which is the efficiency or number of iteration of inner loop. So this way this dependent quadratic loop algorithm efficiency can be calculated. So this is how you need to calculate algorithm efficiency while implementing any algorithm in data structure. So I hope you understood all the scenarios that I explained in this video. So if you want to calculate efficiency of function of n so where n is the number of elements and as per your algorithm code or algorithm steps like you are using how many loops are without loop you are implementing that algorithm. You can identify the efficiency of that algorithm. So with this explanation I am going to conclude this video. So also watch my previous video so that you can also understand like the difference between space and time complexity. And after watching that you will surely understand this like how we can identify algorithm efficiency so that you can write better algorithms while implementing the solution of any problem. So if you want to watch more data structure related videos or data structure implementation with C. So you can go to description of this video where you will find playlist links and also at the end of this video you will find those links as well. So you can follow those playlist so that you can understand the data structure and algorithm concepts well. So I hope you understood whatever I explained in this video. Thank you for watching this video.