Jump to content

Time complexity HELP grrr

- - - - -

  • Please log in to reply
9 replies to this topic

#1
toto_7

toto_7

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 295 posts
I need help to understand how to calculate the time complexity class O() for a function:

int Q (int[]values){

   int len = values.length;

   int res = values[0];

   for (int i =1; i<len; i++){

      if (values[i] > res) res = values[i];

    };

   return res;

}


#2
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
What do you, and don't you understand about measuring the complexity?

#3
toto_7

toto_7

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 295 posts
For this function above for example what is time complexity?

#4
Momerath

Momerath

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 243 posts
What do you think it is? What variables affect how long it runs? How do they affect the runtime?

#5
toto_7

toto_7

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 295 posts
O(N)?

#6
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
This appears to be a basis level question, are you taking a class of which teaches this? I would consult your instructor or learning material to the definitions of each of the asymptotic notations and theorize what fits the best one.

I will start you with the most efficient of the pair, constant time, which can be written as O(1).

  • An algorithm is said to be constant time if the value of T(n) is bounded by a value that does not depend on the size of the input.
  • An algorithm is said to be linear time if the value of T(n) is bounded by a value that increases linearly with the size of the input. O(n).
Now these can be said for the algorithm, what about the other steps of function Q? Does that increase the complexity of the function, from just the algorithm alone?
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#7
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
The first thing I would do: pass in a few different sized arrays, and note how many statements are executed, based on each size. Which statements are responsible for the difference in the number of statements executed?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#8
toto_7

toto_7

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 295 posts
I think 'for' and 'if' statments are responsible

#9
Momerath

Momerath

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 243 posts
Does increasing the size of the array make the if statement take longer to run? Remember to initially look at each line independent of the others.

#10
toto_7

toto_7

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 295 posts
I think got the meaning...regardles the array size, if function always executed one time, then is O(1), if time increased as array increased then is O(N) or O(N^2) and so on...




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users