# Introduction to Data Structure and Algorithms

In this tutorial, we will learn about Data Structures and Algorithms.

Data Structures are the programmatic method of storing data so that data can be used proficiently. Pretty much every endeavor application uses different types of data structures in either way.

## Why to learn Data Structure and Algorithms?

As applications are getting complex and data-rich, there are three normal issues that applications face nowadays.

Data Search − Consider an inventory of 1 million(106) items of a store. On the off chance that the application is to look through an item, it needs to look through an item in 1 million(106) items each time hindering the hunt. As data develops, the search will turn out to be slower.

Processor speed − Processor speed although being exceptionally high, falls limited if the data develops to billion records.

Multiple requests − As a huge number of clients can look through data at the same time on a web server, even the quick server fails while looking through the data.

To take care of the above-mentioned issues, data structures act as the rescue. data can be coordinated in a data structure so that all items may not be needed to be looked at, and the necessary data can be looked at immediately.

## Applications of Data Structure and Algorithms

Algorithm is a step-by-step strategy, which characterizes a set of directions to be executed in a specific request to get the desired output. Algorithms are for the most part made independent of basic languages, for example an algorithm can be actualized in more than one programming language.

From the data structure perspective, the following are some significant classifications of algorithms −

• Search − Algorithm to look through an item in a data structure.

• Sort − Algorithm to sort things in a specific order.

• Insert − Algorithm to insert thing in a data structure.

• Update − Algorithm to update an existing thing in a data structure.

• Delete − Algorithm to erase an existing item from a data structure.

The following computer problems can be solved using Data Structures −

• Fibonacci number series
• Knapsack problem
• Tower of Hanoi
• All pair shortest path by Floyd-Warshall
• Shortest path by Dijkstra
• Project scheduling

## Characteristics of an Algorithm

Not everything strategy can be called an algorithm. An algorithm ought to have the accompanying characteristics −

• Unambiguous − The algorithm ought to be clear and unambiguous. Every one of its steps (or phases) and their inputs/outputs ought to be clear and should prompt just one significance.

• Input − An algorithm ought to have at least 0 very much characterized inputs.

• Output − An algorithm ought to have at least 1 very much characterized outputs, and should coordinate the desired yield.

• Finiteness − Algorithms should end after a finite number of steps.

• Feasibility − Should be possible with the accessible resources.

• Independent − An algorithm ought to have bit by bit bearings, which ought to be independent of any programming code.

## Characteristics of a decent algorithm

1. Input and output ought to be characterized exactly.
2. Each progression in the algorithm ought to be clear and unambiguous.
3. Algorithms ought to be best among various approaches to tackle an issue.
4. An algorithm should exclude computer code. Instead, the algorithm ought to be written in such a way that it can be used in various programming languages.

## Examples Of Algorithms In Programming

Algorithm to add two numbers entered by the user

```Step 1: Start
Step 2: Declare variables num1, num2 and sum.
Step 3: Read values num1 and num2.
Step 4: Add num1 and num2 and assign the result to sum.
sum←num1+num2
Step 5: Display sum
Step 6: Stop```

## Find the largest number among three different numbers

```Step 1: Start
Step 2: Declare variables a,b and c.
Step 3: Read variables a,b and c.
Step 4: If a > b
If a > c
Display a is the largest number.
Else
Display c is the largest number.
Else
If b > c
Display b is the largest number.
Else
Display c is the greatest number.
Step 5: Stop```

Roots of a quadratic equation ax+ bx + c = 0

```Step 1: Start
Step 2: Declare variables a, b, c, D, x1, x2, rp and ip;
Step 3: Calculate discriminant
D ← b2-4ac
Step 4: If D ≥ 0
r1 ← (-b+√D)/2a
r2 ← (-b-√D)/2a
Display r1 and r2 as roots.
Else
Calculate real part and imaginary part
rp ← -b/2a
ip ← √(-D)/2a
Display rp+j(ip) and rp-j(ip) as roots
Step 5: Stop
```

## Factorial of a number entered by the user.

```Step 1: Start
Step 2: Declare variables n, factorial and i.
Step 3: Initialize variables
factorial ← 1
i ← 1
Step 4: Read value of n
Step 5: Repeat the steps until i = n
5.1: factorial ← factorial*i
5.2: i ← i+1
Step 6: Display factorial
Step 7: Stop```

## Check whether a number is a prime number or not

```Step 1: Start
Step 2: Declare variables n, i, flag.
Step 3: Initialize variables
flag ← 1
i ← 2
Step 4: Read n from the user.
Step 5: Repeat the steps until i=(n/2)
5.1 If remainder of n÷i equals 0
flag ← 0
Go to step 6
5.2 i ← i+1
Step 6: If flag = 0
Display n is not prime
else
Display n is prime
Step 7: Stop
```

## Find the Fibonacci series till term ≤ 1000.

```Step 1: Start
Step 2: Declare variables first_term,second_term and temp.
Step 3: Initialize variables first_term ← 0 second_term ← 1
Step 4: Display first_term and second_term
Step 5: Repeat the steps until second_term ≤ 1000
5.1: temp ← second_term
5.2: second_term ← second_term + first_term
5.3: first_term ← temp
5.4: Display second_term
Step 6: Stop```

Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.