A common term people use in computer science and coding is 'algorithm.' What is it and why is it important for coding? In partnership with Juni Learning, we share their article here to define this important concept.
What Is An Algorithm?
An algorithm is a set of step-by-step procedures, or a set of rules to follow, for completing a specific task or solving a particular problem. Algorithms are all around us. The recipe for baking a cake, the method we use to solve a long division problem, and the process of doing laundry are all examples of an algorithm. Here’s what baking a cake might look like, written out as a list of instructions, just like an algorithm:
Preheat the oven
Gather the ingredients
Measure out the ingredients
Mix together the ingredients to make the batter
Grease a pan
Pour the batter into the pan
Put the pan in the oven
Set a timer
When the timer goes off, take the pan out of the oven
Algorithmic programming is all about writing a set of rules that instruct the computer how to perform a task. A computer program is essentially an algorithm that tells the computer what specific steps to execute, in what specific order, in order to carry out a specific task. Algorithms are written using particular syntax, depending on the programming language being used.
Types of Algorithms
Algorithms are classified based on the concepts that they use to accomplish a task. While there are many types of algorithms, the most fundamental types of computer science algorithms are:
Divide and conquer algorithms – divide the problem into smaller subproblems of the same type; solve those smaller problems, and combine those solutions to solve the original problem.
Brute force algorithms – try all possible solutions until a satisfactory solution is found.
Randomized algorithms – use a random number at least once during the computation to find a solution to the problem.
Greedy algorithms – find an optimal solution at the local level with the intent of finding an optimal solution for the whole problem.
Recursive algorithms – solve the lowest and simplest version of a problem to then solve increasingly larger versions of the problem until the solution to the original problem is found.
Backtracking algorithms – divide the problem into subproblems, each which can be attempted to be solved; however, if the desired solution is not reached, move backwards in the problem until a path is found that moves it forward.
Dynamic programming algorithms – break a complex problem into a collection of simpler subproblems, then solve each of those subproblems only once, storing their solution for future use instead of re-computing their solutions.
Example of an Algorithm
There are a number of different algorithms, from simple to very complicated, that exist for solving a Rubik’s cube. Below is just one simple algorithm. First, let’s specify a notation to use (similar to picking a programming language).
Each of the six faces of a Rubik’s cube can be represented by the first letter of their name:
U - up
D - down
L - left
R - right
F - front
B - back
Each face can be turned in three different ways/directions. Using U as an example, these are represented as:
U - clockwise quarter-turn of the upper face
U' - counter-clockwise quarter-turn of the upper face
U2 - half turn in either direction of the upper face
Now, let’s go through the steps in the algorithm to solve a Rubik’s Cube. Feel free to grab one of your own and follow along!
Step 1: The Cross
First, flip some edges so that there is a white cross on the upper face.
Apply the following turns: F, R’, D’, R, F2, R’, U, R, U’, R’, R2, L2, U2, R2, L2.
The cross is now solved.
Step 2: The White Corners
The edges on the white face are now complete, but the corners remain.
Depending on where the white-orange-green corner is in the puzzle, apply one of the following series of turns:
Bottom: R’, D’, R, D (repeat until the corner moves to its correct place)
Top: R’, D’, R, D (this moves the corner to the bottom; then, follow the above instructions)
Step 3: Middle Layer Edges
Flip the cube so that the white is on the bottom.
Look for an edge that is on the top face and doesn’t have yellow on it.
Perform a U-turn so that the color on the front face of the edge matches with the center.
Depending on the direction that the edge could go, apply one of the following series of turns: