## DEV Community π©βπ»π¨βπ» is a community of 967,911 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Leonardo Cunha

Posted on

# Do you know what an algorithm is?

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

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

Here is an example, that probably just pass through your mind:

3-Press ENTER key on your keyboard;
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) with the goal to execute some task(Watch a Star Trek episode).

Awesome, right?

However, when we talk about IT, we can be more precise with this definition. Improve it a little, so we can 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, on finite time, to resolve a 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, we are being cautious with the time variable and the goal is solving a well-specified computational problem.

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

Goal:

Inform us that John is listed or not, 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 are different from "John", print "John is not in 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";
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 inside this list:

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

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

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

First, it set Name = "Spock";
Then, it'll compare "Spock" with "John";
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 isn't very efficient(more content is the next few days).

But, apparently do its job. Solving your computational problem, which in this case is to determine if John is included or not in our list.

Although, maybe a computer isn't capable to resolve, if the input is very large.

To test this, let's use a computer programming language to replicate this algorithm and test it. Using a list of 10 thousand words. Which none of them are John. Therefore, we expect our algorithm to print the sentence: "John is not in the list".

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

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 in the list");
}

``````

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

In the second line, I use a function from the PHP, named "in_array". That's search for a value(our case "John"),inside an array(our case, \$names).

If my function in_array returns a true value, which means it founds John in the list of names, my code prints the string: "Found John!"

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

Once we run this code, we get this output:

``````John is not in the list%
``````

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 in the list");
}

``````

``````Found John!%
``````

And there you go!

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

There is a lot to cover about algorithms, but this was just our first step!

At least we can say we understand the concept.