Algorithms: An overview
We’ve circled algorithms in the last two topics, but now we’re going to get into the meat of it!
An algorithm is a set of instructions for solving a problem or completing a task. In computer
science, algorithms are used to solve all sorts of problems, such as the one I mentioned above
about searching in arrays.
We can combine everything we’ve learned so far together to understand how a binary search
algorithm is better performing than a brute force algorithm.
For example
Let’s say we had an array containing random integers sorted in ascending order, and we don’t
know how many numbers are going to be in the array:
A search algorithm is an algorithm that finds the index of a given integer. If it exists we return
the index of the integer in the array, if it doesn’t we return -1.
An intuitive solution we can come up with is to start from the first integer and check if the value
is what we’re looking for. If it is, we return the index, if it’s not we go right by one index and
repeat until we either find the value we’re looking for or we reach the end of the array and
return -1.
To evaluate the Big O time complexity of this solution, we just need to think about the worst
case scenario.
For example
If we receive a value that doesn’t exist in the array, we have to check every value in the array.
This means our solution runs in O(n) or linear time.
This means that if we have 100 integers we do 100 checks and if we have 1,000,000 integers we
do 1,000,000 checks.
According to our chart, this is a good time, but we can do better with the binary search
algorithm.
The binary search algorithm works if the values in our array are sorted in either ascending or
descending order. Let’s take a look at how binary search works:
The first step is to go to the middle index of the array (if our array is even, we can round up or
down) and check if the value is equal to the integer we’re looking for.
If this value is equal to our integer we return the index, if it isn’t we check if this value is greater
than the one we’re looking for. If it is, we can throw away everything to the right; but if it’s
lesser we throw away everything to the left. With the right half of the array removed, we repeat
the same steps.
We take the middle index, compare the value, and if it exists we return the index, if not we
check if the current middle value is greater than the integer we’re looking for.
If the middle value is greater, than we know we want to continue searching through the values
that are smaller so we throw away the right side, and vice versa if it’s not.
We repeat this until we find our value, and if we can’t find it, we return -1.
Binary search is a much more efficient algorithm since each iteration of choosing the middle of
the array and potentially throwing away half of the array, means with each iteration we’re
reducing the amount of elements we have to search through in half!
Cutting our search space in half per iteration means that in the worst case, we’re dividing the
total array by 2 until we end up with 1 number left.
This is exactly what the logarithm of base 2 does, as its the inverse of 2x.
If 25 is 2 doubling itself 5 times: 2 x 2 x 2 x 2 x 2 = 32; then log2 N is trying to figure out how
many times do we halve N until we end up with 2: log2 32 = 5.
This halving is the exact same as what binary search does on each iteration, it cuts the search
space in half, meaning our Big O for binary search is O(log2 n).
This is the second best category we can get and its better than our previous solution’s O(n)!
tl;dr
There are numerous other algorithms, many of which are more complex than binary search and
many that require a deep understanding of the more complex data structures, and also
algorithmic patterns.
But most aren’t necessary in your day to day as a web developer unless you’re working at (or
even applying to) a big tech company (Meta, Apple, Amazon, Netflix, Google, Microsoft, Tesla,
etc.).
The software engineering interviews for these companies are almost exclusively data structures
and algorithms.
At large tech companies you’ll see more complex data structures such as trees, graphs and
linked lists occasionally in your work, but outside of these large companies it’s much more rare
to encounter them.