You have probably heard something about algorithms in real life. Simply, it is just a step-by-step sequence of actions that you need to do to achieve a useful result. It can be an algorithm for cooking a sandwich described by a recipe, or an algorithm for getting dressed according to today’s weather and your mood.
These algorithms are executed by humans in everyday life, but throughout this course, we will only concentrate on computer algorithms meaning they are executed by computers rather than humans.
Algorithms for computers
Computer algorithms are also everywhere around us. Your smartphone may guide you through a city from one point to another using a certain algorithm. Some algorithms can control the behavior of your enemies in a computer game. A good example you actually use every day is searching for some information on the Web using Google or Yahoo. These services perform sophisticated algorithms to search for the most relevant information for you. Algorithms are also used to calculate the trajectory of rockets and even help doctors to correctly determine diagnoses. Thus, all algorithms are absolutely different in their nature.
The main difference between real-life and computer algorithms is that a computer cannot guess what we want it to do. If something went wrong or an algorithm is not completely clear, a human can adjust an algorithm based on their experience but a computer cannot do the same. That imposes the following restriction: a computer algorithm must be described very precisely.
Of course, you may hear about algorithms that can learn and adapt to non-standard situations, but it is a feature of the algorithms themselves rather than computers executing them.
Programs and algorithms
As you may know, a program is a sequence of instructions to perform some tasks on a computer. The key difference between programs and algorithms is that a program is written in a specific programming language while an algorithm is usually described at a higher level than programming language statements. Thus, an algorithm is like an abstract schema, and the program can be its implementation.
In a sense, algorithms are language-agnostic, i.e. a given algorithm can be implemented in an arbitrary programming language on an arbitrary computer. You may use Java, C++, Python, C# or another language to write the same algorithm.
General properties of algorithms
What is important for a computer algorithm? There is a set of common properties:
- Specified input. An algorithm has one or more input values.
- Specified output. An algorithm produces a result.
- Definiteness. Each step of an algorithm must be precisely defined.
- Correctness. An algorithm should produce the correct output values for each set of input values.
- Finiteness. An algorithm must terminate after a finite number of steps.
- Generality. An algorithm solves problems of a certain type.
You may see slightly different lists of properties elsewhere, but this list contains the most important ones.
Why learn algorithms
Many algorithms have beautiful ideas and allow you to improve algorithmic thinking that is extremely useful in the development of more or less complex systems.
Even if you are not going to develop new algorithms, we believe that knowing about basic algorithms and understanding the principles of how they work are useful for the following reasons:
- it allows you to choose the best algorithm for solving a problem;
- sometimes, interviewers check the knowledge of standard algorithms;
- it is an excellent programming practice with many tricks and non-standard problems.
If you have other reasons to learn algorithms, just write them in comments 🙂
Good luck in learning algorithms!