r/programminghelp • u/[deleted] • Feb 21 '21
Processing help with counting iterations for time complexity
hello. i asked this in /r/learnprogramming but i am still not understanding it so i was hoping i would have better luck here.
Hello. In my data structures class we are supposed to calculate the time complexity of some algorithms. The teacher said we can do it by counting the number of iterations the code runs. She says to calculate the iterations to just draw a table and count them. Then she gives the answer without really showing how she calculated it so Im confused. I was wondering if you guys could help me or know of a textbook or website that will help me to understand it. I will now give her examples and answers:
for (i=1; i<=n; i=i+4) {
...
}floor / 4 + 1 iterations when n >= 1
for (i=1; i<=n; i++)
for (j=1; j<=n; j++) {
...
}n2 iterations when n >= 1
for (i=1; i<=n; i++)
for (j=1; j<=i; j++) {
...
}n(n + 1)/2 iterations when n >= 1
for (i=1; i<=n; i++)
for (j=1; j<=i; j++)
for (k=1; k<=j; k++) {
...
}1+(1+2)+(1+2+3)+…+(1+2+3+…+n) iterations when n >= 1
for (i=1; i<=n; i=2*i) {
...
}[floor]log2 n+1 iterations when n >= 1
for (i=n; i>=1; i=i/5) {
...
}[floor]log5 n+1 iterations when n >= 1
for (i=1; i<=n; i++)
for (j=1; j<=n; j=j*2) {
...
}n(([floor]log2n) + 1) iterations when n >= 1
for (i=1; i<=n; i=2*i)
for (j=1; j<=i; j++) {
...
}1+2+4+8+… 2foor = 2([floor]log2n+1) – 1 iterations when n >= 1
Those are all the examples from the lecture. How in the world do you get the number of iterations? Everytime I search for help all I get is sites that say the time complexity is linear or quadratic, etc..
heres a link to the lecture slides if my formatting is hard to understand: https://drive.google.com/file/d/1I8X1zo0LAfszNm-wsgo8C4JoiCnB4yzz/view?usp=sharing