Algorithms for beginners
Tags: algorithms, beginners
Published: 2023-10-07 04:40:02

## What is algorithms?
What image comes to mind when you hear the word "algorithm"? In my case, "Hmmmm, it sounds kind of difficult".

Even though it sounds difficult, algorithms are very familiar to us in our daily lives. A series of "steps" that describes how to solve a certain problem or complete a certain goal is called an algorithm.

The name "algorithm" is said to come from the name of the 9th century mathematician al-Huwarizmi in present-day Baghdad, Iraq. Algorithms are a concept with a longer history than computers and programs.

Now, let us consider an algorithm with a familiar example. For instance, shopping is one algorithm.　

If you have a list of items to buy one bottle of milk, one bag of potatoes, and a dozen egg, there are multiple ways to find the items in any order. Such a case in which the items can be processed in any order can be called a list-type data structure. Also, an algorithm whose behaviour depends on certain conditions, such as buying another bottle of milk if it costs less than $3, is called a choice structure algorithm. Thus, in fact, algorithms are hidden in our daily life.

## Algorithms and Flowcharts
A flowchart is a diagrammatic technique for designing the flow of a program.

Flowcharts allow for a holistic view of information, clarifying and understanding the process. It is a useful tool that allows even complex information to be presented in a visually clear manner.

Here are some basic types of control structures.

### Sequential
- Executes processes in a top-to-bottom order.

Let's take shopping as an example and represent it in a flowchart and code. When you go shopping, first thing to do is leave home. When you arrive at the grocery store, you search for products, purchase them, and return home. This order cannot be changed. They are executed in order from top to bottom.

![Sequential description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qrkvnv2nu9xqd1oyo4ak.png)

```javascript

leaveHouse();
searchItems();
makePayment();
goBackHome();

```


### Branching
- Select one of two or more alternative paths. 

Suppose you go shopping and buy some milk. You want milk but are concerned about the price. Therefore, you decide on a budget. If the price of a bottle of milk is less than $3, you buy two more bottles of milk. However, if the price is higher than $3, you decide to buy only one bottle. This process can be represented as follows.

![Branching description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u74msbjzmocigw6a1udo.png)

```javascript
searchMilk();
let milkNum = 0;

if (milkPrice < 3){
    milkNum = 2;
}else{
    milkNum = 1;
}

makePayment();

```


### Iterative
- The same process is repeated until the end condition of the iteration is met.

You are now going to cook with potatoes. There are 10 potatoes in a bag. If you use one whole potato, it is too big. So you decide to cut the potatoes in half. The action of cutting the potatoes is repeated until there are no more potatoes in the bag. This can be expressed as follows.

![Iterative description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9goskk2cfxpsehh5pru8.png)

```javascript
let potatoNum = 10

while (potatoNum > 0) {
  cutPotato();
  // A function to cut a potatoes in half 
  potatoNum--;
}

```

---

Algorithms are an important tool for programs to solve problems. Even algorithms that seem complex may be better understood by drawing a flowchart!