![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Mar 2005
Posts: 2
Rep Power: 0
![]() |
What would be the time complexity (accurate) of the following?
n=number of elements ... for(i=0;i<n-2;i++) for(j=i+1;j<n-1;j++) for(k=j+1;k<n;k++) Now if were there just i and j loop the accurate time complexity would be n^2-n/2 but with the third loop i get a bit confused. PLEASE HELP!!! |
|
|
|
|
|
#2 |
|
Programming Guru
![]() |
I'm sorry, is english your native language. I can't understand what you are trying to say. You've confused the hell out of me.
If you are asking how long that code will slow down your program, which i think you might be, then if n is a number greater than 10 you'll be able to see a difference.
__________________
|
|
|
|
|
|
#3 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
I think he wants the Big O of that loop.
|
|
|
|
|
|
#4 |
|
Newbie
Join Date: Mar 2005
Posts: 2
Rep Power: 0
![]() |
Take this for instance that you have some if statement in the core of the loops:
for(i=0;i<n-2;i++) for(j=i+1;j<n-1;j++) for(k=j+1;k<n;k++) if(something is something blabla) How many times would the if statement be executed??? |
|
|
|
|
|
#5 |
|
Expert Programmer
|
Big O should be the same, since they are all linear equations (I actually forget how to spell that word).
These for statements would have varying complexity (if by complexity you mean big O) for( ;; ); for( int i = 0; i < n; i++ ) for( int i = 0; i < n; i *= 2 ) First one never converges, second one converges linearly, and the third converges at a rate of O(2) or something like that.
__________________
Clifford Matthew Roche <geek@cliffordroche.com> Web Hosting: http://www.crd-hosting.com Consulting: http://www.crdev-consulting.com |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|