DEV Community

Leonardo Cunha
Leonardo Cunha

Posted on • Updated on

Do you know what an algorithm is?

An diagram representing an algorithm

We can define an algorithm as a sequence of steps to execute some task.

OK, but what does that mean?

For example, we can say that an algorithm is a series of actions you make to: drive a car, cook a meal, watch your favorite TV show, and so on...

You don't have to worry If this concept is still abstract to you.
Let's take the latter example so that we can understand better.

Have you ever considered the sequence of actions you make, from when you open your web browser to when your favorite TV show begins?

Here is an example that probably just passed through your mind:

1-Open your browser;
2-Type the internet address of Netflix;
3-Press ENTER key on your keyboard;
4-Type your email and password;
5-Select your profile;
6-Select the TV Show Star Trek;
7-Watch the episode;

Do you already notice how this example follows the definition of algorithms that I said before?

We execute a "sequence of steps" (Open your browser, Type your email and password, etc.) to perform some task(Watch a Star Trek episode).

Awesome, right?

However, we can be more precise with this definition. Let's improve it a little to enhance the clarity about the subject that we are speaking of.

So, take a look at this concept of an algorithm:

An algorithm is a sequence of finite instructions or operations to resolve a well-defined computational problem.

See how we change the term "sequence of steps" to "sequence of finite instructions or operations" and "execute some task" to "resolve a computational problem"?

So here, the goal is solving a well-specified computational problem.

As we made before, here is an algorithm example, of this computational environment, in a pseudo-code format:

Goal:

Let us know whether John is listed in our list of friends.

Pseudo-code:

Let Name = name of the friend that is checked at the moment;

For each friend in the list of friends

Check if Name = "John".

If Name = "John", print "Found John!".

If Name != "John", compare the next friend.

If all names in the list differ from "John", print, "John is not on the list".

OK, now let's test this algorithm with the following set of friends:

["Julia", "John", "Marcus"]

First, it set Name = "Julia";
Then, it'll compare "Julia" with "John";
It is different, so check the next friend.

First, it set Name = "John";
Then, it'll compare "John" with "John";
Is equal! So it prints: "Found John!".

There we go. It works!

Now, let's try with another set of friends, but this time, John won't be on this list:

["Kirk", "McCoy", "Spock"]

First, it set Name = "Kirk";
Then, it'll compare "Kirk" with "John";
It is different, so check the next friend.

First, it set Name = "McCoy";
Then, it'll compare "McCoy" with "John";
It is different, so check the next friend.

First, it set Name = "Spock";
Then, it'll compare "Spock" with "John";
It is different, so check the next friend.

There's no friend left, so print, "John is not on the list".

Done, work again! Nice, right?

This algorithm has a linear behavior, which could be more efficient(more content in the next few days).

Nonetheless, it does the job. It solves our computational problem of determining whether John is included in our list.

Although, a computer might be unable to solve it if the input is extensive.

Now to make a more practical experiment, let's use a computer programming language to replicate and test this algorithm. We are using a list of 10 thousand words. None of them is John. Therefore, we expect our algorithm to print the sentence: "John is not on the list".

I will use the PHP programming language, which is a pretty easy and fantastic language to work with. However, with some adjustments, you can replicate this algorithm in any programming language you prefer.

Here is our code:

<?php

$names = array_fill(0, 10000, "Spock");

if (in_array("John", $names)) {
    print_r("Found John!");
} else {
    print_r("John is not on the list");
}

Enter fullscreen mode Exit fullscreen mode

In the first line, I'm creating a variable name "names". The is an array, which contains 10000 elements. And each piece has a value equivalent to the string "Spock".

Next, I use a PHP function named "in_array". That accepts two parameters. First, the element you want to search (our case, "John") and the list you want to look on at.

If my function in_array returns a truthy value, it found John in the list of names. My code prints the string: "Found John!"

On the contrary, it prints, "John is not on the list".

Once we run this code, we get this output:

John is not on the list%   
Enter fullscreen mode Exit fullscreen mode

Nice! And if we test this code with our list, which contains the string, John:

<?php

$names = ["Julia", "John", "Marcus"];

if (in_array("John", $names)) {
    print_r("Found John!");
} else {
    print_r("John is not on the list");
}

Enter fullscreen mode Exit fullscreen mode

We receive this output:

Found John!% 
Enter fullscreen mode Exit fullscreen mode

And there you go!

We can observe that changing the number of inputs doesn't change the behavior of our algorithm. It still is solving our computational problem.

There is much to cover about algorithms, but this was our first step!

At least we can say we understand the concept.

Top comments (0)