There are many ways to solve a problem, which is why algorithms matter, but how do we choose which one to use? Maybe a flash game needs to do some collision detection, or perhaps a flex app holds a large set of data and needs eliminate outliers. Before we start looking at problem solving strategies, we need to learn some terminology, so we will begin with the big one, Big O notation.
Big O notation allows us to describe an algorithm using a limiting factor. That means we take a look at a single aspect of an algorithm, for example speed, and try and determine what type of calculation has the most impact on that aspect. Then from this factor write a simple equation that the algorithm is no worse than. Typically this is in relation to the size of the input, so Big O notation is typically show in relation to the number of elements (n). Since Big O notation is all theoretical (it never uses real numbers, just fake math) it is assumed that only really big numbers of n matter.
For example, if there is a better algorithm for 20 elements, it’s assumed that the difference is insignificant enough that it doesn’t matter. So, if an algorithm a is 5% better than algorithm b with 20+ items, but 50% worse for less than 20, it would take 220 items for the two to be equal. Then for every item over 220 algorithm a is better. Since we assume big numbers, a is “always” better than b.
In mathematical terms, this means we only have to deal with the order of an algorithm. What does this mean? If an algorithm’s speed is n^2+20*n+45, then the n^2 term is the limiting factor. While term 20*n matters a little when n is small, when n is 200, n^2 is 40000 while 20*n is 4000, less than 10% of the end result. Again, we’re dealing with big numbers, so the higher the n, the less the smaller order terms matter. Big O notation simplifies the math for us by ignoring other terms completely. So the Equation is O(n^2) in Big O. Even better, Big O ignores coefficients, so 20*n^2 and n^2 are considered the same, O(n^2). This is because the coefficients are typically related to the system the algorithm is running on. The same algorithm could have many different coefficients thanks to variations in systems and implementations, but it is no worse than O(n^2).
Let’s look at sorting, where there are many algorithms so we can get a better idea of how to compare Big O notation. Insertion Sort is an O(n^2) algorithm while Merge Sort is a O(n log n) algorithm. Since n log n is smaller than n^2 for large numbers, Merge Sort is always faster than Insertion Sort. If you don’t want to do any kind of math, you can use a function graphing tool to get a visual representation of which increases slower.
Unfortunately, proving an algorithm’s worse case is not always easy, especially for complex algorithms. Fortunately for us, we can get by with estimating based on known algorithms. When I talk about specific algorithms or methods, I’ll make sure to point out the Big O value. Unless you’re working on a formal proof, you can usually get by with a simple “This is similar to merge sort” and thus assume you’re dealing with n log n.
Related Posts (generated):