# Algorithms Basics

An algorithm is the steps taken to solve a problem.

In the programming world, we refer to algorithms to represent codes that are capable of giving a proper solution. With the help of code, we instruct the computer so it can work accordingly. In this article, we are going to discuss the main types of data structure algorithms that help developers in solving complex problems.

Let's begin!

## Different Types of Data Structure Algorithms

There are 5 main types of algorithms we often implement in a data structure:

#### Searching

In searching, we try to make an algorithm that is capable of searching a specific element from the data structure.

#### Sorting

Sorting algorithms are capable of sorting the data structure elements in ascending and descending order.

#### Inserting

In the inserting algorithm, we try to write a code that can insert new elements in the data structure. The new element could be inserted randomly or specified to some specific location in the data structure.

#### Update

With update algorithms, we can update or edit the elements already present in the data structure.

#### Delete

Delete algorithms can delete specific or random elements from a data structure.

### How to Write an Algorithm?

As we know, an algorithm is an approach to solving a problem using steps, so there is no such standard to write an algorithm. However, we have provided some characteristics of a good algorithm that you can include while working on an algorithm. One of the main characteristics of an algorithm is independence, which means the algorithm you use should not apply to a specific programming language.

Further, before we code the algorithm in a programming language, we use pseudo code to write the algorithm, and pseudo-code must contain those statements that are common to all programming languages, such as loops, if..else statements, class or structure, assignment operators, etc.

For example, let’s write an algorithm to multiply 2 numbers, X and Y: >>>

• Ask the user to enter a number and store it in a variable X >>>
• Ask the user to enter another integer and store it in a value Y >>>
• Declare a variable Z >>>
• Use * operator to multiple X & Y, X*Y >>>
• Assign X*Y to Z >>> Z = X*Y >>>
• Print the value of Z, print Z

### Characteristics of a Good Algorithm

There are many procedures to solve a problem. However, a good and efficient algorithm has some characteristics to follow. Take a look:

#### Unambiguous

The algorithm must be unambiguous and lead to a proper result. Every step must have a proper meaning. In the algorithm, there should not be a single executable statement that has nothing to do with the result.

#### Input

Input depends on the type of problem we are solving, so if the problem demands user-defined input, the algorithm must have input in it.

#### Finiteness

There must be a limited and finite number of steps in an algorithm. The last step must be capable of giving an appropriate answer.

#### Feasible

The algorithm must be feasible, which means it should be easily implemented using code.

#### Independent

An algorithm must be independent, which means all the steps mentioned in the algorithm must be applied to each programming language.

#### Output

The algorithm must be capable of giving the appropriate result.

### How to Perform Algorithm Analysis?

We analyze an algorithm to check its efficiency. There are two phases where we can analyze an algorithm - after and before implementation, also known as Priori and Posterior Analysis.

#### Priori Analysis

In the priori analysis, we analyze an algorithm theoretically. We read the algorithm and try to analyze theoretically how efficient the algorithm is and how it would solve the problem. However, as it is analyzed by humans, there is a chance that they would not perfectly estimate the efficiency of the algorithm.

#### Posterior Analysis

In posterior analysis, we first implement the algorithm using a specific programming language and then check its live efficiency. The posterior analysis provides the accurate efficiency of an algorithm. It provides the proper and correct stats of algorithm performance on the machine, memory, and processors or CPU.

### What is Algorithm Complexity?

Complexity is a measure to analyze an algorithm. For example, if two algorithms are capable of performing the same task, we analyze their complexity to know which is more efficient. There are two major complexities we often consider when we compare two algorithms:

• Time Complexity
• Space Complexity

#### Time Complexity

The time complexity of an algorithm deals with the amount of time or steps taken to obtain the appropriate answer. In computer science, we represent the time complexity of an algorithm or program using a mathematical function statement O(n) , where n represents the total number of executable steps taken by the algorithm to get the result.

The algorithm which has less time complexity would be considered as a better algorithm.

#### Space Complexity

The space complexity of an algorithm deals with the amount of memory space occupied by the algorithm. If two algorithms have the same time complexity, then the algorithm occupying less space in the memory would be considered a better algorithm.

## Conclusion

Data algorithms are an essential part of data structures. They play a crucial role in solving various computational problems efficiently. Further, it is essential to choose the right data structure for a problem as it leads to better execution and less memory usage. They help in data analysis, data visualization, data transformation, and automating processes, to name a few.

We hope that the above-mentioned information helped you understand the topic clearly.

Keep learning!

People are also reading: