Data, structure, architecture, libraries, frameworks, etc., are all terms you will hear programmers use. However, one word you will come across and that many programmers will tell you is the be-all and end-all of programming are algorithms. Specific steps used to compute a computation are what’s known as an algorithm in computer science. So how important are they really?
Programmers at a professional level should know and understand which algorithms are essential for specific coding situations. This is because sometimes, one algorithm will be more beneficial than another even if it generates the same result. Beginner programmers should understand fundamental concepts and techniques relating to their language first.
If you want to become a programmer and are overwhelmed by everything thrown at you from every direction in terms of what individuals say you need to know (especially about algorithms), this article is for you. We will go over if beginner programmers really need to understand algorithms and how knowing them could benefit you in specific situations using examples.
Do programmers need to know algorithms?
Today’s world has taken us far beyond the beginner days of programming where there were not any frameworks, libraries, architecture, etc., to draw from. Lucky enough, things have changed drastically, and as an individual that wants to learn programming as a beginner, it is not that necessary to understand complex algorithms for the most part. However, there are always some exceptions.
Think about it like this. Are individuals able to play music without knowing music theory? Can people paint the most amazing pieces of art without having any theory in art? The answer to these questions is, of course, they can. The same applies to programming.
It is scarce that you will have to write complex algorithms in code these days as a beginner. The reality of practical coding and real-world coding is that algorithms are almost a non-existent part of it all. This is in terms of you, the beginner programmer having to come up with complex algorithms. It is very rare.
The complexity in 99% of the software is in architecture. Therefore, architecture is far more essential to learn as a beginner programmer than algorithms.
What is architecture in software?
Software architecture is the discipline of creating fundamental structures in a software system. “Architecture” is a metaphor, and it is basically a blueprint for the software system and project. In the most simple terms, it means laying out the necessary tasks that have to be executed by the design teams.
How do you become good at software architecture?
All these foundations and more extend into architecture. The best architecture is simple and allows for building clean apps, systems, and software that are more easily debugged.
The need to know complex algorithms in a system or app, for the most part, is once again pretty rare these days, with a handful of exceptions.
Why should programmers learn algorithms?
As we said, there are situations where perhaps understanding and knowing algorithms will be beneficial. This is because, typically, there are many algorithms that can perform the same task, and some algorithms are better than others even if they can generate the same results.
Best practices for algorithms are ones that use the fewest steps to get to the solution (compute). Remember, the more lines of code, the more processing power the computer needs in order to run it (solve it quickly).
Take into consideration that if a programmer or team of programmers are developing complex apps, systems, or software, this is key and should not be underrated.
Let’s take a look at an example algorithm for “sorting.”. Sorting will practically be in every app, system, or piece of software. If you think about it, you can sort your mail, dates, names, numbers, times, lists, etc., and you may be thinking, well, that can’t be so bad; how many sorting algorithms are there? We looked this up and came up with 48 unique sorting algorithms.
Let’s try sorting out an example using two very different algorithms that generate the same result.
Say there are a set of random numbers that you have to sort for some reason. One thing to note quickly is that a series of items is called an Array.
The selection sort algorithm will scan down the list of numbers starting at the top to find the smallest number, and the first number (20) will be, by default, the smallest.
The following number is 12, which is smaller than 20, thus becoming our new smallest number. Scanning the list, the algorithm will check all the numbers, finally ending and deducing that 5 is the smallest number.
We will then place the array in ascending order swapping the smallest number with the first number sorting one number in the list, and hence our new list looks like this.
Now we repeat the same process with the list, but instead of starting with the first number, we start at the second number. As we can see, 12 is the next smallest number, so nothing changes; hence, we move onto the third number.
Once again, we repeat this process, starting on the third number. The third number will be swopped with the fourth in this instance, and our new list will look like this.
As you can see, when the process is repeated again from the fourth number, nothing will change again. Once we reach the fifth number and repeat, then we will swap the fifth number with the sixth, and our list will finally be in order and complete.
The pseudocode for this example would look something like this.
FUNCTION selectionSort(array) FOR i = 0 TO end of array smallest = i FOR index = i + 1 TO end of array IF array item index < array item smallest smallest = index END IF NEXT swap array items at index and smallest NEXT RETURN array
This function can be used to sort our 6 numbers in our example or even 6 million numbers. Once you have written this function, you can use it over and over again. You can see in this example code, the one FOR loop is nested inside another FOR loop.
We can break this down and say if we want to sort N items, we will have to sort N times. Hence this can be written as N2.
This relationship of input size to the number of steps the algorithm takes to run constitutes the complexity of this specific algorithm. This will give you a rough approximation of how quickly an algorithm is going to be.
N2 is not very efficient. For small arrays, it would be fine. For example, an array with five numbers would have a computation of 25. However, if we had an array of 50, the computations would equate to 2500.
As you can see, even though our array grew by only ten times, our computations increased over 100 times. Logic would dictate that this will be magnified as the arrays grow larger, and this can cause problems if you are a company like Google that needs to sort arrays with millions of entries. It would take forever.
This is just one example that we are giving you here, but as you can see, if you are a programmer at Google and are delegated with handling a project that needs to sort a large list of arrays, then knowing sorting algorithms would be beneficial to your situation and you would opt not to use selection sort.
Let’s take a look at a better algorithm for sorting in this instance and why knowing algorithms would benefit you.
The first thing that Merge sort will do is check that the size of the array is greater than 1. If it is, then it will split the array into two halves. Using our previous array of six numbers, it will be sorted into two groups of three.
Merge sort again will check if the array is larger than one and will continue splitting up the arrays until there is one array for each item. Hence, in our example, we will have six arrays with six numbers. Now the algorithm will begin merging the arrays starting with the first two. These will be arrays of 20 and 12.
Reading the first and only value of each array merge sort will identify which one is smallest and create a new array with that number at the top.
This process is then repeated for the remaining pairs putting them each in sorted order. Thus we end up with three arrays looking like such;
The initial process will continue whereby the first two arrays are merged and sorted, and the process will carry on until there is one final array with all the sorted numbers in ascending order. This computation can be written as (N log N), whereas you can see it is different from N2 as with selection sort.
N comes from the number of times we compare and merge items, and this is directly proportional to the number of items in the array. “log N” comes from the number of merge steps. In our example log2 6 = 2.5. This can be a bit confusing, so let’s take an array of 8 where there would be three splits to the array; log2 8 = 3.
Now, if we double our base of 8 to 16 (twice as many items to sort), it would only increase the number of split steps by 1; log2 16 = 4.
What is really cool about this is even if we increase the items in the list to reach the thousands, the array split will still be small. For example, log2 10000 = 13.
Compared with selection sort where the computation would be 100002, which would equal 100000000. That’s a tad bit more than 13.
So as you can see, if you were working on a project and needed to sort arrays with thousands of items, Merge sort would be much more beneficial than Selection sort.
Even though you may be programming in a language with libraries and frameworks that can implement these algorithms, you would need to understand how each one works to use the correct one in specific situations. Otherwise, you may find that your sorted list is taking 5 hours to load.
Are there other reasons to learn algorithms
Another reason you should learn algorithms is that many significant companies quiz you on algorithms in interviews. Companies such as Google, Microsoft, and the likes all love to ask you to explain and solve with algorithms even if the position does not require you to know them.
What are the essential algorithms a programmer should learn?
There are hundreds of algorithms that perform various tasks, and unless you have an eidetic memory, it would be pretty challenging to learn them all. However, there are a couple that you as a programmer should know because they are implemented very often.
Although we can explain them all because that is beyond the scope of this article, we have listed the top 25 algorithms (courtesy of TechieDelight) that all programmers and computer science students should know.
- Binary Search Algorithm
- Breadth First Search (BFS) Algorithm
- Depth First Search (DFS) Algorithm
- Merge Sort Algorithm
- Quicksort Algorithm
- Kruskal’s Algorithm
- Floyd Warshall Algorithm
- Dijkstra’s Algorithm
- Bellman Ford Algorithm
- Kadane’s Algorithm
- Lee Algorithm
- Flood Fill Algorithm
- Floyd’s Cycle Detection Algorithm
- Union Find Algorithm
- Topological Sort Algorithm
- KMP Algorithm
- Insertion Sort Algorithm
- Selection Sort Algorithm
- Counting Sort Algorithm
- Heap Sort Algorithm
- Kahn’s Topological Sort Algorithm
- Huffman Coding Compression Algorithm
- Quickselect Algorithm
- Boyer–Moore Majority Vote Algorithm
- Euclid’s Algorithm
We discovered that needing to know algorithms as a programmer is a double-edged sword. As a beginner, you don’t really need to know algorithms because you won’t be writing or coming up with them yourself. They will be part of the framework or library of the language that you are using.
You will implement them in your projects, but you will rarely need to apply them in standalone applications in real-world coding.
However, when you work on specific projects that require complex solutions, it would be in your best interest if you understood and knew the available algorithms for that particular situation.
This is because there might be a better solution to the problem you are facing, as with our examples of Selection sort and Merge sort. Selection sort could be used for an array the has very few items, but as soon as the items start to increase, so would the computations, and this would take an extremely long time if the arrays had items with millions of entries.
Even though there are hundreds of algorithms that perform either the same or different functions, there is a handful that every programmer should know. We listed the top 25 algorithms used for various applications and knowing these would allow you to cement and broaden your understanding of programming as a whole.
The best thing to do is to work on your programming skills by performing real-world projects and learning algorithms as you progress as a programmer.