Big O Notations...Need Help

Please support our Computer Science advertiser: Learn about neural networks and artificial intelligence.
Reply

Join Date: Nov 2004
Posts: 68
Reputation: harshchandra is an unknown quantity at this point 
Solved Threads: 1
harshchandra harshchandra is offline Offline
Junior Poster in Training

Big O Notations...Need Help

 
0
  #1
Nov 28th, 2004
hi everyone
dus u plzz tell me the complexities of following codes.

A nested loop followed by a non-nested loop:

1.for (i = 0; i < N; i++) {
for (j = 0; j < N; j++) {
sequence of statements
}
}
for (k = 0; k < N; k++) {
sequence of statements
}


2.A nested loop in which the number of times the inner loop executes depends on the value of the outer loop index:
for (i = 0; i < N; i++) {
for (j = i; j < N; j++) {
sequence of statements
}
}
Reply With Quote Quick reply to this message  
Join Date: Feb 2002
Posts: 12,054
Reputation: cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light 
Solved Threads: 130
Administrator
Staff Writer
cscgal's Avatar
cscgal cscgal is online now Online
The Queen of DaniWeb

Re: Big O Notations...Need Help

 
0
  #2
Nov 28th, 2004
Dani the Computer Science Gal
Follow my Twitter feed! twitter.com/DaniWeb
And if you're interested in Internet marketing there is twitter.com/DaniWebAds
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 60
Reputation: murschech is an unknown quantity at this point 
Solved Threads: 1
murschech murschech is offline Offline
Junior Poster in Training

Re: Big O Notations...Need Help

 
0
  #3
Jan 16th, 2005
In the first case the outer loop (i loop) repeats N times and for each repeat of the outer loop (i loop) the inner loop repeats N times. As you wrote this program, the k loop runs once, having nothing to do with i or j, so whatever the loop does, it does it N^2 + N times and this is O(N^2). In the second case for i = 0 the j loop runs N times, for i = 1 it runs N-1 times, etc. So the number of times the loop does its stuff is 1+2+...+N, and this sum is N*(N+1)/2 = N^2 /2 + N/2. This is also O(N^2), so both programs do O(N^2) repeats of whatever they do.

Perhaps your question is why N^2 + N is O(N^2) and also N^2/2 + N/2 is O(N). The answer is that a function of N is called O(N^2) when that function devided by N^2 approaches a non-zero limit as N approaches infinity.

I guess I just did your homework for you. That's bad.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,824
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Narue's Avatar
Narue Narue is online now Online
Senior Bitch

Re: Big O Notations...Need Help

 
0
  #4
Jan 16th, 2005
>I guess I just did your homework for you. That's bad.
That's not an edit, so you knew that you did his homework and you knew that it was bad before you clicked submit. So were you trying to show off? If so, you failed miserably.
New members chased away this month: 3
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 207
Reputation: Real-tiner is an unknown quantity at this point 
Solved Threads: 8
Real-tiner Real-tiner is offline Offline
Posting Whiz in Training

Re: Big O Notations...Need Help

 
0
  #5
Jan 18th, 2005
The k loop runs N times, not one time.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 60
Reputation: murschech is an unknown quantity at this point 
Solved Threads: 1
murschech murschech is offline Offline
Junior Poster in Training

Re: Big O Notations...Need Help

 
0
  #6
Jan 18th, 2005
The loop runs once but, as you say, it does whatever it does N times. That's where the N in N^2+N comes from. By contrast, the j loop runs once for each value of i, therefore the j loop runs N times. Each of the N times it runs it does whatever it does N times. That's where the N^2 comes from.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Computer Science Forum
Thread Tools Search this Thread



Tag cloud for Computer Science
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC