Algorithms are the beauty of programming. Their choice separates the fast from the slow programs. One great thing about programming is that you don't need to reinvent the wheel every time. Learn the techniques, used through the years and then use them in your practice. Let's take a look at the most popular ones, analyze and compare them.
The next step is complexity and analysis of algorithms. They are very important elements of your programming knowledge. Analysis will teach you what advantages each technique has. This way you know what is the better choice in the different programming situations and environments.
Do you already know how to read flow charts?
Do you know how to analyze and find the complexity?
- Great! Let's move on ..
The most simple algorithms are linear. They don't make any decisions. They just do their stuff step-by-step. Here is an example flow chart;
Here is an example in C that reads 3 numbers and then prints their sum. It does not make any decisions, but just follows the actions 1, 2, 3...
Branched algorithms take at least one decision during its execution. They are a bit more complex than linear and more useful, too. Here is a very simple example:
And here is an example in C that finds the biggest value from 3 numbers. To make a simple decision in C, we use the if statement. If we need to choose an exact value, out of many options, we can also use the switch statement, but in this example we don't need it.
Cyclic are a special type of branched ones. They take at least one decision, but one of its outcomes goes back and repeats the decision. This allows to automate a program to repeat some action many times.
Normally this is coded, using loops. In C, there are 3 loops - while, do..while and for. Another way is to implement our custom loop, using a decision with an "if" or a "switch" and do the repetition, using the goto statement and a label.
However, using goto should be avoided 99% of the time, because it makes the code harder to read and fix.
Here is an example in C, using the for loop:
Sorting is a very common task in programming. We may sort a table in a web page or an array of data or any other data structure. The basic reason for this is to make searching in the data faster.
But how do we make sorting faster? It depends on the situation. Sometimes the data size is small and a simple implementation will be the best choice. In other cases we will choose a more sophisticated sorter.
Some of the factors to consider are the data structure itself, its size and size of a single element. I explain in deeper detail about this in the sorting algorithms article. If you follow the link, you can read about the most famous sorters.
Do you know a very smart algorithm? Such that is very efficient, curious or just cool?
Share it with us!
Click below to see algorithms from other visitors to this page...
Linear search is very simple algorithm. It uses "brute force" to find an element. 1. declare the array a n 2. declare "value" 3. User initialize "value" …
Binary Search Not rated yet
In most cases Binary Search is faster than Linear Search. We use it when the array is sorted. The idea behind it is take advantage of the sorted elements. …