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 4 of 4

Thread: big O

  1. #1
    Member
    Join Date
    Oct 2012
    Posts
    133
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default big O

    need help with big o notion. wht i understant you can tell how fast algorithm is running by using big O.

    so my question is can any one tell me if i have the anwser right or wrong. if right or wrong can you plz explain as much as you can.

    1) this algorithm is running O(n) or n?
    for(int i = 0; i < n; i++)
    { ... }

    2) O(10)?
    for(int i = 0; i < 10; i++){ ... }

    3) o(n)??
    int n = array[i].length;
    for(int i = 0; i < n; i++)
    if(array[i] == 3)
    {
    ...
    }

    4)
    for(int i = 10; i > 0; i--)
    {
    ...
    }


    5) O(n*m)? n = 1st array size | m = 2nd array size
    for(int i = 0; i < n; i++)
    {
    for(int x = 0; x < m; x++)
    {
     
    }
    }

    6)O(n*m)?? n=array.length | m=array[y].length
    for(int y = 0; y < array.length; y++)
    		{
    			for(int x = 0; x < array[y].length; x++)
    			{
    ....
    			}
    		}
    are there any more usefull case?


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: big O

    Two things about big-O notation to keep in mind:

    1. Any constant coefficients are removed. So O(10000000) -> O(1)

    2. You're only interested in the largest term as n approaches infinity, so O(n*log(n) + n + 1) -> O(n*log(n))

    Following these rules,

    1. Yep
    2. Nope, see rule 1
    3. Yep
    4. See rule 1
    5. Yep
    6. Yep

    There are too many algorithms with various performance bounds to reasonably list. A lot of algorithms have different worse/average/best big-O performance bounds, and some have no known/proven bounds. There are also some algorithms which have a given theoretical bound, but for all practical data sets the performance is either much worse or much better.

    Check out merge sort for the divide and conquer style algorithms

    Check out quick sort or insertion for an algorithm with various performance bounds dependent on input data

  3. #3
    Member
    Join Date
    Oct 2012
    Posts
    133
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default Re: big O

    thanks this was so much helpful


    let say if some on interview say "what is the runtime of this loop
    loop[i=0 i<n i++]

    right answer would be "big o of n" or just "n"?


    also can some one give me quicky example of big-omega. let say loop[i=0 i<n i++]
    or
    big-otheta?

    are they kind of same as big-o?

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: big O

    Written it's O(n). Either way you'll probably be able to get the idea across, which is the most important part. You could also call it linear runtime.

    big-omega and big-theta are similar mathematical concepts, but have different purposes. Big-omega describes the lower bound and big-theta describes both the upper and lower bounds.

    In the case of the simple loop, omega(n) and theta(n) perfectly bound the runtime performance.

    In gauging performance/runtime speed or memory usage we're concerned with the upper bound so big-O is ideal. Honestly I can't think of a single common example where either of these has been particularly interesting over big-O.

    See Wikipedia for a more thorough explanation.

Similar Threads

  1. Replies: 3
    Last Post: January 25th, 2013, 06:53 AM
  2. Big O
    By shanem in forum Java Theory & Questions
    Replies: 6
    Last Post: January 5th, 2013, 06:35 PM
  3. Big Integer help
    By ZekeQR in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 18th, 2012, 10:03 AM
  4. A Big Hello to everybody ot there..
    By Rituparna in forum Member Introductions
    Replies: 1
    Last Post: February 24th, 2011, 06:25 AM
  5. Big-O Question
    By Kumarrrr in forum Algorithms & Recursion
    Replies: 2
    Last Post: March 3rd, 2010, 07:46 AM