Algorithms

    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.

Do you learn better from video?

Learn faster with deeper understanding!
The "Computer programming for beginners" course is the perfect place to begin with programming.

Start now!

    Note: We use flow charts to describe the sequence of steps. If you are not familiar, review the flow chart and flow chart symbols lessons from the programming tutorial for beginners.

    If you are a beginner, make sure you know what we are talking about here. Check out the algorithm definition and next: examples.

    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?

    Yes? - Great! Let's move on ..

Algorithm structure types

    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;

Linear algorithm flowchart

    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...

int main(void)
{
    int var1, var2, var3, sum;
    printf("Please, input 3 numbers!\n");
    scanf("%d%d%d", &var1, &var2, &var3);
    sum = var1 + var2 + var3;
    printf("The sum %d + %d + %d is %d.", var1, var2, var3, sum);
    return 0;
}

Branched

    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:

Branched algorithm flowchart

    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.

Do you learn better from video?

Learn faster with deeper understanding!
The "Computer programming for beginners" course is the perfect place to begin with programming.

Start now!

int main(void)
{
    int var1, var2, var3, max;
    printf("Please, input 3 numbers!\n");
    scanf("%d%d%d", &var1, &var2, &var3);
    max = var1;
    if(var2 > max)
        max = var2;
    if(var3 > max)
        max = var3;
    printf("%d is the max from %d, %d and %d.", max, var1, var2, var3);
    return 0;
}

Cyclic

    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.

Cyclic algorithm

    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:

int main(void)
{
    int count, temp, i, sum = 0;
    printf("How many numbers you want to sum?\n");
    scanf("%d", &count);
    for(i = 1; i <= count; ++i)
    {
        printf("Pleans input the next number: ");
        scanf("%d", &temp);
        sum += temp;
    }
    printf("The sum is: %d", sum);
    return 0;
}

Sorting

    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.

Tell us about your favourite algorithm

Do you know a very smart algorithm? Such that is very efficient, curious or just cool?
Share it with us!

Algorithms from other visitors

Click below to see algorithms from other visitors to this page...

Linear Search 
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. …

Click here to write your own.


› Algorithms

Did this help? Support me with your vote ;-)

Did this help?