Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Big O - Algorithm Analysis

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    16
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Big O - Algorithm Analysis

    How do I analyze a for loop that looks like

    for (j = 0; j < 2*n; j += 2)

    I know that if it was just j++ it would loop 2n times but the += is throwing me off. This loop is nesting in a normal loop of

    for(i=0; i < n; i++)

    I know this will make it n^2 but I don't know how the j += 2 affects the time.


    Thanks for the help,
    PeskyToaster


  2. #2
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: Big O - Algorithm Analysis

    j += 2
    just adds 2 to j, it is the same as
     j = j + 2
    , and because j is incrementing twice every loop, it is going to reach it's end twice as fast, so it is going to be looping
    1/2 * 2n
    or
    n
    times.

  3. The Following User Says Thank You to Tjstretch For This Useful Post:

    PeskyToaster (March 15th, 2012)

  4. #3
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Big O - Algorithm Analysis

    That loop is the same as for (int index = 0; index < N; index++) in terms on time complexity.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  5. #4
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Big O - Algorithm Analysis

    And, does the factor of 2x or 0.5x or whatever was worrying you, really matter?

  6. #5
    Junior Member
    Join Date
    Nov 2011
    Posts
    16
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Big O - Algorithm Analysis

    I have another problem. First here's the code snippet. It's just general code for analysis

     
    for (j=0; j<2n; j++) {
         i = 1;
        Generic Statement;
        while (j<n) {
              Generic statement;
              Generic statement;
              i = i *2;
       }
    }

    How does the i = 2n and the j < 2n affect the Big O and execution time. The execution time is just the equation with each statement being one time unit.

Similar Threads

  1. Reading Big File analysis
    By tcstcs in forum Java Theory & Questions
    Replies: 1
    Last Post: March 9th, 2012, 07:35 AM
  2. Sales Analysis for various products
    By faridzul90 in forum Java Theory & Questions
    Replies: 3
    Last Post: September 22nd, 2011, 08:36 AM
  3. [SOLVED] Algorithm Help
    By aussiemcgr in forum Java Theory & Questions
    Replies: 2
    Last Post: September 10th, 2010, 04:12 PM
  4. An online source offers project analysis
    By talha06 in forum The Cafe
    Replies: 0
    Last Post: June 20th, 2010, 12:49 PM
  5. algorithm
    By AmmrO in forum Algorithms & Recursion
    Replies: 13
    Last Post: September 24th, 2009, 09:18 PM