What is an Algorithm ?
A set of rules/step for a computer program to accomplish a task.
Why we need to know different Algorithm ?
similar to DS, knowing different algorithms and choosing the better one provides the optimal processing of the software, again it makes the program more efficient when we use the right algorithm for the tasks.
- Imagine going to a library and finding a book, based on how the library is organized we need to choose the right steps arrive at our desired book
How to decide on which algo to choose?
Mainly two factors:
- Correctness - Which algorithm can achieve our desired result
- Efficiency
Types
Based on what they are used to do we can classify into below types
Simple Recursive algorithms
- Where Iteration is involved
Divide and Conquer algorithms
- Divides the problem into similar smaller sub problems to arrive at the solution by combining the subproblems solution
Dynamic Programming Algorithms
- Works on memoization, uses the old solution to arrive at new solution
Greedy Algorithms
- Works by taking the optimal solution at each step without worrying about the future outcomes
Brute Force Algorithms
- Tries everything until the solution is reached
Randomized Algorithms
- Uses a Random Number at least once during the computation to make a decision