What is an Algorithm in coding?

Computer programs, no matter how big or small, are aimed to solve some problem. As your intuition would suggest, a problem is a scenario that needs to be addressed or a task to be completed. In the Computer Science world, you can think of it in terms of input and output. A problem definition should not restrict you from how the problem is to be solved.

The algorithm is a set of instructions or processes followed to solve a particular problem that can transform an input into a specific output through those steps or processes. One or more algorithms can solve a problem. The pros of finding more than one solution are that one solution could be more efficient than the other for a certain set of input.

For example, one sorting algorithm might be best for sorting a restrictive set of integers. At the same time, another might lead in terms of efficiency for an extensive collection of integers, and a third might be best for sorting strings.

Properties of an Algorithm

By definition, an algorithm should have a few properties.

  1. An algorithm must have some data (input) to process.
  2. An algorithm may or may not produce any output to display on the screen. Still, it must compute the desired result for each input because every algorithm implements some processing on data to produce some result (even if that means system crash). The desired property here is whether a given algorithm performs as intended.
  3. It must consist of clear steps or instructions that are doable in a finite amount of time. There shouldn’t be any confusion over which step should be taken next. Often algorithms use selection statements like if, else, or switch to control the flow of processing.
  4. It must consist of a finite number of steps. It makes sense. Isn’t it? If an algorithm is made of an infinite number of steps, we could never hope to implement it or get an output from it.
  5. It must have a termination point. Infinite loops are not desired in computer programs.

How are Algorithms written?

An algorithm can be written as pseudocode.

Pseudocode is written in plain English-like format so that humans could understand it. Often programmers adopt the paper-pencil approach to write down the algorithms and then implement these into a computer program.

Pseudocode is an informal way of writing down an algorithm. And there is no right or wrong way of writing pseudocode, as it is not executable and intended only for humans.

Pseudo-codes are independent of any programming language. For example, in pseudocode, you can write ‘display output’ to print output. It can be translated into System.out.printlin (Java), print (Python), cout (C++)Or any other programming language of choice.
There is no right or wrong way of writing pseudocode, as it is not executable and intended only for humans.

Example of Algorithm in pseudocode

Here is an example of a pseudocode that adds two integers given by the user.

Step 1: Start

Step 2: Declare two variables a, b to hold the user’s input

Step 3: Declare a variable sum to hold the result of the addition

Step 4: Get the input from the user by displaying the prompt message
Display “Enter the first number:”
a <- user input
Display “Enter the first number:”
b <- user input

Step 5: Add variables a and b. And assign the result to a variable called sum.
Sum <- a + b

Step 6: Display sum

Step 7: Stop

 

As an assignment, think of an algorithm that takes three integers as input and displays the largest number.

Leave a Reply

Your email address will not be published. Required fields are marked *