In this tutorial, we are going to learn the basics of Data Structures and Algorithms.

** Data structure **is a way of organising data into the memory so we can use it efficiently.

**Algorithm** is step-by-step solution to solve a particular problem. We can say that its a set of well defined instructions to solve a particular problem.

### Examples Of Algorithms

**Algorithm to subtract two numbers **

```
Step 1: Start
Step 2: Declare variables no1, no2 and substraction.
Step 3: Read the values of no1 and no2.
Step 4: Subtract no1 and no2 and assign the result to substraction.
substraction=no1-no2
Step 5: Display substraction
Step 6: End
```

**Algorithm to find square of a numbers**

```
Step 1: Start
Step 2: Declare two variables num and square.
Step 3: Read the value of num.
Step 4: Multiply num with num and assign the result to square.
square = num*num
Step 5: Display square
Step 6: End
```

### Why to learn Data Structures & Algorithms?

If you are good at DSA(Data Structures And Algorithms) and you know how to solve a particular problem efficiently, the companies like FAANG (**F**acebook, **A**mazon, **A**pple, **N**etflix, **G**oogle) are hiring programmers who are good at DSA.

### What are Algorithms?

Algorithm is nothing but a solution to a problem. One problem may have multiple algorithms.

For ex.

**Problem: Find the sum of first n natural numbers**

**Algorithm 1:**

```
Step 1: Start
Step 2: Declare variables i, num and sum.
Step 3: Read the value of num.
Step 4: Set i=1 and sum=0
Step 5: Repeat steps 5 to 7 till i is less than equal to num
Step 6: sum = sum + i
Step 7: i = i+1
Step 8: Display sum
Step 9: End
```

We can implement this algorithm in any Programming Language, let’s implement above algorithm in** C Programming Language.**

```
#include<stdio.h>
int main() {
int num, i, sum; //declare i, num and sum
scanf("%d", &num); //Read the value of num.
i = 1;
sum = 0; //Set i=1 and sum=0
//Repeat loop till i is less than equal to num and increment value of i by 1
for(i=1; i<=num; i++){
sum = sum + i;
}
//Display sum
printf("Sum of first %d numbers is %d .", num, sum);
}
```

If you enter smaller values then its okay. But above algorithm is not suitable for larger values. Let’s see another algorithm.

**Algorithm 2:**

```
Step 1: Start
Step 2: Declare variables num and sum.
Step 3: Read the value of num.
Step 4: Set sum=0
Step 5: sum = (num*(num+1))/2
Step 6: Display sum
Step 7: End
```

Here we know formula for **summation of first N natural numbers** i.e.

**n*(n+1)/2.**

```
#include<stdio.h>
int main() {
int num, sum; //declare num and sum
scanf("%d", &num); //Read the value of num.
sum = 0; //Set sum=0
//use formula
sum = num*(num+1)/2;
//Display sum
printf("Sum of first %d numbers is %d .", num, sum);
}
```

A software/application consists of many algorithms, optimising any one of them leads to good performance.