Intro to Algorithms
While the word may seem somewhat alien and even intimidating to some if you are just getting your feet wet. But do not worry, we use algorithms to help ourselves and even use them in our daily lives without even knowing it. I am not referring to the devices or applications you connect with daily, but you as a person. Let’s jump right into it.
What is an algorithm?
The way that I was taught how to better understand algorithms is to think of them like making a grilled cheese and that is not too far off from the truth. According to Merriam-Webster’s broad definition, an algorithm is “a step-by-step procedure for solving a problem or accomplishing some end”. Maybe something like a recipe for making grilled cheese so you can enjoy that gooey goodness. Without even knowing it you use an algorithm when following a recipe, sorting laundry, and even getting dressed.
In the development realm, an algorithm is exactly the same. We create a function or method that will follow the necessary steps to achieve our goals. These algorithms could be finding the smallest word/s in a data set, sorting data sets alphabetically or numerically, removing odd/even numbers, does the string contain valid parenthesis, is the string provided a valid phone number; anything really. Let’s take a look at some examples of different algorithms, while breaking them down.
Sorting The Laundry
Let’s revisit the example of having to sort the laundry. When personally doing a load of laundry I will often make two piles. One for color and one for whites, as I have previously had the misfortune of color bleeding into my whites when doing a mixed load. To start things off we will have our array of all our dirty clothes.
We will then need to declare our function, aptly named sort laundry, that takes in an array as an argument. Since we know that we are going to make two piles or have two loads, we will create an empty array for color load and an empty array for our white load like so.
Our sort laundry function, first will need to go through each piece of clothing in our dirty laundry pile. While it is looking at each item of clothing, we can determine which load it will be placed with. I would choose to write my function as such.
For each piece of clothing, we will first put the string to lower case, should we have gotten lazy or missed a capitalization when making our list. Then if our item of clothing includes white in the name, we will push the item into our white’s pile. Should our clothing item not include white, it will be pushed into the colors pile. We can then return however you would like, for these purposes I have decided to return a message with the length and items in each pile.
While this may seem like a simple function that sorts our laundry, we have still created an algorithm. In this case it is simply sorting the items based on the condition that the string includes white. At the same time, some cream or other off-white colors that should be included in the white’s load would be placed in the colors load. This would require you to change up your algorithm’s conditions. I have helped you get this far and challenge you to make the necessary changes and/or additions to complete such a task.
Find the shortest word and its length
Now let’s take a look at an example that I had personally received as part of a technical interview. This question may not seem too difficult but shows you can transform data when necessary as well as the ability to work with different data types. For this example, we will be given a string where we will need to extract that shortest word and provide its length. Let’s start out by declaring our function, find short word. We will also need to turn that string into an array.
By splitting the string at each space, we have now reassigned string to be an array of all the words that have been provided. Now that our data type has changed from a string to an array of strings, we can now loop through each word. But before we start our loop, lets first set our shortest word to the first word in the array.
We will then create a loop to compare the length of the shortest word to the length of the current word in the loop. Should the shortest word be bigger than the current word in our loop, we will then reassign the shortest word to be that word. Otherwise, we will do nothing. This will continue until every word has been compared to the shortest word. This is how I would personally write this function.
We can then also customize our return how we would like but should be sure to include the shortest word as well as its length.
Again, this may seem like a simple function preforming a simple task, but again have created an algorithm. The test strings in this case are short sentences and one could possibly do so by just reading them. But imagine getting strings that are paragraphs or even pages long and having to do so all day long. That would take a lot of time to do manually and would honestly burn you out in the process.
Not only can the computer make these calculations in fractions of the time, they also don’t get tired or burnt out, and in theory could keep going forever. While I am not trying to compare anyone’s brain power to computing power, we create algorithms to help us achieve our goals. And while one algorithm alone might not seem to make much sense, but multiple algorithms all running together in harmony help create your favorite apps and their features.
I would like to thank you for reading along and hope that you have learned something new! Keep an eye out for more articles the future!
If you would like to view a copy of these examples to play around with for yourself or just for reference, you can find the link for the GitHub Repo here.