What is an algorithm – this may sound like a silly question, but there are many definitions of algorithms on the Internet. In previous articles Patryk wrote about the time and space complexities of algorithms, so it would be appropriate to explain what algorithms are.
Let’s get back to our definitions. Here are a few of them:
- An algorithm its a set of procedures of logical steps leading to the solution of a problem.
- An algorithm for the compilation of instructions, the execution of which provides us with a certain goal. It is a finite sequence of certain actions necessary to complete the task.
- Algorithm to a method of executing a problem with a plan, taking steps that will always be displayed to solve this problem.
The simplest analogy for explaining what is an algorithm
First analogy that comes to my mind when I think of an algorithms it is doing laundry in a washing machine. To wash some clothes you always have to follow some steps. For example:
- Gather your dirty clothes and sort them into piles based on color and fabric type.
- Measure the correct amount of detergent to use and add it to the washing machine.
- Place the sorted clothes into the washing machine.
- Choose the appropriate wash cycle based on the type of clothes and the degree of soil.
- Start the machine and let it run through the cycle.
- Once the cycle is complete, transfer the clothes to the dryer or hang them up to air dry.
In programming the procedure representing washing would look similar.
// Step 1: Gather dirty clothes
let dirtyClothes = gatherDirtyClothes();
// Step 2: Sort clothes
let sortedClothes = sortClothes(dirtyClothes);
// Step 3: Measure detergent
let detergentAmount = measureDetergent();
// Step 4: Add detergent to machine
addDetergent(detergentAmount);
// Step 5: Place sorted clothes in machine
placeClothesInMachine(sortedClothes);
// Step 6: Choose wash cycle
let washCycle = chooseWashCycle(sortedClothes);
// Step 7: Start machine
startMachine(washCycle);
// Step 8: Transfer clothes to dryer or hang to air dry
let dryMethod = transferClothes();
dryClothes(dryMethod);
Underneath washing machine interface we would have electronical system taking care of start of the washing machine drum, keeping appropriate temperature and so on. The same underneath each of our methods we would have some programming concepts like loops, conditional statements and other elements of syntax and used technologies that computer can understand to do ‘the laundry’ when we ask for it by using programming language.
Algorithms it’s just like creating a recipe
Another, Probably the most commonly used analogy to illustrate the idea of an algorithm is a recipe. Performing steps according to the introduction will lead us to the effect, which is some dish. In programming, the individual steps are determined by the requirements of the program and limited by the syntax and other boundaries of the used technologies. The same like with cooking you may need a blender or sometimes some fancy aparature to check temperature of the baked break inside it. When some housewife is out of this fancy aparature has to look for workarounds the same as programmers do. But always the same as in cooking with recipe in programming the goal is to get things done in a repetitive way. The goals of our ‘cooking-algorithms’ are some soup, baked chicken, spaghetti or whatever in programming the expected result of an algorithm may be finding an item in the sorted array, sorting users by their surname, displaying data in the table, submit a form when user clicks button up to the much more complicated stuff.
Thinking algorithmically
Algorithmic problems are solved using so-called Algorithmic thinking. It consists in breaking large insoluble, divisible problems into smaller, indivisible and solvable ones, and then solving them step by step.
The initial factor that we should consider when writing an algorithm is simply its correctness. The correct algorithm is the one that when run always gives expected results.
For each type of input data, the algorithm should always produce the same result. But remember that within expected results there could be no result (under certain conditions we may expect algorithm to do nothing).
Let’s go to the another example. Given an algorithm that determines the index of a given number in a sequence. If we use a sequence that does not include searched number we may want to return an information of on finding the number in the sequence. As a side note, often such algorithms in this case return -1.
Algorithm needs boundaries
In order to determine the frames of the algorithm, it is necessary to think about it’s boundaries. Algorithm problem needs to be designed the way that it will be solveable. It must have a clear entrance and exit, input and output. In addition, the result should be achieved in a reasonable time. The algorithm does not “grind” something into impossibility.
In programming, as in life, there are many different ways to achieve the same effect. Some are very effective, others slower. The time that it takes for a computer to run the algorithm is determined by the code quality, hardware wear, and the size of the input data.
How do I know that my algorithm is good?
There are many measures to determine the quality of the algorithm, like readability, usage of the newer syntax and so on. Two of such metrics are time and space complexities. About which Patryk wrote in this(time) and this(space) articles. And these are very important topics, if not crucial that as a programmer you should be familiar with – at least at the basic level. Not knowing them may lead you to crashing the whole application. So put aside playing videogames for a while and take a brief look at some articles about it :).
However, when it comes to the quality of algorithms, there is one thing – more efficient doesn’t always mean the better. Very ofthen, especially, during using high-level programming lanaguages so called write time and the readability, scalability and other factors when the code are more important. Sometimes writing a few basic lines of code more it is the cost a few CPU clocks, but make the code way easier to understand for other programmers.
Summary of what is an algorithm
Algorithm is the guide leading to the execution of some task like a recipe leading to cooking tasty meal. Writing an algorithm should start from clearly defining the problem. This problem must be solvable and have a clear entrance and exit / input and output. The algorithm should be able to solve the problem in a reasonable amount of time and without consuming vast amounts of computer resources each time when run.
How to learn algorithms and data structures
Algorithms as the domain are particularly old. First of them were used over 4000 years ago! Many of the emerging algorithmic problems have already been solved and classified.
Therefore it would be a waste of time to try to reinvent the will. It is a good idea to study algorithms and data structures. Not only because it is required knowledge in many of the biggest companies (FAANG) but also it improves your problem solving abbilites.
A simple roadmap of how to become good in solving algorithmic problems would be:
1. Learn basic algorithms and data structures – start from the overview then get into details.
2. Implement algorithms and data structures by yourself. It will give you more profound understanding and hands-on experience.
3. Start solving algorithmic problems on portals like leetcode or hackerrank from simple through medium to hard ones. Do it gradually, step by step. Don’t try to reach too far out of your comfort zone.
PRO TIP
Don’t spend too much time on hitting your head against the wall. Many of the experts advise to not spend more than 30-40min on solving the problem. It is anyway the time given on the interviews. So whenever you are exceeding it just jump into solutions and analyse them. There are thousands of programming puzzles it is all about recognizing patterns. Don’t feel guilty when something is too hard for you. You will see similar tasks in the near future anyway.
PS to What is an algorithm
If you would like to familiarise yourself with everything you need to know about abstractions in programming you can do it here.
Articles about time and space complexities are under the mentioned links.
Take care,
Patryk
Senior Software Engineer with over 7 years of experience and entrepreneurial background. Most often, apart from delivering good quality code on time, responsible for introducing good practices, teaching programmers and building team bonds andestablishing communication on the line of development-management. Privately Kākāpō and Wombat enthusiast, traveler and retired acrobat.