At the simplest level, an algorithm is a set of specific instructions or steps that can be taken to successfully complete a task and that can be repeated indefinitely.
When we talk about an algorithm in computational terms, we’re talking about the specific set of steps that are carried out to complete the computation.
Of course, the details of what make algorithms so important are a lot more complicated than this, but as a starting point, this introductory definition is a great place to start when thinking about algos (that’s the abbreviation for algorithms).
Algorithm Essentials – Complexity and Cocktail Parties
Think of almost any complex task and without fail, the simplest way to get it done is usually to break it down into a series of smaller, manageable steps.
This is essentially what an algorithm does.
An algorithm does its thing by carrying out all of the smaller component steps that are needed to complete the final complex task.
You can think of this a bit like any number of repetitive, multi-step, and otherwise complex routines that might need to be carried out in the real world – like spending the night making individual cocktails for guests at a party.
Each cocktail made requires a very specific set of steps that have to be followed in a specific order and using specific quantities of ingredients each time.
Not just this, but each type of cocktail requires a different set of variables to the others.
When someone orders a Margarita, the requirements are going to be very different to a Manhattan or a Moscow Mule. Each are different and each have specific steps which culminate in a complex (but delicious) end-product.
However what about if everybody that night orders Mojitos?
Well in this case, the same step by step preparation is required, but it needs to be repeated exactly the same each time the drink is made.
What we need, is an algorithm.
Now lets say we decide to build a robotic assistant that will flawlessly execute the production of our cocktails. In order to program our automated mixing maestro to create the perfect drink on command every time, we need to give it instructions on how to create each drink a guest orders so that it can repeat as often as necessary without fail.
We’ll need to produce a series of algorithms (one for each drink) that let the machine know the exact steps that need to be taken, in which order they need to be executed, and the ingredients and quantities required at each step along the way.
Here’s what the (overly simplified) non-code version of our algorithm might look like for making a Strawberry Daiquiri:
- Guest orders Strawberry Daiquiri
- Take 1 medium glass
- Add 1 strawberry to the base of the glass and muddle
- Add 3/4 ounce of lime juice to the glass
- Add 2 ounces of white rum to the glass
- Fill remainder of the glass with ice to the rim
- Add cocktail shaker to top of glass and hold both together
- Continue to hold shaker and glass together and simultaneously turn glass, emptying contents of the glass into shaker
- Continue to hold shaker and glass together and shake both vigorously
- Take a new, chilled glass from the refrigerator
- Place a strainer over chilled glass and empty the contents from the cocktail shaker into the strainer
- Serve drink to guest
With this set of instructions in place, our robo-mixologist should be ready to tackle this order and make the perfect drink whenever it’s requested.
Once we program all of the other drinks into the machine in the same way – with individual algorithms for each – not only will we not need to tell the machine every single time a drink is requested (or worse yet, make it ourselves), but from that point on, the perfect replicable cocktail is just a button press or voice command away.
This is the power of algorithms and they form the backbone of computer programs which have to carry out thousands of complex and varied tasks again and again and again…and again.
Sometimes algorithms are put to work helping with the mundane repetitive tasks that we simply wouldn’t want to carry out on repeat day in and day out, and sometimes they’re put to work on less common tasks where having a programmed system find a better way of doing them is also incredibly beneficial.