Last week we have seen how different algorithms which do the same thing in the end, can differ a lot by amount of steps it takes to run. We measure it in steps and not in seconds, or something else, since time really varies depending on hardware, background applications consuming resources, etc. When speaking about optimization, we mainly care about the leading factor of the equation(for example: if it takes n^2 +7n+15 iterations to run the algorithm, where n is, say, length of a list, then we say it is of the order n^2). We care only about the highest power, because as n gets bigger, it is mostly affected by the biggest power. In the previous example, if n = 100, n^2+7n+15 = 10000+700+15. Already we can see that 7n and especially a constant 15, are not as important as n^2. And a list of length 100 is a rather short one. Imagine how big the difference between n^2 and 7n is going to be when we get to millions of entries? That is the main reason why we neglect other parts of the equation but the highest degree term. In addition to that, if the highest degree is for example: 6n^3, we still are going to ignore 6 in front of n, for the same reasons.
Here is a list of the shortest time-run to the longest one in ascending order:
1) constants
2)log
3)square roots
4)n
5)n*logn
6)n^2
7)n^3
8)2^n
9)n^n
Obviously when choosing an algorithm from two which do the job, you are going to pick the "faster" one.
Big O notation is just a way to write the order of the run-time. So order of the first example is
n^2 which can be written as: O(n^2)(that is not the exact symbol for big O, there is a little tiny disconnection in the correct one, but you get the idea.)
No comments:
Post a Comment