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.

    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.

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.

Searching

Search algorithms are fundamental techniques in programming used to locate specific information within a collection of data. These algorithms play a role in various applications, from finding elements in an array to searching for records in databases. The primary goal of a search algorithm is to efficiently and accurately determine the presence or absence of a target element in the given data.

Search algorithms can be broadly classified into two categories:

  1. Sequential Search: Sequential search, also known as linear search, involves systematically checking each element in the dataset one by one until the target element is found or until all elements have been examined. While simple to understand and implement, sequential search may not be efficient for large datasets, as it requires a linear number of comparisons.
  2. Binary Search: Binary search is a more efficient approach used when the dataset is sorted. It employs a divide-and-conquer strategy, repeatedly dividing the search space in half and eliminating half of the remaining data with each step. This logarithmic reduction in the search space makes binary search significantly faster than sequential search for large datasets.

Algorithm Complexity

Algorithm complexity refers to how the performance of a given algorithm scales as the input size changes. It provides insights into how efficient an algorithm is in terms of time and memory usage.

Algorithms for game programming

Convert between measurement units

With our user-friendly format and practical examples, mastering unit conversions has never been more accessible. The section contains tools, explanations and example about converting from various measurement units.

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

Dijkstras Single Source Shortest Path Algorithm Not rated yet
Dijkstra's single source shortest path algorithm is used in graphing. It is an adaptation of the breadth first search traversal algorithm for use on weighted …

Click here to write your own.

   Search this site: