Â
An algorithm is a way of taking a large series of steps and breaking them down into smaller steps that can be executed more quickly. An algorithm is an instruction set that a computer evaluates based on how well it matches a set of criteria. There is a lot of information out there about algorithms. Even the Wikipedia article on algorithms has thousands of words devoted to it. Algorithms are useful because they often work more quickly and easily than humans.
Â
Â
Let’s consider a real-world example of an algorithm for an activity that does not necessitate the use of a computer: creating a grilled cheese sandwich.
Â
When making grilled cheese, you usually follow a set of steps to achieve the desired result. You'll need bread first. After that, butter the bread. After that, the cheese is added. Cooking the grilled cheese is the final stage.
Simple, right? The same thing happens in the programming world as well. There are list of steps which takes an input and produces an output.Â
Â
Let's look at it in more details.
Â
Â
Â
Step 1: Get a detailed description of the issue. This stage is far more challenging than it appears...
Â
Step 2: Examine the issue...
Â
Step 3: Construct a high-level algorithm.
Â
Step 4: Fine-tune the algorithm by including more information...
Â
Step 5: Go over the algorithm again.
Â
Let’s code a simple algorithm to print the numbers 1-10 in Java.
Â
public class Exercise10 {
    public static void main(String[] args)
    {    Â
   int i;
   System.out.println ("The first 10 natural numbers are:\n");
   for (i=1;i<=10;i++)
   {     Â
    System.out.println (i);
   }
   System.out.println ("\n");
  }
}
Â
Output:
The first 10 natural numbers are:Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â
1Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â
2Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â
3Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â
4Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â
5Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â
6Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â
7Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â
8Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â
9Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â
10
Â
This was a simple example. There can be lots of different types of algorithms,
Â
Â
Â
Here is a list of the types of Algorithms to begin with:
Let's look at them in details.
Â
BRUTE FORCE ALGORITHM
This is the simplest and most fundamental sort of algorithm. A Brute Force Algorithm is the most basic solution to a problem or the first solution that comes to mind when we perceive the problem. Technically, it's the same as iterating through all of the possible solutions to the problem.
Â
GREEDY ALGORITHM
Greedy is an algorithmic paradigm that assembles a solution piece by piece, constantly opting for the next component that provides the most evident and immediate benefit. Greedy is best suited to problems when picking locally optimal also leads to a global solution.
Â
RECURSIVE ALGORITHM
A recursive algorithm is one that calls itself with "smaller (or simpler)" input values and gets the result for the current input by applying basic operations to the smaller (or simpler) input's returned value.
Â
BACKTRACKING ALGORITHM
Backtracking is an algorithmic strategy for recursively solving problems by attempting to develop a solution progressively, one piece at a time, and discarding any solutions that do not satisfy the problem's criteria at any point in time.
Â
DIVIDE AND CONQUER ALGORITHM
This technique can be broken down into three sections:
Â
DYNAMIC PROGRAMMING ALGORITHM
The goal is to simply save the results of sub-problems so that we don't have to redo them later. The temporal complexity of this simple optimization is reduced from exponential to polynomial.
Â
RANDOMIZED ALGORITHM
A randomized algorithm refers to an algorithm that uses random integers to determine what to do next at any point in its logic. In Randomized Quick Sort, for example, we choose the next pivot using a random integer.
Â
Â
Â
Implementing an algorithm helps you understand its logic and can be used in other problem-solving situations. If you know your algorithm, then you can write an efficient program.