Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 19th, 2005, 7:19 AM   #1
panzerss
Newbie
 
Join Date: Mar 2005
Posts: 2
Rep Power: 0 panzerss is on a distinguished road
Unhappy Time Complexity Problem

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!!!
panzerss is offline   Reply With Quote
Old Mar 19th, 2005, 9:11 AM   #2
tempest
Programming Guru
 
tempest's Avatar
 
Join Date: Oct 2004
Posts: 1,041
Rep Power: 6 tempest is on a distinguished road
Send a message via ICQ to tempest Send a message via AIM to tempest Send a message via Yahoo to tempest
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.
__________________

tempest is offline   Reply With Quote
Old Mar 19th, 2005, 12:15 PM   #3
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
I think he wants the Big O of that loop.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Mar 19th, 2005, 2:01 PM   #4
panzerss
Newbie
 
Join Date: Mar 2005
Posts: 2
Rep Power: 0 panzerss is on a distinguished road
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???
panzerss is offline   Reply With Quote
Old Mar 19th, 2005, 5:21 PM   #5
kurifu
Expert Programmer
 
kurifu's Avatar
 
Join Date: Jul 2004
Location: Halifax, Nova Scotia (Canada)
Posts: 784
Rep Power: 5 kurifu is on a distinguished road
Send a message via ICQ to kurifu Send a message via MSN to kurifu
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 &lt;geek@cliffordroche.com&gt;
Web Hosting: http://www.crd-hosting.com
Consulting: http://www.crdev-consulting.com
kurifu is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:49 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC