what will be the value of x after the following code is executed? This is a topic that many people are looking for. amritsang.org is a channel providing useful information about learning, life, digital marketing and online courses …. it will help you have an overview and solid multi-faceted knowledge . Today, amritsang.org would like to introduce to you Compute The Time Complexity Of The Following Code. Following along are instructions in the video below:
Youtube. This is a video on computing. The asymptotic complexity of the following code fragment.
Fragment. So here we have the following code fragment in red and theres two things that we need to know one is the cost of each statement and the amount of times that each of these statements are ran. So first we have this i equals 1.
There we go so we have i equals. 1. And the cost may be some unit of time that we dont know so we just put a c for constant.
And then say its a subscript 1. Because its gonna be mini constants and its written one time the same here sum equals zero. It has a cost that we dont know we call it c.
Subscript. 2. Because it may be different from the first one and it runs one time next year.
We have a while loop while i is less than or equal to n. I equals 1. And it increases by 1 each time so this runs in plus one time and this may take some constant of time we call it c.
3. Now you may think why does this run and plus. One time and not in well the check itself has to check the very last number to know when to execute so for example.
If n equal. 3. Then its a run from i equals 1.
So we said its 1 less than or equal to 3. Well say yes go through the while loop. I increases.
It becomes 2 its 2 less than or equal to 3 yes go through while loop and now i equals. 3. Is 3 less than or equal to 3.
Yes go through the while loop. So i need to know when to stop and on this last one here i becomes. 4.
Is i less than equal to 3. No it stops so you can see that although n. Equals.
3. Is executed 1 2. 3.
4. Times. So.
Thats n plus. 1. Time okay next.
We have this statement. Jake was one and its ran within the loop. Here the loop itself only runs end times.
So it takes some constant amount of time for and its ran on the end times. Because its within the loop next. We have this wild loop.
Here and the wild loop. Itself. Runs n.
Plus. 1. Time.

Because j. Equals. 1.
And then j increases by 1 each. Time. So.
It takes some constant unit of time call it c 5 n. Plus 1. But wait this wild loop is within a while loop and that while loop runs end times.
So we multiply this times in and we get n. Squared plus n. All right next.
We have some equals. Some plus 1. It takes some constant of time we call it c 6.
And its ran in times from the inner loop here and its also ran in times from the outer loop. So the running time is n. Times n.
Which equals n. Squared. Same for this statement.
Here is ran n times. Within the inner loop and n times within the outer loop. And it takes some constant of time we call it c7 so it runs in x and x.
Which equals n. Square. And last is i equals.
I plus 1 um. And this statement costs some amount of time we call it c8. And its only ran on the outer while loop.
So its run in time. Okay. So now here we have all of our costs and our times and so what were going to need to do is were going to need to multiply them and add them so we need to multiply the costs by their time.
So. It is a c8 here and we need to to add them together to figure out the running time now you can do that that way or theres another way you can just notice that the largest number here would be n. Squared and you can say that oh well this runs big data and squared or big o of n.
Squared. The more mathematical way. I will show you now so first we have c1 times.
1. So lets put down here total cost equals we have c1 times 1 which is c1 plus c2. Times 1 which is c 2 plus c 3 times n plus c 3.
Times. 1. So.
We have c. 3. Or.
Plus. C. 3.
Plus. N. Times.
C. 3. And actually lets go.

Ahead. And. Change.
C. 1. Plus.
C. 2. Plus c.
3. To some other constant number. Well call that constant ac9 to help make our equation.
Smaller. So let c. 1.
Plus. C. 2.
Plus c. 3. Equal c.
9. All right so now we just have a c 9. There plus c plus n.
Times c. 3. Okay.
I didnt want to raise that there so now we want to add c. 4. Times.
In so plus n. Times c. 4.
Next. We have c 5. Times.
N. Squared. Plus.
N. So. We have a in times.
C. 5. Plus n.
Squared. Times c. 5.
Now. We have plus c. 6.
Times. N. Squared.
And plus c. 7. Times.

N. Squared. So c.
6. Sorry. N.
Squared. Times. C.
6. Plus. N.
Squared times. C. 7.
Plus. We have our c 8. Here.
And in multiply. It by n. So plus n.
Times c. 8. So thats going to give us our total cost.
Lets go ahead and group. Some of these lets group this in time c8 with this with all these variables here that are being multiplied. It by in as well so plus and times c.
8. And now we can rewrite this equation. Again.
If we let c. 3. Plus.
C. 4. Plus c.
5. Plus. C.
8. Equal. Well call it c.
10. All right. So rent let all that equal c.
10. So. Thats n.
Times. C. 3.
N. Times. C.
4. N. Times.

C. 5. And n.
Times. C. 8.
Thats what i move. This down. 1.
Here because were just grouping. Plus and squared times c. I believe that was 5 ok.
And we can move this over and x c8. So you can see im just. Grouping um.
The variables. Together. So.
Now we can rewrite this. Though. We have c.
9. Plus. N.
Times. C. Subscript n.
All right. And then we still have our rhythm stuff down here. So we covered all this here that we need to add and we can just say thats n squared times.
Some other constant. Because if we let c. Six plus c.
Seven plus c. Five equals. Some other constant.
Well call it c. Eleven. So that is equal c.
Eleven. Then we get n squared times c. Eleven.
And so now our total cost equals. Some constant c. Nine plus n.
Times. Some constant c. 10 plus n.
Squared times. Some constant. We call c11 kind of looks familiar if we rewrite this again.
You can see that it is a c11. N. Squared.
Plus c 10 n plus c 9. And if we replace the variables. C11 c10 and c9 with something like this a squared plus bn plus c and you can see that the constants dont really matter.
Its just the this last variable here that matters the n squared. So this is equal to or belongs to big theta of n square alright thank you guys .

Thank you for watching all the articles on the topic Compute The Time Complexity Of The Following Code. All shares of amritsang.org are very good. We hope you are satisfied with the article. For any questions, please leave a comment below. Hopefully you guys support our website even more.