Algorithm PDF
Algorithm PDF
UNIT-1
ALGORITHM
Properties of Algorithms
The following are the primary factors that are often used to judge the quality of the
algorithms.
Time – To execute a program, the computer system takes some amount of time. The lesser is
the time required, the better is the algorithm.
Memory – To execute a program, computer system takes some amount of memory space.
The lesser is the memory required, the better is the algorithm.
Example
In some programs, certain set of statements are executed again and again based upon
conditional test. i.e. executed more than one time. This type of execution is called looping or
iteration.
PROBLEM SOLVING AND PYTHON PROGRAMMING
PREPARED BY : SARADHA.S , AP/CSE,SECE
Example:
Step 1: Start
Step 2: get n value.
Step 3: initialize i=1
Step 4: if (i<=n) go to step 5 else go to step 7
Step 5: Print i value and increment i value by 1
Step 6: go to step 4
Step 7: Stop
Functions:
that
performs a particular task.
FLOWCHART:
Flow chart is defined as graphical representation of the logic for problem solving. The
purpose of flowchart is making the logic of the program clear in a visual representation.
1. Communication: - Flowcharts are better way of communicating the logic of a system to all
concerned.
PROBLEM SOLVING AND PYTHON PROGRAMMING
PREPARED BY : SARADHA.S , AP/CSE,SECE
2. Effective analysis: - With the help of flowchart, problem can be analyzed in more effective
way
3. Proper documentation: - Program flowcharts serve as a good program documentation,
which is needed for various purposes.
4. Efficient Coding: - The flowcharts act as a guide or blueprint during the systems analysis
and program development phase.
PSEUDO CODE:
used for
explain an algorithm.
nes.
the general working of the program, because it is not based on any programming language.
e readable
Example:
BEGIN
GET n
INITIALIZE i=1
FOR (i<=n) DO
PRINT i
i=i+1
ENDFOR
END
Advantages:
with
converting a flowchart to programming language.
Disadvantages:
compared to flowchart.
PROBLEM SOLVING AND PYTHON PROGRAMMING
PREPARED BY : SARADHA.S , AP/CSE,SECE
Example:
BEGIN
GET a,b
ADD c=a+b
PRINT c
END
PROGRAMMING LANGUAGE
A programming language is a set of symbols and rules for instructing a computer to perform
specific tasks. The programmers have to follow all the specified rules before writing program
using programming language. The user has to communicate with the computer using
language which it can understand.
1. Machine language
2. Assembly language
3. High level language
Machine language:
The computer can understand only machine language which uses 0’s and 1’s. In machine
language the different instructions are formed by taking different combinations of 0’s and
1’s.
Advantages:
Translation free:
Machine language is the only language which the computer understands. For executing any
program written in any programming language, the conversion to machine language is
necessary. The program written in machine language can be executed directly on computer.
In this case any conversion process is not required.
PROBLEM SOLVING AND PYTHON PROGRAMMING
PREPARED BY : SARADHA.S , AP/CSE,SECE
High speed
The machine language program is translation free. Since the conversion time is saved, the
execution of machine language program is extremely fast.
Disadvantage:
Machine dependent: According to architecture used, the computer differs from each other.
So machine language differs from computer to computer. So a program developed for a
particular type of computer may not run on other type of computer.
Assembly language:
Ex: ADD a, b
Assembler:
Advantage:
Disadvantage
Machine dependent : The assembly language program which can be executed on the
machine depends on the architecture of that computer.
Hard to learn : It is machine dependent, so the programmer should have the hardware
knowledge to create applications using assembly language.
PROBLEM SOLVING AND PYTHON PROGRAMMING
PREPARED BY : SARADHA.S , AP/CSE,SECE
Less efficient :
program.
language.
High level language contains English words and symbols. The specified rules are to be
followed while writing program in high level language. The interpreter or compilers are
used for converting these programs in to machine readable form.
Translating high level language to machine language The programs that translate high level
language in to machine language are called interpreter or compiler.
Compiler:
A compiler is a program which translates the source code written in a high level language in
to object code which is in machine language program. Compiler reads the whole program
written in high level language and translates it to machine language. If any error is found it
display error message on the screen.
Interpreter
Interpreter translates the high level language program in line by line manner. The interpreter
translates a high level language statement in a source program to a machine code and
executes it immediately before translating the next statement. When an error is found the
execution of the program is halted and error message is displayed on the screen.
Advantages
1. Readability
2. High level language is closer to natural language so they are easier to learn and
understand
3. Machine independent
4. High level language program have the advantage of being portable between
machines.
5. Easy debugging
6. Easy to find and correct error in high level language
Disadvantages
Less efficient
PROBLEM SOLVING AND PYTHON PROGRAMMING
PREPARED BY : SARADHA.S , AP/CSE,SECE
The translation process increases the execution time of the program. Programs in high level
language require more memory and take more execution time to execute.
Examples:
Pascal
Python
Examples:
Clean
Haskell
Examples:
C
PROBLEM SOLVING AND PYTHON PROGRAMMING
PREPARED BY : SARADHA.S , AP/CSE,SECE
C++
C#
JAVA
Procedural (imperative) programming implies specifying the steps that the programs should
take to reach to an intended state. A procedure is a group of statements that can be referred
through a procedure call. Procedures help in the reuse of code. Procedural programming
makes the programs structured and easily traceable for program flow.
Examples:
Hyper talk
MATLAB
Scripting language:
Scripting language are programming languages that control an application. Scripts can
execute independent of any other application. They are mostly embedded in the application
that they control and are used to automate frequently executed tasks like communicating
with external program.
Examples:
Apple script
VB script
Markup languages:
A markup language is an artificial language that uses annotations to text that define hoe the
text is to be displayed.
Examples:
HTML
XML
Examples:
Joule
Limbo
he algorithm solves.
handle.
correctly for
all TIME
solving it
approximately.
PROBLEM SOLVING AND PYTHON PROGRAMMING
PREPARED BY : SARADHA.S , AP/CSE,SECE
algorithm.
specifying a
problem’s instance
approach to
solving problems algorithmically that is applicable to a variety of problems from different
areas of computing.
he following reasons.
language-like constructs.
Pseudocode is usually more precise than natural language, and its usage often yields more
succinct algorithm descriptions.
was a
flowchart, a method of expressing an algorithm by a collection of connected geometric
shapes containing descriptions of the algorithm’s steps.
it needs to
be converted into a computer program written in a particular computer language. We can
look at such a program as yet another way of specifying the algorithm, although it is
preferable to consider it as the algorithm’s implementation.
een specified, you have to prove its correctness. That is, you have
to prove that the algorithm yields a required result for every legitimate input in a finite
amount of time.
PROBLEM SOLVING AND PYTHON PROGRAMMING
PREPARED BY : SARADHA.S , AP/CSE,SECE
because an
algorithm’s iterations provide a natural sequence of steps needed for such proofs.
for a few
specific inputs can be a very worthwhile activity, it can ot prove the algorithm’s correctness
conclusively.