Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what does O(N) mean [duplicate]

Possible Duplicate:
What is Big O notation? Do you use it?

Hi all,

fairly basic scalability notation question.

I recently recieved a comment on a post that my python ordered-list implimentation "but beware that your 'ordered set' implementation is O(N) for insertions"

Which is great to know, but I'm not sure what this means.

I've seen notation such as n(o) o(N), N(o-1) or N(o*o)

what does the above notation refer to?

like image 297
Fire Crow Avatar asked Dec 15 '09 18:12

Fire Crow


People also ask

Is O 1 and O n the same?

In short, O(1) means that it takes a constant time, like 14 nanoseconds, or three minutes no matter the amount of data in the set. O(n) means it takes an amount of time linear with the size of the set, so a set twice the size will take twice the time. You probably don't want to put a million objects into one of these.

What is O n in data structure?

O(n) Called “O of n” or Linear Time. As more items are added to the array in an unsorted fashion, it takes a corresponding linear amount of time to perform a search. e.g. Insertion & Deletion in an array.

What is better O n or O Logn?

O(n) means that the algorithm's maximum running time is proportional to the input size. basically, O(something) is an upper bound on the algorithm's number of instructions (atomic ones). therefore, O(logn) is tighter than O(n) and is also better in terms of algorithms analysis.

What is O 2 n?

O(2n) denotes an algorithm whose growth doubles with each addition to the input data set. The growth curve of an O(2n) function is exponential - starting off very shallow, then rising meteorically.


3 Answers

O(n) is Big O Notation and refers to the complexity of a given algorithm. n refers to the size of the input, in your case it's the number of items in your list.

O(n) means that your algorithm will take on the order of n operations to insert an item. e.g. looping through the list once (or a constant number of times such as twice or only looping through half).

O(1) means it takes a constant time, that it is not dependent on how many items are in the list.

O(n^2) means that for every insert, it takes n*n operations. i.e. 1 operation for 1 item, 4 operations for 2 items, 9 operations for 3 items. As you can see, O(n^2) algorithms become inefficient for handling large number of items.

For lists O(n) is not bad for insertion, but not the quickest. Also note that O(n/2) is considered as being the same as O(n) because they both grow at the same rate with n.

like image 25
Colin Gislason Avatar answered Oct 08 '22 03:10

Colin Gislason


The comment was referring to the Big-O Notation.

Briefly:

  1. O(1) means in constant time - independent of the number of items.
  2. O(N) means in proportion to the number of items.
  3. O(log N) means a time proportional to log(N)

Basically any 'O' notation means an operation will take time up to a maximum of k*f(N)
where:

k is a constant multiplier

f() is a function that depends on N

like image 198
quamrana Avatar answered Oct 08 '22 03:10

quamrana


It's called Big O Notation: http://en.wikipedia.org/wiki/Big_O_notation

So saying that insertion is O(n) means that you have to walk through the whole list (or half of it -- big O notation ignores constant factors) to perform the insertion.

This looks like a nice introduction: http://rob-bell.net/2009/06/a-beginners-guide-to-big-o-notation/

like image 28
dreeves Avatar answered Oct 08 '22 03:10

dreeves