Coding Basics 3-Part Series: What is an Algorithm?
By: Tech Elevator Instructor Walt Impellicceiri
[PART 3] In this 3-part series, I’m breaking down the foundation of computer programs. We will focus on what they are and how they are made.
An algorithm is a set of guidelines for how we perform a task. Recall that a computer program is a set of instructions that a computer executes to perform a task. The difference between a computer program and an algorithm is twofold – a computer program may contain more than one algorithm, and a computer program consists of algorithms translated into machine readable code. Setting that aside, let’s dive into algorithms a bit more.
Algorithms are everywhere. I’d consider a recipe to make cookies to be an algorithm. In computer science, however, the most famous algorithms are more fundamental. There are algorithms for sorting, searching, finding the shortest path in a given route, and more. Algorithms can be defined using different forms of notation. Natural language, flowcharts, and even images are appropriate. From these forms of notation, an algorithm can be translated into source code using a developer’s preferred programming language.
Below I’d like to dive into an example of a famous sorting algorithm called merge sort. This algorithm is famous because of its performance. It’s among the fastest sorting algorithms.
Merge sort uses a divide and conquer strategy, splitting the problem into easier to accomplish subproblems before solving each subproblem. The algorithm can be described as such:
- Repeatedly divide the list in half until you arrive at n sublists, one for each item in the list. Each sublist can be considered sorted.
- Merge the sublists in sorted order repeatedly until there is only one list remaining. The resulting list will be sorted.
So why is this important? When we are writing computer programs, the need to implement an algorithm comes up over and over again. I’d like to revisit the pizza program that I mentioned in the first article. Constructing a pizza involves its own algorithm. Before you jump into making a pizza, though, you might consider using a sorting algorithm. If the ingredients for your pizza are sorted in the order in which you use them for constructing the pizza, it’s likely that you’ll be able to make pizzas quicker!
Now, let’s assume that you make a bunch of pizzas and line them up in a row, each with their own toppings. You may want to further sort your pizzas in a way to help your guests find the right slice. Perhaps you will sort them in the order in which they were cooked so the pizza is at a perfect eating temperature. Perhaps you will divide them into vegetarian and meat options. Perhaps they will go so quickly that it does not matter. Regardless, I hope you are catching my point! There are constantly situations where algorithms could be useful, whether that is in real life or in software!
Good luck on your coding journey. If I can help with anything along the way, feel free to drop a comment below.
Walt graduated from the University of Pittsburgh with a degree in Computer Engineering. He has spent the last 10+ years developing applications for companies in the healthcare, retail and education industries. He leads instruction at Tech Elevator Pittsburgh.