Select Your Favourite
Category And Start Learning.

Algorithms in

JAVA

What Are Algorithms?

 

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.

Image

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.

 

 

Method for Creating Algorithms

 

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,

 

 

Types Of Algorithms

 

Here is a list of the types of Algorithms to begin with:

  1. Brute Force Algorithm
  2. Greedy Algorithm
  3. Recursive Algorithm
  4. Backtracking Algorithm
  5. Divide & Conquer Algorithm
  6. Dynamic Programming Algorithm
  7. Randomized Algorithm
Image

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:

  1. Divide: This includes breaking the problem down into smaller parts.
  2. Conquer: Successively solve sub-problems until they're all solved.
  3. Combine: Combine the sub-issues to achieve the overall problem's final answer.

 

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.

Author
AuthorPriyanka Dutta
Priyanka graduated with B.Tech in Electronics & Communication Engineering. She like organising data so much that building dashboards and models feel rewarding to her. She also likes to write about her work.

Follow us on your favourite social platform

Share with friends