Algorithms Review, Part 1

Recently I've been branching out into new areas of software development like artificial intelligence and machine learning. I will be covering these topics in future posts. As much as I enjoy learning new things, I believe it is important to refresh old skills in order to stay sharp and maintain strong fundamental skills. To that end, I have been reading a book on algorithms that I highly recommend, "Grokking Algorithms, Second Edition" by Aditya Bhargava. He provides a fresh take on an old topic. If you haven't studied algorithms, or if it has been awhile, I suggest you give the book a look. What I am going to do here is go through the algorithms he covers in his book and provide working examples in JavaScript as appropriate. Note that his examples are in Python, so if you get the books, expect to see slightly different syntax from his examples.

The first algorithm is binary search. Binary search is useful for searching a sorted array of values. It runs in O log n time rather than O n time, providing significant savings over a simple search that checks each value in turn from lowest to highest. Not that most languages, like JavaScript, already have built-in functions that provide this functionality, so this is more of an academic exercise than anything else. Here is my JavaScript version.