Data Structure and Algorithms 101.
Published: 2022-06-21 16:49:45

What is Data Structure?

**Data Structures** allows you to organize your data in such a way it enables you to store collections of data, relate them and perform operations on them accordingly.

**Types of Data Structures**

**1.****_Built-in/Primitive data structures_**: They store data of only one type. They include:

a)**_List_**: Lists can have heterogeneous data types in them. They can be mutable that is they can be changed. We use Square brackets to create lists.

```
Example:
a=['apple','banana','pear']
#We can add another fruit into the lists through
a.append(orange)
print(a)
#When we run this in any IDE; a=['apple','banana','pear','orange']
```
b)**_Tuples_** : They are not mutable. They are faster than lists. We use regular brackets to create tuples.

```
b=(3,4,5)
print(b)
solution:(3,4,5)

```
c)**_Dictionary_** : They hold key value pairs. We use curly brackets to initialize dictionaries. Dictionaries are mutable.

```
c={1:'python',2:'java'}
print(c)
solution: {1:'python',2:'java'}
```
d)**_Sets_**: These are unordered collection of unique elements. We use curly brackets to initialize sets. Sets are mutable.

```
d = {2,3,4,2}
print(d)
solution: {2,3,4}
#This is because sets do not repeat same elements inside it.
```
**2. User-defined/derived/non-primitive data Structures**: They are data of more than one type. They include;

a)**_Stacks_**: The data that is entered last will be the first one to be accessed. They are built using array structure and have operations namely **_push_** which adds an element to the collection and _**pop**_ which removes elements from a collection. They are used in applications such as recursive programming, reversing words etc.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tfqyigi3hd05ebvys3yz.jpeg)
b)_**Queues**_: This is a linear data structure based on the principle of first in first out.  They are also built using an array structure and operations can be done on them. They are used in the application of traffic congestion management and in operating systems for job scheduling.

![Queues](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/372yn1km9lgx2qjsi5hq.jpeg)
c)_**Trees**_: This is a non-linear data structure that has roots and nodes. Roots are where the data originates while nodes are any point where data is accessible to us. The node to proceed is the parent and the latter is the child. Trees are widely used in Html pages and are efficient for searching purposes.
![](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ic7e7kp9lnqc3etejit7.jpeg)
d)_**Linked lists**_: These are liner data structures t are not stored concurrently but are linked together through pointers. The node of a list composes of data and a pointer. They are used in image viewing applications and music player applications.
![](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bkqdvfs48kf3uuyew1ap.jpeg)
e)_**Graphs**_: They are used to store data collection of points and edges. Many applications such as google maps and uber use graphs to find the least path in order to maximize profits.
![](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t976peu72ld4e1y7m9xx.jpeg)
f)_**Hashmaps**_: They are the same as what directions are in python. They are used to implement applications such as phonebooks and populate data.
![](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nyhs6nu3k8z2fprvz4xj.jpeg)
What are Algorithms?

**Algorithms** are rules or instructions which are formulated in a finite sequential order to solve problems. They provide pseudocode for problems. 

_Important things to note when writing algorithms._
   -Figure out what is the exact problem
   -Determine where you need to start
   -Determine where you need to stop
   -Formulate the intermediate steps
   -Review your steps

_Elements of a good Algorithm_
   -Steps should be finite, clear, and understandable
   -Clear and precise description of inputs and outputs
   -The algorithm should be flexible
   -The steps should make use of general programming fundamentals

**Algorithm classes**
There are three types;
   i)_**Divide and Conquer**_ - Divides the problem into subparts and solves the subparts separately.
   ii)_**Dynamic programming**_ - Divides the problem into subparts remembers the results of the sub-parts and applies them to similar ones.
   iii)_**Greedy Algorithms**_ - Involves taking the easiest step while solving a problem without worrying about the complexity of the future steps.

**Tree Traversal Algorithms**
There are three types of tree traversals:
   i)_**In-order traversal**_: refers to visiting the left nodes followed by the root and then right nodes.
![](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eanhzvvuq10p523y1jv2.jpeg)
  ii)_**Pre-ordered traversal**_: refers to visiting the root nodes followed by the left nodes and then right nodes.
![](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3p8r8pqctfz6a5ii6uxm.jpeg)
  iii)_**Post-order traversal**_: refers to visiting the left nodes followed by the right nodes and then the root node.
![](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bm36brb5ilgezovl84qv.jpeg)
**Sorting Algorithms**
It is used to sort data in some given order. It can be classified into five steps.
   i)_**Merge sort**_: The given list is first divided into smaller lists and compares to adjacent lists and records in the desired sequence. 
   ii)_**Bubble sort**_: is a comparison algorithm that first compares and sorts adjacent elements if they are not in the specified order.
   iii)_**Insertion sort**_: Picks one element of the given list at a time and places it at the exact position where it is to be placed.
   iv)_**Selection sort**_: Picks one element of a given list at a time and places it at the exact spot where it is placed.
   v)_**Shell sort**_: allows you to sort elements apart from each other.

**Searching Algorithms**
  Searching algorithms are used to search for or fetch elements present in some given dataset. There are many types of search algorithms; Linear search, Binary search, Exponential search, and interpolation search.

**Algorithm analysis**
a)_**A prior analysis**_: where all factors are assumed to be constant and do affect the implementation.
b)_**A posterior analysis**_: The actual values like time complexity or execution on time of an algorithm, and space complexity are gathered.


**Thank you for taking your time to read 🤗🥳Happy learning😇🌟.**