Computer Algorithm

Algorithm is the set of pre defined steps in order to solve or perform a given task. This can be found anywhere from actions we perform on daily basis as making coffee, solving rubic cubes to complex computer programs like Google search, social apps and E-commerce apps.

Everything starting from brushing your teeth in the morning to your bed time routine in the night involves the use of Algorithm.

Formulaes used in mathematics and programs written in different languages to reach the desired output fom a given input is also an Algorithm.

Algorithm can be written in many ways such as “plain english, flowcharts, programming languages etc.”

Algorithm:

For Examples let us see an Algorithm to find the greatest of three numbers.

Start
Input three numbers A, B & C.
Check IF A>B 
IF Yes 
{Check IF A>C
IF Yes 
Output A is greatest
IF No 
Output C is gratest }
IF No 
{Check IF B>C
IF Yes 
Output B is greatest 
IF No
Output C Is greatest }
Stop

Flowchart of the above algorithm-

Characteristics of Algorithm

Well defined input

An Algorithm must have at least one well defined input to start with and find the result.

Well difined output

There should always be an output of an Algorithm.

Finiteness

There should be the finite number of steps for solving a given problem in an Algorithm. An Algorithm may be complex and lengthy but it must have to stop at the end.

Language Independent

Algorithm must be independent of any language. A good Algorithm will be equally effective in any language natural or programming.

Unambiguous

Instructions used in the Algorithm should be straightforward and clear to understand and execute.

Types of Algorithm

Bruteforce Algorithm

This is the most used algorithm to solve a given problem. This algorithm is mostly used by hackers to crack passwords and it is an effective one. So make sure to make lengthy and complex passwords to protect from such attacks.

Example:

Imagine you have a padlock with 4 digit numberlock and you forgot the numbers of lock, than you turn each digit counter through 0 to 9 till the lock opens. This process to try all the combinations of four digit nuber is called bruteforce method (Algorithm).

Greedy Algorithm

The principle of Greedy algorithm is to select the locally best result to obtain the globally best result. In simple words choosing the best of available options (at present) each time untill the final result is obtained. The present selection is locally (presently) best but may or may not be globally (final result). This algorithm is inclined to select the most advantageous option at each step

Pros: Easy to use and describe.

Cons: Not optimal for every problem.

Example:

Problem- To find the best possible combination of tokens to make a sum of 37, if the face value of tokens are 5, 10, 2 & 1.

Sol-

step1 selct biggest value untill it is >37.
Step2 Sum (10+10+10=30).
Step 3 Sum 30+10>37.
Step 4 select 5 the second biggest number, now 30 +5 < 37.
Step 5 Again select 5, 35+ 5>37.
Step 6 Now select 2, 35+2=37.
Step 7 Stop

Recursive Algorithm

In Recursive Algorithm, same step is repeated again and again untill the final result is achieved.
To solve a Recursive computer problem, the base function calls itself again and again.

Divide and Conquer Algorithm

In this type of Algorithm, a complex problem is first divided into small problem. smaller problems are solved individually and at the last final solution is achieved by combining the solutions of smaller problems.

Dynamic Programming Algorithm

In Dynamic Programming Algorithm , a big problem is divided into smaller subproblems and the solution of sub-problems is stored in an array to be used while solving the big problem. It avoids recomputation of overlapping subproblems

Randomised Algorithm

Algorithm where random numbers are used any where in its logic is called Randomised Algorithm.
Randomness is used to reduce the time, memory and complexity of the problem.

‘Las Vegas Algorithm ’ is an example of randomised algorithm.