## Algorithm

A procedure having well-defined steps for solving a particular problem is called an algorithm. Or in other words, an algorithm can be defined as a finite set of logic or instructions, written in order to accomplish a certain predefined task. Being just a solution (logic) of a problem, and not the complete program or code, it can be represented either as an informal description using a Flowchart or Pseudocode.

### Major categories of algorithms:

**Sort:**This category of algorithms is developed for sorting the items in a certain order.**Search:**This category of algorithms is developed for searching the items inside a data structure.**Delete:**This category of algorithms is developed for deleting the existing element from the data structure.**Insert:**This category of algorithms is developed for inserting an item inside a data structure.**Update:**This category of algorithms is developed for updating the existing element inside a data structure.

### The properties based on which the performance of an algorithm is measured:

**Time complexity:**The way of representing the amount of time needed by a program to run to completion is defined as time complexity.**Space complexity:**The amount of memory space required by an algorithm, during the course of its execution is defined as space complexity. In case, when limited memory is available and for the multi-user system, space complexity is needed.

### Each algorithm must have:

**Specification:**It specifies the description of the computational procedure.**Pre-conditions:**It specifies the condition(s) on input.**Body of the Algorithm:**It specifies a sequence of clear and unambiguous instructions.**Post-conditions:**It specifies the condition(s) on output.

### Example: Design an algorithm to add the two numbers a and b and display the result in c.

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

**Alternative algorithm:**

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

### Characteristics of an Algorithm:

The must-follow characteristics of an algorithm are:

**Input:**0 or well-defined inputs are a must for an algorithm.**Output:**1 or well-defined outputs, matching with the desired output, are a must for an algorithm.**Feasibility:**The termination of an algorithm after the finite number of steps is also a must.**Independent:**The step-by-step directions, independent of any programming code, are a must for an algorithm.**Unambiguous:**An unambiguous and clear algorithm is strictly recommended. It means that the steps and input/outputs of the algorithm must be clear and directed to only one meaning.