It’s really good to practice with the algorithm challenges, on Free Code Camp and elsewhere, but I’m thinking in particular about algorithm analysis. This answers a question that we (web developers) take for granted - How do we tell if one algorithm is faster than another? It’s not about how much code is written, because modern compilers are really good at optimizing human-written code. We could benchmark two different algorithms, but getting two computers set up to race each other is harder than it sounds. There are background processes to account for, hardware has to be identical, and we need to come up with mock data. We would certainly be able to get a sense of which algorithm is faster, but not why. The most usable approach to finding the “speed” of an algorithm is by getting an abstract idea of how much work is done per element of data being processed.
Most of the algorithms introduced in introductory algorithm classes are about the most efficient way to deal with arrays of information. As an example, write an algorithm that will find a particular number in an array
The easiest solution is to start from the beginning and iterate through the array until the number is found. So, if we keep adding numbers to the array, how much more work needs to be done to find a number (in the worst case)? For this array, it’s after 15 iterations. If we add another 10 numbers, the worst case would go up to 25 iterations. Add another 100, the worst case is now 125. The work increases linearly with the number of inputs. We generalize this by saying the algorithm has a linear time complexity in the worst case, or use the shorthand notation, O(n) (Big-O of N). We can do better than this, though I won’t say how. A simple algorithm can search a sorted array in O(log n). What does that mean? If we have an array with 1,000,000 elements, then a linear search could take 1,000,000 iterations. The logarithmic algorithm would take 20 (rounding up). That’s a big deal. Imagine how long it’s taken for computer hardware speeds to increase by a factor of 50,000, yet we can do that with a fundamental understanding of algorithms.
For more info, check out the algorithm lessons in the guides above. It’s a difficult, wide ranging topic, but knowing even the basics will get you far.