| | |
Big O Notations...Need Help
Please support our Computer Science advertiser: Learn about neural networks and artificial intelligence.
![]() |
•
•
Join Date: Nov 2004
Posts: 68
Reputation:
Solved Threads: 1
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
}
}
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
}
}
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

Follow my Twitter feed! twitter.com/DaniWeb
And if you're interested in Internet marketing there is twitter.com/DaniWebAds
•
•
Join Date: Dec 2004
Posts: 60
Reputation:
Solved Threads: 1
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.
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.
![]() |
Similar Threads
- PHP vs ASP... the big ShOwdOwN (IT Professionals' Lounge)
- Big O and little o notations (C)
Other Threads in the Computer Science Forum
- Previous Thread: Linear Algebra crisis
- Next Thread: online camera
| Thread Tools | Search this Thread |
Tag cloud for Computer Science
ai algorithm algorithms assignment assignmenthelp assignments automata battery bigbrother binary bittorrent bizarre bletchleypark blogging bomb business codebreaker compiler computer computerscience connect conversion csc data dataanalysis dataintepretation development dfa dissertation dissertationthesis dissertationtopic employment energy extensions foreclosure foreclosuresoftware fuel geeks givemetehcodez government graphics hardware history homeowners homework homeworkassignment homeworkhelp humor ibm idea ideas internet iphone ipod jobs kindle laser laws lazy linkbait lsmeans mainframes marketing mining mobileapplication msaccess nano netbeans news os p2p parser piracy piratebay principles programming rasterizer research sam-being-cute sas science security simulation software spoonfeeding spying sql stephenfry student supercomputer supercomputing sweden technology tree turing turingtest two'scompliment virus warehouse ww2






