Time of a java method

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

Join Date: Mar 2006
Posts: 3
Reputation: david31337 is an unknown quantity at this point 
Solved Threads: 0
david31337 david31337 is offline Offline
Newbie Poster

Time of a java method

 
0
  #1
Mar 6th, 2006
try {
     for (int j=0; j < length; j++) { 
              int indx = s.indexOf(text.charAt(j)); 
                if (indx >= 27 && indx <= 36) { 
                    code += ((indx + key)%26); 
                }
               else { 
                    code += s.charAt((indx + key)%36); 
               }
        }
 }
Hi, Im pritty new to java, I wrote this code and just out of intrest was wondering about it's time complexity. Is there any way to work out max and min run times for any given value? I've been looking into this a bit, and have come up with nothing. Anyway any insite on how to start/finish this would be great?
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,850
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: 754
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: Time of a java method

 
0
  #2
Mar 6th, 2006
>and just out of intrest was wondering about it's time complexity
Um, O(N). Loops are what you look for primarily when working out time complexity, and since there's only one loop, it's pretty straightforward.
New members chased away this month: 4
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 2,055
Reputation: Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice 
Solved Threads: 139
Team Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Super Senior Demiposter

Re: Time of a java method

 
2
  #3
Mar 6th, 2006
It is O(length), but what is the variable s? Is it a string of fixed size? Is it a variable length string? If so, then generally, your running time is O(length * s.length()), because the indexOf method should take O(s.length()) time in the average and worst case. Holding s.length() fixed, your running time is O(length).
All my posts may be redistributed under the GNU Free Documentation License.
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 3
Reputation: david31337 is an unknown quantity at this point 
Solved Threads: 0
david31337 david31337 is offline Offline
Newbie Poster

Re: Time of a java method

 
0
  #4
Mar 7th, 2006
S = a string of 36 letters and numbers.
Thanks for the replies.
So s is fixed so the running time in O(length)?! What does that actually mean, is that a good running time? Im guessing here, but is that this big O theory? Sorry for being stupid, but my course don't cover this and the info about it on the net is very confusing!
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,850
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: 754
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: Time of a java method

 
0
  #5
Mar 7th, 2006
http://www.eternallyconfuzzled.com/articles/bigo.html

I usually don't plug my own stuff out of principle, but any other explanations I've found have been overly mathematical and confusing even to me. And I like to think that I have an above average understanding of these things. :rolleyes:
New members chased away this month: 4
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 3
Reputation: david31337 is an unknown quantity at this point 
Solved Threads: 0
david31337 david31337 is offline Offline
Newbie Poster

Re: Time of a java method

 
0
  #6
Mar 7th, 2006
That is a good link! makes sense now! Thanks all for the replies!
Reply With Quote Quick reply to this message  
Reply

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




Views: 2427 | Replies: 5
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