How to write the Algorithm step by step?

A sequential solution to a problem written in human language.

  • The algorithm is the first step of the solution process after the analysis of the problem, programmers write the algorithm of that problem.
  • An algorithm is a finite sequence of instructions, logic, and explicit step by step procedures for solving a problem.
  • The number of instructions should be finite.
  • Order is important when it comes to instructions.

Rules for Write the Algorithm Step by Step

You must know how to solve a problem before you can write an algorithm. The solution must make sense.

write the algorithm step by step
  • In order to solve a problem, you must identify all inputs and outputs.
  • Add the necessary data and constants.
  • Steps in the computation.
  • You can print / display all the results.

Qualities of a Good Algorithm

  • Both input and output must be clearly defined.
  • Each step in an algorithm should be clear and unambiguous.
  • In most cases, the algorithm is the best way to solve a problem.
  • Program code is not part of an algorithm.
  • An algorithm should be written so that it can be used in other programming languages.

Where Do We Use Algorithm?

  • To find the best route to take (Google Map).
  • (Chess) Making games that can defeat us in them.
  • We can buy some products online and pay for them while sitting in our home and many more (almost everywhere).

Example to write the Algorithm step by step

Live Example 1:

Write an algorithm To make a coffee.

Steps Instructions
Step 1 Take a pot, add the proper amount of water.
Step 2 Place the pan on a gas stove and lights it.
Step 3 As soon as it boils, add the coffee powder.
Step 4 Add enough sugar and milk to taste.
Step 5 Put it in the cup and drink it.

Example 2:

Write an algorithm to add two numbers entered by the user.

StepsInstructions
Step 1Start.
Step 2 Define num1, num2, and sum.
Step 3 Ask the user to enter two numbers.
Step 4We will read values num1 and num2.
Step 5The sum will be calculated once num1 and num2 are added.
sum ← num1 + num2
Step 6Display sum.
Step 7Stop.

Algorithm Patterns

  • Following are three major patterns of algorithms:
    1. Sequential
      • The steps describe how each step occurs.
      • For Example:
        • Create an algorithm to find the sum of any two numbers.
        • Write an algorithm to find the volume of a cylinder.
    2. Conditional
      • An instruction may be divided based on more than one condition.
      • For Example:
        • Write an algorithm to check whether a given number is even or odd.
        • Write an algorithm to find out a voter’s eligibility based on her/his age.
    3. Iteration
      • There is more than one repetition of the same set of instructions.
      • For Example:
        • Write an algorithm to print all the even numbers between 1 to 10.
        • Write an algorithm to print all the odd numbers between 1 to 10.

Example 3:

Write an algorithm to find the largest among three numbers.

StepsInstructions
Step 1Start.
Step 2Declare variables a, b and c.
Step 3Read values a, b and c.
Step 4If a>b
          If a>c
                  Display a is the largest number.
          Else
                Display c is the largest number.
Else
          If b>c
                   Display b is the largest number.
          Else
                Display c is the largest number.
Step 5Stop

Types of Algorithm

Divide and Conquer Algorithm

  • The divide and conquer algorithm works by repeatedly breaking down a problem into two or more smaller problems of the same or related type until the problems are simple enough to solve directly.
  • For Example
    • Merge Sort
    • Quick Sort
    • Binary Search

Backtracking Algorithm

  • Backtracking is a method to find a solution to a problem step by step.
  • It approaches problems recursively and tries to solve one piece of the problem at a time in order to find its solution.
  • If one of the solutions fails, we discard it and try another one.
  • In other words, a backtracking algorithm solves a sub problem and, if it fails to do so, undoes the last step and tries again to solve the problem.
  • For Example
    • N Queen Problem

Brute Force Algorithm

  • There are many algorithms in this concept, but this is one of the simplest.
  • An algorithm that uses brute force iterates through all the possible solutions to find at least one that can solve the problem.
  • For Example
    • Selection Sort

Greedy Algorithm

  • These algorithms solve optimization problems.
  • By applying this algorithm, we can derive a local optimum solution (without considering any implications in the future) and then hope to find the global optimum solution.
  • The method does not guarantee that we will be able to find an optimal solution.
  • For Example
    • Huffman Coding and Diijkstra’s Algorithm

Dynamic Programming Algorithm

  • In this technique, the main focus is on the speed of execution rather than the amount of memory used.
  • In other words, a dynamic programming algorithm solves complex problems by breaking them into simple subprograms, and then it solves each of them once and then stores them for a future case.
  • As a result of these algorithms, they will use the results of previous runs to find new ones.
  • For Example
    • Fibonacci Series

Recursive Algorithm

  • An algorithm that calls itself until it solves the problem.
  • For Example
    • Tower of Hanoi

Conclusion

If you liked this article, you may also be interested in these topics: