# Introduction to Algorithms

What is an Algorithm?

An Algorithm is the finite set of sequential instructions to accomplished a task where instructions are written in a simple English language.

It is called as a step by step solution of the problems.

It is a well-developed, organized approach to solving complex problems

It refers to logic of a program.

What are the Characteristics of an Algorithm?

An Algorithm should have the following characteristics:

1. Finiteness: An algorithm terminates (end) after a finite number of steps.

2. Input: – An algorithm accepts zero or more input.

3. Output: – An algorithm produces at least one output and should match the      desired output.

4.   Definiteness: – Each step in an algorithm is unambiguous i.e. each
step should have a unique meaning.

5.   Effectiveness: – An algorithm consists of basic instruction that are
realizable.

6.   Independent: – An algorithm should have step by step directions
which should be independent of any programming code.

7.   Feasibility: – An Algorithm should be feasible with the available resources.

Why Algorithms?

Computer is quite fast, accurate and powerful machine to execute number of tasks.

But computer has no intelligence (mindless or no thinking capability or no decision power) of its own.

It can perform a task only in a manner in which it is instructed to do.

During the process of solving any problem, one tries to find the necessary steps to be taken in a sequence.

Steps can be written in some natural language.

Natural language means the language which we use in our day to day life such as Hindi or English.

Later, these instructions are written in some computer language such as C, C++, JAVA, PHP etc.

Example 1: Write an algorithm to calculate area and circumference of circle.

Solution: Let r is denote to radius of circle and circum is denote to circumference of circle.

1.    Start

3.    area = 22/7 x r x r

4.    circum = 2 x 22/7 x r

5.    Write area, circum

6.    Stop

Example 2: Write an algorithm to find largest number among three numbers

Solution: Let a is first number, b is second number, c is third number and l is largest number which stored the result.

1.    Start

3.    If a > b then

goto step 4

otherwise

goto step 5

4.    if a > c then

l = a

otherwise

l = c

5.    if b > c then

l = b

otherwise

l = c

6.    Write “Largest No “, l

7.    stop

Example 3: Write an algorithm to display and find sum of first N terms Natural Number.

Solution: Let i is denote to generate natural number and sum is denote to addition of all-natural number and n is denote the last value.

Required algorithm is:

Step 1: Start

Step 3: sum = 0

Step 4: i = 1

Step 5: Repeat steps 6 to 8 until (i<=n)

Step 6: display i

Step 7: sum = sum + i

Step 8: i = i + 1

Step 9: write sum

Step 10: End