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.

# Thread: big O

1. ## 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?  Reply With Quote

3. ## 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  Reply With Quote

4. ## 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?  Reply With Quote

5. ## 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.  Reply With Quote