Question:
java Big-O help...really dont understand this..?
suma29
2009-01-25 17:13:15 UTC
Describe the order of magnitude of each of the following functions using Big-O notation

a) N2 + 3N
b) 3N2 + N
c) N5 + 100N3 + 245
d) 3Nlog2N + N2
e) 1 + N + N2 + N3 + N4
f) (N * (N-1) ) /2

44 : Describe the order of magnitude of each of the following code sections, using Big-O notation

a) count =0;
for (i = 1; i <=N ; i++)
count++;

b) count = 0;
for (i = 1; i <=N ; i++)
for (j = 1; j <=N ; j++)
count++;
c) value = N;
count = 0;
while (value > 1)
{
value = value / 2;
count++;
}
Five answers:
Icarus Rising
2009-01-25 17:45:40 UTC
Edit: Big-O notation is supposed to give you a sense of how an algorithm will scale as the size of the dataset that it is working on increases. If an algorithm takes 1 second to process one piece of data, and 10 seconds to process 10 pieces, it may be linear. O(n) represents linear -- graph out y = x to understand that. An algorithm that takes 100 seconds to process 10 items might be on the order of n^2. As the data set increases, it takes a long time for the algorithm to finish. In your assignment, the first several problems already provide you the complexity of different components of some algorithm (the actual algorithm is unimportant as the analysis has already been done for you). The second part of your assignment gives you a chance to try to come up with those complexity functions yourself. Big O is not concerned with the ACTUAL complexity, but rather with the overall speed of growth as the data set increases. The mentality here is that no matter how fast you make your code, the growth rate of the curve will always look the same. It is really used to compare algorithms to one another rather than provide any real indication of how long a particular algorithm will take.



In Big-O notation, the largest "order" is the winner. Until you get used to identifying order by sight, a good way to handle these problems is to:



1) split out the functions into a sum of parts

2) for each part, replace any constant coefficients with 1.

3) keep ONLY the part the grows the fastest on a graph.

4) repeat steps 1-3 until you are unable to split the function up any more



a) Apply the procedure I laid out:

1. split into N^2 and 3N

2. throw away the 3. N^2 and N remain

3. N^2 grows fastest. Keep it.

4. We can't split up N^2 so we are done. The answer is O(N^2)



b)

1. 3N^2 and N

2. N^2 and N

3. N^2 grows fastest

4. Done. It is O(N^2)



c)

1. N^5, 100N^3, 245

2. N^5, N^3, 1

3. N^5 grows fastest

4. Done, it is O(N^5)



d)

1. 3NLog2N, N^2

2. NLog2N, N^2 (I'm not sure I'm reading Log(2N) correctly)

3. I know Log(2N) is slower than N, so N^2 is faster

4. Done, it is O(N^2)



you can do e)



f)

1. (N * (N - 1) ) / 2

2. N * (N - 1)

3. Keep it all and simplify N^2 - N

1. N^2, -N

2. N^2, N

3. N^2

4. Done... O(N^2)





44. I'll give you the formulae... you can solve the order. Notice that to be absolutely complete, I can represent EVERY line in an order equation. Loops multiply... everything else adds.



a) 1 + N * (1)

b) 1 + N * (N * (1)

c) 1 + 1 + ...



ok... this can be tricky... you have to investigate the algorithm to see how long it will take to run. Worst case, you can keep a counter inside the loops. Vary the N and see how it relates to the counter. I can tell looking at this that it divides the number over and over until it reduces to 1. This takes log2(N) time. Not all algorithms are open to analysis. If a look runs and will complete at some unknown time in the future, it is simply labeled "decidable". If it might NEVER end in some situations, then it is "partially decidable". If it will NEVER end ever, it is "undecidable". These are the worst three classifications to be in. But you are probably not there in your studies, and will not encounter them as problems.



so... 1 + 1 + log2(N)(1 + 1)



Not even tricky...

1. 1,1,log2(n),log2(n)

2. same

3. log2(n) grows the fastest (pick either one, they are the same)

4. Can't break up log2(n), so we're done. Answer is O(log2(n))
fackelman
2016-10-06 10:36:59 UTC
Big O Notation Java
?
2016-10-25 19:32:13 UTC
i imagine you would have misunderstood some thing even as listening to about the large Bang concept. the large Bang concept states that each and every thing is produced from a tiny aspect, smaller than an atom (no longer an Atom). even as the bang exploded, (and do not ask the position this got here from because no one knows of in any respect), they say that because it accelerated Subatomic debris formed (one of those count number number). those were smaller than atoms - be conscious that there have been not any atoms acceptable contained in the starting up of the Universe till 380,000 years after - and they were chaotic. Very, very chaotic. As in, that they'd pass from one position to the different, some might want to look on an same position and it develop into all basically mayhem. After wards - please recognize if I do get information incorrect, i do not want you to quote me, that is basically what I genuinely have heard - count number number and anti-count number number annihilated one yet another in, say, a conflict. After this 'conflict' count number number had one. Gravity, on the instantaneous, develop into somewhat sturdy and now appears to be like very susceptible. carry a series of keys and drop it to the floor, it truly is gravity. notwithstanding, carry a magnet and the magnet will entice the keys and Gravity loses - unusual! After 380,000 years or so atoms began to form which created Galaxies, stars, planets, moon and extra. and that is slightly description. yet do not quote me on each and each and every of the documents!
Blackcompe
2009-01-25 17:23:56 UTC
a) N2 + 3N = n^2

b) 3N2 + N = n^2

c) N5 + 100N3 + 245 = n^2

d) 3Nlog2N + N2 = log(n)

e) 1 + N + N2 + N3 + N4 = 2^n

f) (N * (N-1) ) /2 = n



O(n)

------

a) count =0;

for (i = 1; i <=N ; i++)

count++;



O(n^2)

------

b) count = 0;

for (i = 1; i <=N ; i++)

for (j = 1; j <=N ; j++)

count++;



O(n) or O(log(n))

------

c) value = N;

count = 0;

while (value > 1)

{

value = value / 2;

count++;

}
anonymous
2009-01-25 17:17:02 UTC
What exactly don't you understand about this?



EDIT: The question asks you to find Big-O notation for the given algorithms.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...