Data Structures and Algorithms - Part II
Tags: datastructures, algorithms, beginners, javascript
Published: 2022-11-22 18:03:47

**if** 
 you haven't already, i highly suggest that you check out part one, where i talked about data structures. Link for part one: https://dev.to/jonnynotbravo/data-structures-and-algorithms-4ni8

**else**
 Enjoy!

## What is an Algorithm? 

In simple terms, an algorithm is a set of instructions or steps for completing a task. Our daily life consists of algorithms. Morning and night routines, a recipe, and even the steps we do to tie our shoes are algorithms.  

![Tie shoe gif](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iix0h8oi0xi1dqgk44jt.gif)

## Algorithms in Programming
So what does tying a shoe has anything to do with programming? To tie our shoes, we must follow a set of steps. Likewise, to write a program, we have to write an algorithm. Generally speaking, almost any code that has zero or more defined input is executed with an algorithm. The process is as follows: 

![Algo Process](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gygyr5dx8qhntmf0kp3p.jpg)

An algorithm should have zero or more well-defined **inputs**. It should also have zero or more well-defined **outputs**. After the program runs, the algorithm must terminate. Let's look at an example:

```javascript
function add(num1, num2) {

   const sum = num1 + num2;

   return sum;
}
```



Here we have a function that takes two numbers as parameters and finds the sum of the two numbers. Here are some things to notice: 

- num1 & num2 - **the input values**

- a "sum" variable that does the addition operation - **the algorithm**

- the return value - **the output**


## The Importance of Algorithms 
The primary purpose of algorithms is to perform a task or solve a problem. Different people develop different algorithms for these problems, and some people will have better algorithms than others. So what makes an algorithm a "better" algorithm? Let's look at how these two codes below use two different algorithms to add two numbers. 

## Code A
```javascript

///Declare an empty array variable
const array = []

function add(num1, num2) {
  
  ///Push the sum of the two numbers into the array
  array.push(num1 + num2)
  //Convert the array to a string
  const arrayToString = array.join()
  //Convert the string to a number
  const stringToNumber = Number(arrayToString)

  //output
  return stringToNumber;

}
```

## Code B
```javascript
function add(num1, num2) {
  
  return num1 + num2;

}

```

It's evident that code A uses a complex algorithm to find the sum of the two numbers. It adds the numbers and pushes them into an array. Next, that array is converted to a string. Next, that string is converted to a number, and then we finally get the result. This algorithm works, and it's guaranteed that we get the correct answer. However, it is less efficient.

In programming, efficiency is the number of computational resources used by an algorithm. In simple terms, it's the number of steps the program takes to execute. The more steps and more complex the algorithm, the less efficient the algorithm is. 

On the other hand, Code B looks very easy to understand, and it takes the least amount of code to execute in terms of code complexity, time, and memory. 

## List of Algorithms
There are many algorithms in computer programming that help to develop great programs and thus will improve efficiency and quality. Here are some of the most familiar ones and the ones that are used almost on a daily basis. 

**Searching Algorithms**

- Linear Search
- Binary Search 

**Sorting Algorithms**

- Quick Sort
- Heap Sort
- Merge Sort
- Buble Sort
- Selection Sort
- Radix Sort


**Graph Algorithm**

- Dijkstra's Algorithm 
- Breadth-first search
- Depth-first search
- A* algorithm

## Conclusion

Algorithms and algorithmic thinking, or the ability to define clear steps to solve a problem, are crucial in many different fields. Even if we’re not conscious of it, we use algorithms and algorithmic thinking all the time. Algorithmic thinking allows us to break down problems and conceptualize solutions in terms of discrete steps. Being able to understand and implement an algorithm require us to practice structured thinking and reasoning abilities.


Thank you!

[Jonny](https://www.linkedin.com/in/jonnytilahun/)