DEV Community

Cover image for Introduction to Algorithms
raVaN-DEV
raVaN-DEV

Posted on • Updated on • Originally published at totheinnovation.com

Introduction to Algorithms

What are algorithms?

An Algorithm a step-by-step procedure which defines a set of instructions to be executed in a certain order to get the desired output.

In other words, algorithms are series of steps or rules for solving a computational problem.

Alt Text

Example Code:

if (clock.getTime() == 7am){
coffeeMaker.boilWater();
if (water.isBoiled()){
coffeeMaker.addCoffee();
coffeeMaker.pourCoffee();
}
}
else{
coffeeMaker.doNothing();
}

A good algorithms is like using right and efficient tool in the workshop.

Example: Using wrong algorithms mean cutting paper with a saw or trying to cut a piece of plywood with knife.

Categories of Algorithms

1.Search – Algorithm to search an item.
2.Sort – Algorithm to sort items in a certain order.
3.Insert – Algorithm to insert item.
4.Update – Algorithm to update an existing item.
5.Delete – Algorithm to delete an existing item.

Characteristics of an Algorithm

Not all procedure can be called an algorithm.

An algorithm should have the following characteristics:

1.Unambiguous – Each of its step and their inputs/outputs should be clear and must lead to only one meaning
2.Input – Should have 0 or more well-defined inputs
3.Output – should have 1 or more desirable well-defined outputs.
4.Finiteness – must terminate after a finite number of steps.
5.Feasibility – Should be feasible with the available resources.
6.Independent – Should have step-by-step directions and independent of any programming language.

How to Write an Algorithm?

There is no well-defined standard for writing algorithms. Algorithms are never written in support of a particular programming code.

We write algorithms in a step-by-step manner. Algorithms writing is a process and is executed after the problem domain is well-defined.

Example

Design an algorithm to add two numbers and display the result.

step 1 - START
step 2 - declare three integers a, b &c
step 3 - define values of a & b
step 4 - add values of a & b
step 5 - store output of step 4 to c
step 6 - print c
step 7 - STOP

Algorithms tell the programmers how to code the program. Alternatively, the algorithm can be written as :

step 1 - START ADD
step 2 - get values of a & b
step 3 - c ← a + b
step 4 - display c
step 5 - STOP

In design and analysis of algorithms, usually, the second method is used to describe an algorithm.

We design algorithms to get a solution to a given problem. A problem can be solved in more than one ways.

[deleted user] image

[Deleted User]

Visit my website

Top comments (0)