DSA is incomplete without algorithms. Let's look at a general overview of what algorithms are and why we need them.
What are Algorithms?
An algorithm is a finite set of instructions, executed in a specific order to solve a particular problem. We apply algorithms in our daily lives, knowingly or unknowingly. Like making a particular dish, or the process of washing clothes, they all have a defined start, a step-by-step procedure and an end. From a technical perspective, we can create algorithms to solve a problem in an effective way.
For example: to calculate the product of two numbers, we
start by creating a function
declare variables to store both numbers and the answer
read values from the user
save the product in a 3rd variable
display the product
end by closing the function
This can also be displayed in a quick and visually elegant way using flowcharts.
Characteristics of an Algorithm
The following are some basic characteristics of algorithms:
Input: An algorithm might have none or some inputs.
Output: An algorithm gives one or more than one output.
Unambiguity: Algorithms are clear and simple
Effectiveness: Algorithms solve space and time complexities very effectively
Well defined: Algorithms are straightforward set of instructions and thus, are well defined.
Finiteness- Algorithms terminate after a finite number of sets.
Types of Algorithms
Divide and conquer algorithms – Divide the problem into smaller subproblems of the same type; solve those smaller problems; and then integrate those solutions to solve the original problem.
Searching algorithms- Search through a data structure in a particular order to find a particular value.
Sorting algorithms- Arrange a collection of data in a particular order. like for numbers it can be ascending or descending.
Randomized algorithms – To solve the problem, utilise a random number at least once during the computation.
Greedy algorithms – In order to find the optimal solution for the entire problem, find the optimal solution at the local level.
Recursive algorithms – Repeat a certain set of instructions, each time changing the number of instances thus finding your way to the solution.
Backtracking algorithms – explore all possible solutions by building step by step and undoing some steps if they lead to a dead end.
Dynamic programming algorithms – a complex problem can be broken down into a number of smaller, simpler subproblems. Each of these subproblems can then be solved just once, with the solutions being stored for later use rather than being recalculated.