954,483 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Time of a java method

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?

david31337
Newbie Poster
3 posts since Mar 2006
Reputation Points: 10
Solved Threads: 0
 

>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.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

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).

Rashakil Fol
Super Senior Demiposter
Team Colleague
2,658 posts since Jun 2005
Reputation Points: 1,135
Solved Threads: 177
 

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!

david31337
Newbie Poster
3 posts since Mar 2006
Reputation Points: 10
Solved Threads: 0
 

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:

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

That is a good link! makes sense now! Thanks all for the replies!

david31337
Newbie Poster
3 posts since Mar 2006
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You