Question

    

Cette question a déjà une réponse ici:

         

Je demande plus sur ce que cela signifie pour mon code. Je comprends mathématiquement les concepts, j'ai juste du mal à comprendre ce qu’ils signifient sur le plan conceptuel. Par exemple, si vous effectuez une opération O (1) sur une structure de données, je comprends que le nombre d'opérations à effectuer ne va pas augmenter car il y a plus d'éléments. Et une opération O (n) signifierait que vous effectueriez un ensemble d'opérations sur chaque élément. Quelqu'un pourrait-il remplir les blancs ici?

  • Comme que ferait exactement une opération O (n ^ 2)?
  • Et qu'est-ce que ça veut dire si une opération est O (n log (n))?
  • Et est-ce que quelqu'un doit fumer du crack pour écrire un O (x!)?
Était-ce utile?

La solution

Une façon de penser à cela est la suivante:

O (N ^ 2) signifie que pour chaque élément, vous faites quelque chose avec tous les autres éléments, par exemple en les comparant. Le tri à bulles en est un exemple.

O (N log N) signifie que pour chaque élément, vous faites quelque chose qui n'a besoin que d'examiner le log N des éléments. C'est généralement parce que vous connaissez les éléments qui vous permettent de faire un choix efficace. Les tris les plus efficaces en sont un exemple, comme le tri par fusion.

O (N!) signifie faire quelque chose pour toutes les permutations possibles des N éléments. Voyageur est un exemple de cela, où il y a N! manières de visiter les nœuds, et la solution par force brute consiste à examiner le coût total de toutes les permutations possibles afin de trouver la solution optimale.

Autres conseils

La grosse chose que la notation Big-O signifie pour votre code est la façon dont il va évoluer lorsque vous doublez la quantité de & "; choses &"; ça fonctionne. Voici un exemple concret:

Big-O       |  computations for 10 things |  computations for 100 things
----------------------------------------------------------------------
O(1)        |   1                         |     1
O(log(n))   |   3                         |     7
O(n)        |  10                         |   100
O(n log(n)) |  30                         |   700
O(n^2)      | 100                         | 10000

Donc, prenez quicksort qui est O (n log (n)) vs type de bulle qui est O (n ^ 2). Lors du tri de 10 choses, le tri rapide est 3 fois plus rapide que le tri à bulles. Mais pour trier 100 choses, c'est 14 fois plus vite! Il est donc important de choisir l’algorithme le plus rapide. Lorsque vous parvenez à des bases de données contenant plusieurs millions de lignes, cela peut faire toute la différence entre l'exécution de votre requête en 0,2 seconde et celle de plusieurs heures.

Une autre chose à considérer est qu'un mauvais algorithme est une chose que la loi de Moore ne peut pas aider. Par exemple, si vous avez un calcul scientifique qui est O (n ^ 3) et qu'il peut calculer 100 choses par jour, doubler la vitesse du processeur ne vous rapporte que 125 choses par jour. Cependant, mettez ce calcul à O (n ^ 2) et vous ferez 1000 choses par jour.

clarification: En fait, Big-O ne dit rien sur les performances comparatives de différents algorithmes au même point de taille, mais plutôt sur les performances comparatives du même algorithme à des points de taille différents:

                 computations     computations       computations
Big-O       |   for 10 things |  for 100 things |  for 1000 things
----------------------------------------------------------------------
O(1)        |        1        |        1        |         1
O(log(n))   |        1        |        3        |         7
O(n)        |        1        |       10        |       100
O(n log(n)) |        1        |       33        |       664
O(n^2)      |        1        |      100        |     10000

Vous pouvez trouver utile de le visualiser:

Analyse Big O

De plus, sur LogY / LogX , les fonctions n 1/2 , n, n 2 all ressembler à lignes droites , tandis que l'échelle LogY / X 2 n , e n , 10 n sont des lignes droites et n! est linearithmic (ressemble à n log n ).

C'est peut-être trop mathématique, mais voici mon essai. (Je suis un mathématicien.)

Si quelque chose est O ( f ( n )), son temps d'exécution sur les n éléments sera égal à A f ( n ) + B (mesuré par exemple en cycles d'horloge ou en opérations de la CPU). Il est essentiel de comprendre que vous avez également ces constantes A et B , qui proviennent de la mise en œuvre spécifique. B représente essentiellement la & "surcharge constante &"; de votre opération, par exemple certains prétraitements que vous faites ne dépendent pas de la taille de la collection. A représente la vitesse de votre algorithme de traitement d'élément actuel.

La clé, cependant, est que vous utilisez la grande notation O pour déterminer à quel point quelque chose va évoluer . Ces constantes n’auront donc pas vraiment d’importance: si vous essayez de comprendre comment passer de 10 à 10000 articles, qui se soucie du surcoût constant B ? De même, d’autres préoccupations (voir ci-dessous) l'emporteront certainement sur le poids de la constante multiplicative A .

Donc, la vraie affaire est f ( n ). Si f n'augmente pas du tout avec n , par exemple. f ( n ) = 1, alors vous ferez des économies fantastiques --- votre temps d'exécution sera toujours juste A + B . Si f croît linéairement avec n , c'est-à-dire f ( n ) = n , votre temps d'exécution sera pratiquement identique à celui attendu - si vos utilisateurs attendent 10 éléments pendant 10 ns, ils attendront 10 000 éléments pour 10 000 éléments (en ignorant la constante additive). Mais si la croissance est plus rapide, comme n 2 , vous avez des problèmes. les choses commenceront à ralentir beaucoup trop quand vous aurez de plus grandes collections. Le journal f ( n ) = n ( n ) est un bon compromis. Généralement, votre opération ne peut pas soyez si simple que de donner une mise à l’échelle linéaire, mais vous avez réussi à réduire les choses de telle sorte que l’échelle soit bien meilleure que f ( n ) = n 2 .

Dans la pratique, voici quelques bons exemples:

  • O (1): récupérer un élément d'un tableau. Nous savons exactement où il se trouve dans la mémoire, alors nous allons simplement le chercher. Peu importe si la collection contient 10 articles ou 10 000; il est toujours à l'index (par exemple) 3, donc nous sautons simplement à l'emplacement 3 en mémoire.
  • O ( n ): récupération d'un élément dans une liste liée. Ici, A = 0,5, car il vous faudra en moyenne parcourir la moitié de la liste des liens pour trouver l'élément que vous recherchez.
  • O ( n 2 ): divers & "dumb &"; algorithmes de tri. Parce que généralement leur stratégie implique, pour chaque élément ( n ), vous regardez tous les autres éléments (donc un autre n , donnant ainsi à n < sup> 2 ), puis placez-vous au bon endroit.
  • O ( n journal ( n )): divers & "smart &"; algorithmes de tri. Il s'avère que vous n'avez qu'à regarder, par exemple, 10 éléments d'une collection de 10 10 éléments pour vous trier intelligemment par rapport à tous les membres de la collection. Parce que tout le monde va aussi examiner 10 éléments et que le comportement qui en résulte est parfaitement orchestré, ce qui suffit pour créer une liste triée.
  • O ( n !): un algorithme qui & "essaye tout, &"; car il y a (proportionnel à) n ! combinaisons possibles d'éléments n susceptibles de résoudre un problème donné. Donc, il passe en revue toutes ces combinaisons, les essaie, puis s’arrête à chaque fois.

don.neufeld's answer is very good, but I'd probably explain it in two parts: first, there's a rough hierarchy of O()'s that most algorithms fall into. Then, you can look at each of those to come up with sketches of what typical algorithms of that time complexity do.

For practical purposes, the only O()'s that ever seem to matter are:

  • O(1) "constant time" - the time required is independent of the size of the input. As a rough category, I would include algorithms such as hash lookups and Union-Find here, even though neither of those are actually O(1).
  • O(log(n)) "logarithmic" - it gets slower as you get larger inputs, but once your input gets fairly large, it won't change enough to worry about. If your runtime is ok with reasonably-sized data, you can swamp it with as much additional data as you want and it'll still be ok.
  • O(n) "linear" - the more input, the longer it takes, in an even tradeoff. Three times the input size will take roughly three times as long.
  • O(n log(n)) "better than quadratic" - increasing the input size hurts, but it's still manageable. The algorithm is probably decent, it's just that the underlying problem is more difficult (decisions are less localized with respect to the input data) than those problems that can be solved in linear time. If your input sizes are getting up there, don't assume that you could necessarily handle twice the size without changing your architecture around (eg by moving things to overnight batch computations, or not doing things per-frame). It's ok if the input size increases a little bit, though; just watch out for multiples.
  • O(n^2) "quadratic" - it's really only going to work up to a certain size of your input, so pay attention to how big it could get. Also, your algorithm may suck -- think hard to see if there's an O(n log(n)) algorithm that would give you what you need. Once you're here, feel very grateful for the amazing hardware we've been gifted with. Not long ago, what you are trying to do would have been impossible for all practical purposes.
  • O(n^3) "cubic" - not qualitatively all that different from O(n^2). The same comments apply, only more so. There's a decent chance that a more clever algorithm could shave this time down to something smaller, eg O(n^2 log(n)) or O(n^2.8...), but then again, there's a good chance that it won't be worth the trouble. (You're already limited in your practical input size, so the constant factors that may be required for the more clever algorithms will probably swamp their advantages for practical cases. Also, thinking is slow; letting the computer chew on it may save you time overall.)
  • O(2^n) "exponential" - the problem is either fundamentally computationally hard or you're being an idiot. These problems have a recognizable flavor to them. Your input sizes are capped at a fairly specific hard limit. You'll know quickly whether you fit into that limit.

And that's it. There are many other possibilities that fit between these (or are greater than O(2^n)), but they don't often happen in practice and they're not qualitatively much different from one of these. Cubic algorithms are already a bit of a stretch; I only included them because I've run into them often enough to be worth mentioning (eg matrix multiplication).

What's actually happening for these classes of algorithms? Well, I think you had a good start, although there are many examples that wouldn't fit these characterizations. But for the above, I'd say it usually goes something like:

  • O(1) - you're only looking at most at a fixed-size chunk of your input data, and possibly none of it. Example: the maximum of a sorted list.
    • Or your input size is bounded. Example: addition of two numbers. (Note that addition of N numbers is linear time.)
  • O(log n) - each element of your input tells you enough to ignore a large fraction of the rest of the input. Example: when you look at an array element in binary search, its value tells you that you can ignore "half" of your array without looking at any of it. Or similarly, the element you look at gives you enough of a summary of a fraction of the remaining input that you won't need to look at it.
    • There's nothing special about halves, though -- if you can only ignore 10% of your input at each step, it's still logarithmic.
  • O(n) - you do some fixed amount of work per input element. (But see below.)
  • O(n log(n)) - there are a few variants.
    • You can divide the input into two piles (in no more than linear time), solve the problem independently on each pile, and then combine the two piles to form the final solution. The independence of the two piles is key. Example: classic recursive mergesort.
    • Each linear-time pass over the data gets you halfway to your solution. Example: quicksort if you think in terms of the maximum distance of each element to its final sorted position at each partitioning step (and yes, I know that it's actually O(n^2) because of degenerate pivot choices. But practically speaking, it falls into my O(n log(n)) category.)
  • O(n^2) - you have to look at every pair of input elements.
    • Or you don't, but you think you do, and you're using the wrong algorithm.
  • O(n^3) - um... I don't have a snappy characterization of these. It's probably one of:
    • You're multiplying matrices
    • You're looking at every pair of inputs but the operation you do requires looking at all of the inputs again
    • the entire graph structure of your input is relevant
  • O(2^n) - you need to consider every possible subset of your inputs.

None of these are rigorous. Especially not linear time algorithms (O(n)): I could come up with a number of examples where you have to look at all of the inputs, then half of them, then half of those, etc. Or the other way around -- you fold together pairs of inputs, then recurse on the output. These don't fit the description above, since you're not looking at each input once, but it still comes out in linear time. Still, 99.2% of the time, linear time means looking at each input once.

A lot of these are easy to demonstrate with something non-programming, like shuffling cards.

Sorting a deck of cards by going through the whole deck to find the ace of spades, then going through the whole deck to find the 2 of spades, and so on would be worst case n^2, if the deck was already sorted backwards. You looked at all 52 cards 52 times.

In general the really bad algorithms aren't necessarily intentional, they're commonly a misuse of something else, like calling a method that is linear inside some other method that repeats over the same set linearly.

Ok - there are some very good answers here but almost all of them seem to make the same mistake and it's one that is pervading common usage.

Informally, we write that f(n) = O( g(n) ) if, up to a scaling factor and for all n larger than some n0, g(n) is larger than f(n). That is, f(n) grows no quicker than, or is bounded from above by, g(n). This tells us nothing about how fast f(n) grows, save for the fact that it is guaranteed not to be any worse than g(n).

A concrete example: n = O( 2^n ). We all know that n grows much less quickly than 2^n, so that entitles us to say that it is bounded by above by the exponential function. There is a lot of room between n and 2^n, so it's not a very tight bound, but it's still a legitimate bound.

Why do we (computer scientists) use bounds rather than being exact? Because a) bounds are often easier to prove and b) it gives us a short-hand to express properties of algorithms. If I say that my new algorithm is O(n.log n) that means that in the worst case its run-time will be bounded from above by n.log n on n inputs, for large enough n (although see my comments below on when I might not mean worst-case).

If instead, we want to say that a function grows exactly as quickly as some other function, we use theta to make that point (I'll write T( f(n) ) to mean \Theta of f(n) in markdown). T( g(n) ) is short hand for being bounded from above and below by g(n), again, up to a scaling factor and asymptotically.

That is f(n) = T( g(n) ) <=> f(n) = O(g(n)) and g(n) = O(f(n)). In our example, we can see that n != T( 2^n ) because 2^n != O(n).

Why get concerned about this? Because in your question you write 'would someone have to smoke crack to write an O(x!)?' The answer is no - because basically everything you write will be bounded from above by the factorial function. The run time of quicksort is O(n!) - it's just not a tight bound.

There's also another dimension of subtlety here. Typically we are talking about the worst case input when we use O( g(n) ) notation, so that we are making a compound statement: in the worst case running time it will not be any worse than an algorithm that takes g(n) steps, again modulo scaling and for large enough n. But sometimes we want to talk about the running time of the average and even best cases.

Vanilla quicksort is, as ever, a good example. It's T( n^2 ) in the worst case (it will actually take at least n^2 steps, but not significantly more), but T(n.log n) in the average case, which is to say the expected number of steps is proportional to n.log n. In the best case it is also T(n.log n) - but you could improve that for, by example, checking if the array was already sorted in which case the best case running time would be T( n ).

How does this relate to your question about the practical realisations of these bounds? Well, unfortunately, O( ) notation hides constants which real-world implementations have to deal with. So although we can say that, for example, for a T(n^2) operation we have to visit every possible pair of elements, we don't know how many times we have to visit them (except that it's not a function of n). So we could have to visit every pair 10 times, or 10^10 times, and the T(n^2) statement makes no distinction. Lower order functions are also hidden - we could have to visit every pair of elements once, and every individual element 100 times, because n^2 + 100n = T(n^2). The idea behind O( ) notation is that for large enough n, this doesn't matter at all because n^2 gets so much larger than 100n that we don't even notice the impact of 100n on the running time. However, we often deal with 'sufficiently small' n such that constant factors and so on make a real, significant difference.

For example, quicksort (average cost T(n.log n)) and heapsort (average cost T(n.log n)) are both sorting algorithms with the same average cost - yet quicksort is typically much faster than heapsort. This is because heapsort does a few more comparisons per element than quicksort.

This is not to say that O( ) notation is useless, just imprecise. It's quite a blunt tool to wield for small n.

(As a final note to this treatise, remember that O( ) notation just describes the growth of any function - it doesn't necessarily have to be time, it could be memory, messages exchanged in a distributed system or number of CPUs required for a parallel algorithm.)

I try to explain by giving simple code examples in C#.

For List<int> numbers = new List<int> {1,2,3,4,5,6,7,12,543,7};

O(1) looks like

return numbers.First();

O(n) looks like

int result = 0;
foreach (int num in numbers)
{
    result += num;
}
return result;

O(n log(n)) looks like

int result = 0;
foreach (int num in numbers)
{
    int index = numbers.length - 1;
    while (index > 1)
    {
        // yeah, stupid, but couldn't come up with something more useful :-(
        result += numbers[index];
        index /= 2;
    }
}
return result;

O(n^2) looks like

int result = 0;
foreach (int outerNum in numbers)
{
    foreach (int innerNum in numbers)
    {
        result += outerNum * innerNum;
    }
}
return result;

O(n!) looks like, uhm, to tired to come up with anything simple.
But I hope you get the general point?

The way I describe it to my nontechnical friends is like this:

Consider multi-digit addition. Good old-fashioned, pencil-and-paper addition. The kind you learned when you were 7-8 years old. Given two three-or-four-digit numbers, you can find out what they add up to fairly easily.

If I gave you two 100-digit numbers, and asked you what they add up to, figuring it out would be pretty straightforward, even if you had to use pencil-and-paper. A bright kid could do such an addition in just a few minutes. This would only require about 100 operations.

Now, consider multi-digit multiplication. You probably learned that at around 8 or 9 years old. You (hopefully) did lots of repetitive drills to learn the mechanics behind it.

Now, imagine I gave you those same two 100-digit numbers and told you to multiply them together. This would be a much, much harder task, something that would take you hours to do - and that you'd be unlikely to do without mistakes. The reason for this is that (this version of) multiplication is O(n^2); each digit in the bottom number has to be multiplied by each digit in the top number, leaving a total of about n^2 operations. In the case of the 100-digit numbers, that's 10,000 multiplications.

No, an O(n) algorithm does not mean it will perform an operation on each element. Big-O notation gives you a way to talk about the "speed" of you algorithm independent of your actual machine.

O(n) means that the time your algorithm will take grows linearly as your input increase. O(n^2) means that the time your algorithm takes grows as the square of your input. And so forth.

The way I think about it, is you have the task of cleaning up a problem caused by some evil villain V who picks N, and you have to estimate out how much longer it's going to take to finish your problem when he increases N.

O(1) -> increasing N really doesn't make any difference at all

O(log(N)) -> every time V doubles N, you have to spend an extra amount of time T to complete the task. V doubles N again, and you spend the same amount.

O(N) -> every time V doubles N, you spend twice as much time.

O(N^2) -> every time V doubles N, you spend 4x as much time. (it's not fair!!!)

O(N log(N)) -> every time V doubles N, you spend twice as much time plus a little more.

These are bounds of an algorithm; computer scientists want to describe how long it is going to take for large values of N. (which gets important when you are factoring numbers that are used in cryptography -- if the computers speed up by a factor of 10, how many more bits do you have to use to ensure it will still take them 100 years to break your encryption and not just 1 year?)

Some of the bounds can have weird expressions if it makes a difference to the people involved. I've seen stuff like O(N log(N) log(log(N))) somewhere in Knuth's Art of Computer Programming for some algorithms. (can't remember which one off the top of my head)

One thing that hasn't been touched on yet for some reason:

When you see algorithms with things like O(2^n) or O(n^3) or other nasty values it often means you're going to have to accept an imperfect answer to your problem in order to get acceptable performance.

Correct solutions that blow up like this are common when dealing with optimization problems. A nearly-correct answer delivered in a reasonable timeframe is better than a correct answer delivered long after the machine has decayed to dust.

Consider chess: I don't know exactly what the correct solution is considered to be but it's probably something like O(n^50) or even worse. It is theoretically impossible for any computer to actually calculate the correct answer--even if you use every particle in the universe as a computing element performing an operation in the minimum possible time for the life of the universe you still have a lot of zeros left. (Whether a quantum computer can solve it is another matter.)

The "Intuitition" behind Big-O

Imagine a "competition" between two functions over x, as x approaches infinity: f(x) and g(x).

Now, if from some point on (some x) one function always has a higher value then the other, then let's call this function "faster" than the other.

So, for example, if for every x > 100 you see that f(x) > g(x), then f(x) is "faster" than g(x).

In this case we would say g(x) = O(f(x)). f(x) poses a sort of "speed limit" of sorts for g(x), since eventually it passes it and leaves it behind for good.

This isn't exactly the definition of big-O notation, which also states that f(x) only has to be larger than C*g(x) for some constant C (which is just another way of saying that you can't help g(x) win the competition by multiplying it by a constant factor - f(x) will always win in the end). The formal definition also uses absolute values. But I hope I managed to make it intuitive.

  • And does somebody have to smoke crack to write an O(x!)?

No, just use Prolog. If you write a sorting algorithm in Prolog by just describing that each element should be bigger than the previous, and let backtracking do the sorting for you, that will be O(x!). Also known as "permutation sort".

I like don neufeld's answer, but I think I can add something about O(n log n).

An algorithm which uses a simple divide and conquer strategy is probably going to be O(log n). The simplest example of this is finding a something in an sorted list. You don't start at the beginning and scan for it. You go to the middle, you decide if you should then go backwards or forwards, jump halfway to the last place you looked, and repeat this until you find the item you're looking for.

If you look at the quicksort or mergesort algorithms, you will see that they both take the approach of dividing the list to be sorted in half, sorting each half (using the same algorithm, recursively), and then recombining the two halves. This sort of recursive divide and conquer strategy will be O(n log n).

If you think about it carefully, you'll see that quicksort does an O(n) partitioning algorithm on the whole n items, then an O(n) partitioning twice on n/2 items, then 4 times on n/4 items, etc... until you get to an n partitions on 1 item (which is degenerate). The number of times you divide n in half to get to 1 is approximately log n, and each step is O(n), so recursive divide and conquer is O(n log n). Mergesort builds the other way, starting with n recombinations of 1 item, and finishing with 1 recombination of n items, where the recombination of two sorted lists is O(n).

As for smoking crack to write an O(n!) algorithm, you are unless you have no choice. The traveling salesman problem given above is believed to be one such problem.

Most Jon Bentley books (e.g. Programming Pearls) cover such stuff in a really pragmatic manner. This talk given by him includes one such analysis of a quicksort.

While not entirely relevant to the question, Knuth came up with an interesting idea: teaching Big-O notation in high school calculus classes, though I find this idea quite eccentric.

Think of it as stacking lego blocks (n) vertically and jumping over them.

O(1) means at each step, you do nothing. The height stays the same.

O(n) means at each step, you stack c blocks, where c1 is a constant.

O(n^2) means at each step, you stack c2 x n blocks, where c2 is a constant, and n is the number of stacked blocks.

O(nlogn) means at each step, you stack c3 x n x log n blocks, where c3 is a constant, and n is the number of stacked blocks.

To understand O(n log n), remember that log n means log-base-2 of n. Then look at each part:

O(n) is, more or less, when you operate on each item in the set.

O(log n) is when the number of operations is the same as the exponent to which you raise 2, to get the number of items. A binary search, for instance, has to cut the set in half log n times.

O(n log n) is a combination – you're doing something along the lines of a binary search for each item in the set. Efficient sorts often operate by doing one loop per item, and in each loop doing a good search to find the right place to put the item or group in question. Hence n * log n.

Just to respond to the couple of comments on my above post:

Domenic - I'm on this site, and I care. Not for pedantry's sake, but because we - as programmers - typically care about precision. Using O( ) notation incorrectly in the style that some have done here renders it kind of meaningless; we may just as well say something takes n^2 units of time as O( n^2 ) under the conventions used here. Using the O( ) adds nothing. It's not just a small discrepancy between common usage and mathematical precision that I'm talking about, it's the difference between it being meaningful and it not.

I know many, many excellent programmers who use these terms precisely. Saying 'oh, we're programmers therefore we don't care' cheapens the whole enterprise.

onebyone - Well, not really although I take your point. It's not O(1) for arbitrarily large n, which is kind of the definition of O( ). It just goes to show that O( ) has limited applicability for bounded n, where we would rather actually talk about the number of steps taken rather than a bound on that number.

Tell your eight year old log(n) means the number of times you have to chop a length n log in two for it to get down to size n=1 :p

O(n log n) is usually sorting O(n^2) is usually comparing all pairs of elements

Suppose you had a computer that could solve a problem of a certain size. Now imagine that we can double the performance a few times. How much bigger a problem can we solve with each doubling?

If we can solve a problem of double the size, that's O(n).

If we have some multiplier that isn't one, that's some sort of polynomial complexity. For example, if each doubling allows us to increase the problem size by about 40%, it's O(n^2), and about 30% would be O(n^3).

If we just add to the problem size, it's exponential or worse. For example, if each doubling means we can solve a problem 1 bigger, it's O(2^n). (This is why brute-forcing a cipher key becomes effectively impossible with reasonably sized keys: a 128-bit key requires about 16 quintillion times as much processing as a 64-bit.)

Remember the fable of the tortoise and the hare (turtle and rabbit)?

Over the long run, the tortoise wins, but over the short run the hare wins.

That's like O(logN) (tortoise) vs. O(N) (hare).

If two methods differ in their big-O, then there is a level of N at which one of them will win, but big-O says nothing about how big that N is.

To remain sincere to the question asked I would answer the question in the manner I would answer an 8 year old kid

Suppose an ice-cream seller prepares a number of ice creams ( say N ) of different shapes arranged in an orderly fashion. You want to eat the ice cream lying in the middle

Case 1 : - You can eat an ice cream only if you have eaten all the ice creams smaller than it You will have to eat half of all the ice creams prepared (input).Answer directly depends on the size of the input Solution will be of order o(N)

Case 2 :- You can directly eat the ice cream in the middle

Solution will be O(1)

Case 3 : You can eat an ice cream only if you have eaten all the ice creams smaller than it and each time you eat an ice cream you allow another kid (new kid everytime ) to eat all his ice creams Total time taken would be N + N + N.......(N/2) times Solution will be O(N2)

log(n) means logarithmic growth. An example would be divide and conquer algorithms. If you have 1000 sorted numbers in an array ( ex. 3, 10, 34, 244, 1203 ... ) and want to search for a number in the list (find its position), you could start with checking the value of the number at index 500. If it is lower than what you seek, jump to 750. If it is higher than what you seek, jump to 250. Then you repeat the process until you find your value (and key). Every time we jump half the search space, we can cull away testing many other values since we know the number 3004 can't be above number 5000 (remember, it is a sorted list).

n log(n) then means n * log(n).

I'll try to actually write an explanation for a real eight year old boy, aside from technical terms and mathematical notions.

Like what exactly would an O(n^2) operation do?

If you are in a party, and there are n people in the party including you. How many handshakes it take so that everyone has handshaked everyone else, given that people would probably forget who they handshaked at some point.

Note: this approximate to a simplex yielding n(n-1) which is close enough to n^2.

And what the heck does it mean if an operation is O(n log(n))?

Your favorite team has won, they are standing in line, and there are n players in the team. How many hanshakes it would take you to handshake every player, given that you will hanshake each one multiple times, how many times, how many digits are in the number of the players n.

Note: this will yield n * log n to the base 10.

And does somebody have to smoke crack to write an O(x!)?

You are a rich kid and in your wardrobe there are alot of cloths, there are x drawers for each type of clothing, the drawers are next to each others, the first drawer has 1 item, each drawer has as many cloths as in the drawer to its left and one more, so you have something like 1 hat, 2 wigs, .. (x-1) pants, then x shirts. Now in how many ways can you dress up using a single item from each drawer.

Note: this example represent how many leaves in a decision-tree where number of children = depth, which is done through 1 * 2 * 3 * .. * x

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top