Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
17 views

Algorithm

The document discusses algorithms, their characteristics, examples, and analysis. An algorithm is a step-by-step procedure to solve a problem. It should be unambiguous, have inputs/outputs, be finite, and feasible. The document provides examples of algorithms to add numbers and find an average. It also discusses analyzing algorithms for efficiency before and after implementation.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Algorithm

The document discusses algorithms, their characteristics, examples, and analysis. An algorithm is a step-by-step procedure to solve a problem. It should be unambiguous, have inputs/outputs, be finite, and feasible. The document provides examples of algorithms to add numbers and find an average. It also discusses analyzing algorithms for efficiency before and after implementation.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Algorithm -Algorithm is a step – by – step procedure which is helpful in solving a

problem. it is written in English like sentences.

Characteristics of an Algorithm
Not all procedures can be called an algorithm. An algorithm should have the
following characteristics −

• Unambiguous − Algorithm should be clear and unambiguous. Each of its


steps (or phases), and their inputs/outputs should be clear and must lead to
only one meaning.
• Input − An algorithm should have 0 or more well-defined inputs.
• Output − An algorithm should have 1 or more well-defined outputs, and
should match the desired output.
• Finiteness − Algorithms must terminate after a finite number of steps.
• Feasibility − Should be feasible with the available resources.
• Independent − An algorithm should have step-by-step directions, which
should be independent of any programming code.

Example
Problem − 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

Algorithm for finding the average of three numbers is as follows −

• Start
• Read 3 numbers a,b,c
• Compute sum = a+b+c
• Compute average = sum/3
• Print average value
• Stop

Algorithm Analysis
Efficiency of an algorithm can be analyzed at two different stages, before
implementation and after implementation. They are the following −

• A Priori Analysis − This is a theoretical analysis of an algorithm. Efficiency


of an algorithm is measured by assuming that all other factors, for example,
processor speed, are constant and have no effect on the implementation.
• A Posterior Analysis − This is an empirical analysis of an algorithm. The
selected algorithm is implemented using programming language. This is
then executed on target computer machine. In this analysis, actual statistics
like running time and space required, are collected.

We shall learn about a priori algorithm analysis. Algorithm analysis deals with the
execution or running time of various operations involved. The running time of an
operation can be defined as the number of computer instructions executed per
operation.
Algorithm Complexity
Suppose X is an algorithm and n is the size of input data, the time and space used
by the algorithm X are the two main factors, which decide the efficiency of X.

• Time Factor − Time is measured by counting the number of key operations


such as comparisons in the sorting algorithm.
• Space Factor − Space is measured by counting the maximum memory
space required by the algorithm.

The complexity of an algorithm f(n) gives the running time and/or the storage
space required by the algorithm in terms of n as the size of input data.

Space Complexity
Space complexity of an algorithm represents the amount of memory space required
by the algorithm in its life cycle. The space required by an algorithm is equal to the
sum of the following two components −

• A fixed part that is a space required to store certain data and variables, that
are independent of the size of the problem. For example, simple variables
and constants used, program size, etc.
• A variable part is a space required by variables, whose size depends on the
size of the problem. For example, dynamic memory allocation, recursion
stack space, etc.

Algorithm vs Program: Difference Between Algorithm and


Program
Computer algorithms solve the problem while computer programs implement them in a form
that a computer can execute. Here are the main differences between algorithms and programs:

Algorithm Program

It refers to a set of instructions for a


It is a well-defined, step-by-step, logical computer to follow. A program can be an
procedure for solving a given problem. implementation of many algorithms, or a
program can even contain no algorithms.

An algorithm provides abstract steps for The constituents of a program may not be
processing one sequence of related conceptually related.
information into a different sequence of
derived information.

It could be written in any programming


It is written using plain English language language such as Python, Java, C++,
and can be understood by those from a non- JavaScript, or any other language,
programming background. depending on the particular task the
program is being designed for.

We write computer programs in a


It can be expressed in natural language, computer language. Then a compiler or
flow charts, pseudocode, and in a variety of interpreter translates it into a language
programming languages. that is understandable by any computer
system.

A program is always executed by a


An algorithm can be executed by a person.
computer.

You might also like