# big O

• March 2nd, 2013, 07:37 PM
hwoarang69
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?
Code :

```for(int i = 0; i < n; i++) { ... }```

2) O(10)?
Code :

`for(int i = 0; i < 10; i++){ ... }`

3) o(n)??
Code :

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

4)
Code :

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

5) O(n*m)? n = 1st array size | m = 2nd array size
Code :

```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
Code :

```for(int y = 0; y < array.length; y++) { for(int x = 0; x < array[y].length; x++) { .... } }```
are there any more usefull case?
• March 2nd, 2013, 10:03 PM
helloworld922
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
• March 3rd, 2013, 12:29 AM
hwoarang69
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?
• March 3rd, 2013, 01:51 PM
helloworld922
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.