Results 1 to 6 of 6
  1. #1
    hawaiifiver is offline Member
    Join Date
    Jan 2009
    Posts
    21
    Rep Power
    0

    Default Time complexity - foor loop

    Hello to all.

    I am trying to figure out the time complexity for the following piece of code.

    Java Code:
    for (int i = 1; i < n; i +=10)
    {
               
            for (int j = n; j > 0; j --)
            {
    
                   product *= (i*j);
             }
    
    }
    Should I be thinking here about the fact that there are two foor loops? The inner loop executes n times for each run of the outer loop. I think this might have something to do with quadratic time. Or should i be looking at statements like product *= (i*j); Any tips?


    Thanks again!

  2. #2
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default ah.... huh?

    Sounds impressive, but I have no clue what you want.
    The inner loop executes n times for each run of the outer loop.
    The above is a correct statement.

    • Are you looking for the mathamatical interpretation for the above?
    • Do you want to know which is more efficient: the above or product *= (i*j)?
    • Do you want to use these in a program or are asking for some other reason?

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  3. #3
    hawaiifiver is offline Member
    Join Date
    Jan 2009
    Posts
    21
    Rep Power
    0

    Default

    Hi CJSL,

    Yes I am trying to figure out the time complexity of the code.
    I'm not using it in a program or looking for a mathematical interpretation.

    I am trying to write the time complexity in big Oh notation.

    If n = 50. Then the number of multiplications in the inner loop will be 50. In my notes it says the time taken to complete what the code is doing is approximately proportional to n.

    Would it be reasonable to assume that the time complexity is of order n, i.e O(n)

  4. #4
    ramya victor is offline Member
    Join Date
    Feb 2011
    Posts
    2
    Rep Power
    0

    Default time complexity

    is the time complexity of this code is 0(n2). the outer for loop is executed n times and inner for loop is executed and hence the inner code is executed n*n= n2 times.

  5. #5
    ramya victor is offline Member
    Join Date
    Feb 2011
    Posts
    2
    Rep Power
    0

    Default

    can u tell wheteher i am correct? my sir has thought us like dat. plzzz tell me if am correct

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Replies: 2
    Last Post: 11-04-2008, 02:48 AM
  2. Time
    By Fireking in forum New To Java
    Replies: 3
    Last Post: 09-07-2008, 01:30 AM
  3. Replies: 21
    Last Post: 08-01-2008, 11:13 AM
  4. Singleton considered stupid, Java and complexity
    By fishtoprecords in forum Forum Lobby
    Replies: 11
    Last Post: 07-06-2008, 03:38 AM
  5. Hello, first time here.
    By ludragon in forum Introductions
    Replies: 2
    Last Post: 01-03-2008, 05:03 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •