Chapter 2 Big O notation questions and explanations (package meeting), bignotation

Source: Internet
Author: User

Chapter 2 Big O notation questions and explanations (package meeting), bignotation

I have listed some difficult questions or error-prone questions. I have not explained them too much, but if you have any questions, please trust me or comment on them, I will try to answer questions or questions in real time.


Int x = 0;

For (int I = 4 * n; I> = 1; I --)
X = x + 2 * I;


O (n)

The loop runs O (n) times and does O (1) work per iteration.




Int z = 0;
Int x = 0;
For (int I = 1; I <= n; I = I * 3 ){
Z = z + 5;
Z ++;
X = 2 * x;

}


O (log n)


Think about the values of I as the loop progresses. it will take on the series of values 1, 3, 9, 27, 81,243 ,..., 3 k. since I is tripling on each iteration, it takes on successive powers of three.


The loop clearly only does O (1) work per iteration, so the main


Question here is how many total iterations there will be. The loop


Will stop when I> n. If we let k be some arbitrary iteration of


Loop, the value of I on iteration k will be 3 k. The loop stops when


3 k> n, which happens when k> log3 n. Therefore, the number


Iterations is only O (log n)




Int y = 0;
For (int j = 1; j * j <= n; j ++)
J <= (n) ^ 1/2 y ++;
O (√ n)
Notice that j is still growing linearly, but the loop runs as long


J2 ≤ n. This means that as soon as j exceeds √ n, the loop will


Stop. Therefore, there will only be O (√ n) iterations of the loop,


And since each one does O (1) work, the total work done is O (√ n)


Int B = 0; // constant
For (int I = n; I> 0; I --)
For (int j = 0; j <I; j ++)

B = B + 5;



O (n ^ 2)
The most accurate answer wocould be O (n2)


Int y = 1;
Int j = 0;
For (j = 1; j <= 2n; j = j + 2)
Y = y + I;


Int s = 0;
For (I = 1; I <= j; I ++)

S ++;



O (n)


Int B = 0;
For (int I = 0; I <n; I ++)
For (int j = 0; j <I * n; j ++)
B = B + 5;


The inner loop will run 0 + n + 2n + 3n + 4n +... + n (n-1) = n (0 + 1


+ 2 +... + n-1) times, so the total work done is O (n3). You


Shouldn't multiply by the number of times the outer loop runs because


You're already summing up your SS all iterations. The most accurate


Runtime wocould be O (n3)


Int t = 0;
For (int I = 1; I <= n; I ++)
For (int j = 0; j * j <4 * n; j ++)
For (int k = 1; k * k <= 9 * n; k ++)
T ++;


Look at the second loop. This actually runs O (√ n) times using


Same logic as one of the earlier parts. That third inner loop also


Runs O (√ n) times, and so the total work done will be O (n2)




Int a = 0;
Int k = n * n;
While (k> 1)
{
For (int j = 0; j <n * n; j ++)
{A ++ ;}
K = k/2;
}
The outer loop starts with k initialized to n2, but notice that k is


Halved on each iteration. This means that the number of iterations


The outer loop will be log (n2) = 2 log n = O (log n), so the outer


Loop runs only O (log n) times. That inner loop does do O (n2) work, so


The total runtime is O (n ^ 2 log n)


Int I = 0, j = 0, y = 0, s = 0;
For (j = 0; j <n + 1; j ++)
Y = y + j;
For (I = 1; I <= y; I ++)

S ++;


O (n ^ 3)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.