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?

12 Years
Discussion Span
Last Post by david31337

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


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


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!

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.