EST102 Programming in C

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 492

EST102

PROGRAMMING IN C

MODULE I
SYLLABUS
Basics of Computer Hardware and Software

Basics of Computer Architecture: processor, Memory,


Input & Output devices

Application Software & System software: Compilers,


interpreters, High level and low level languages

Introduction to structured approach to programming,


Flow chart Algorithms, Pseudo code
Important!!
➔ Memory Hierarchy
➔ RAM VS ROM
➔ System Software VS Application Software
➔ Language Processors
➔ High Level Languages VS Low Level
Languages
➔ Properties of good algorithm
➔ Symbols used in flowchart
➔ Algorithm
➔ Flowchart Linear Search, Bubble Sort
➔ Pseudocode
Basics of Computer
Architecture
Block Diagram of
Computer
Central Processing Unit
( CPU )
• CPU is responsible for the overall
working of all components of the
computer.
• It consists of two parts:
1) Arithmetic and Logic Unit(ALU)
2) Control Unit (CU)
Central Processing Unit
( CPU )
• CPU is responsible for the overall working
of all components of the computer.
• It consists of two parts:
1) Arithmetic and Logic Unit(ALU)
The ALU performs arithmetic
operations and conducts the
comparison of information for logical
decisions.
2) Control Unit (CU)
Central Processing Unit
( CPU )
• CPU is responsible for the overall working
of all components of the computer.
• It consists of two parts:
1) Arithmetic and Logic Unit(ALU)
2) Control Unit (CU)
The control unit is responsible for
sending / receiving the control signals
from / to all components.
Memory
• The memory unit of the computer is used
to store data, instructions for processing
data, intermediate results for processing
and the final processed information.
• The memory of the computer of two types:
1) Primary Memory
2) Secondary Memory.
Memory Hierarchy
CPU Registers
• Registers are very fast computer memory
which are used to execute programs and
operations efficiently.
• This is done by giving access to commonly
used values.
Accumulator
Most frequently used register used to store data taken
from memory.
MAR – Memory Address Register
It holds the address of the location to be accessed from
memory.
MDR – Memory Data Register
It contains data to be written into or to be read out
from the addressed location.
General Purpose Registers
These are numbered as R0,R1,…..,Rn and used to
store temporary data.
PC - Program Counter
It contains the memory address of next instruction
being executed.
IR – Instruction Register
It holds the instruction which is currently been
executed.
Cache Memory
• Extremely fast memory.
• Act as a buffer between primary
memory and CPU.
• It holds frequently requested
data & instructions so that they
are immediately available to the
CPU when needed.
Primary Memory
• Primary memory is faster in speed,
less in size and costlier.
• It consists of :
1)Read Only Memory(ROM)
2)Random Access Memory
(RAM)
Criteria RAM ROM
Full form Random Access Read Only
Memory Memory
Storage Temporary Storage Permanent Storage
Volatility Volatile Non-volatile
Storage Store data in MBs Store data in GBs
limit
Cost Costlier than ROM Cheaper than RAM
Writing Writing data is faster Writing data is
speed slower
Examples Static & Dynamic RAM PROM, EPROM,
& EEPROM
Secondary Memory
• Anything stored in secondary
memory remains available even
if the computer is switched off.
Secondary Storage Devices
• Magnetic Storage
1) Magnetic Tape
2) Magnetic Hard Disk
3) Floppy Disk
• Optical Storage
Input Devices
• Input devices accept data and instructions from
the user or from another computer system.
• The most common input devices are:
1) Keyboard
2) Mouse
3) Scanner
4) Joystick
5) Microphone
6) Optical Character Reader (OCR)
Output Devices
• Output devices return processed data to the
user or to another computer system.
• The most commonly used output devices
are:
1) Monitor
2) Printer
3) Speaker
4) Plotters
Bus
• Defined as a set of physical connections
which can be shared by multiple hardware
components in order to communicate with
one another.
• Three fundamental buses are:
1) Control Bus
2) Instruction Bus
3) Address Bus
1) Control Bus - Used to transfer the
control signals from one component to
another. It is a bidirectional bus and can
carry control signals in both directions.
2) Instruction Bus - Also known as data
bus. It is a bidirectional bus and can
carry the data in both the direction.
3) Address Bus - Used to transfer the
address of memory location from one
component to another. It is unidirectional
bus.
Factors affecting computer
performance
1) The speed of the CPU
2) The size of the RAM (Random Access
Memory)
3) The speed of the hard disk
4) Hard disk space
5) Multiple applications running on the
computer
6) Type of graphic card
7) Defragmenting files
Questions
1) Write short note on processor and memory in a computer. (3 Marks)
2) List any three CPU registers and give the purpose of each one. (3 Marks)
3) List out the different types of buses used in a computer system.(2 Marks)
4) Which are the parameters used for rating the performance of a computer?
(3 Marks)
5) Give the roles of PC, IR and MAR. (3 Marks)
6) Differentiate features of RAM and ROM. (3 Marks)
7) What is the significance of cache memory? (2 Marks)
8) List any four input devices used with computer. (2 Marks)
9) Differentiate between digital computers and analog computers.
(3 Marks)
10) What is memory hierarchy? Explain with a neat diagram. Compare in
terms of speed, cost and storage. (4 Marks)
Application Software
&
System Software
Software
• Software refers to the set of computer
programs, procedures that describe
the programs and how they are to be
used.
• Computer software is normally
classified into two broad categories:
1)System Software
2)Application Software
System Software
• It is a general purpose software
which is used to operate computer
hardware.
• It provides platform to run
application software.
Example: OS, Compiler, Assembler,
Interpreter etc.
Application Software
• It is a specific purpose software
which is used by user for performing
a specific task.
• Application software can’t run
independently.
Example: MS Word, Notepad,
Windows Media Player etc.
Language Processors /
Translators
Language Processors /
Translators
Language Processors /
Translators
A special translator system software is
used to translate the program written
in high level language to machine code
is called language processors.
Language Processors /
Translators
1) Compiler
2) Assembler
3) Interpreter
Language Processors
1) Compiler
Source Code Object Code
Compiler
(High Level (Machine
Language) Language)
2) Assembler
3) Interpreter
Language Processors
1) Compiler
2) Assembler

Source Code Object Code


Assembler
(Assembly (Machine
Language) Language)
3) Interpreter
Questions
1) What are the differences between compiled and
interpreted languages? Give example for each.
(3 Marks)
2) Differentiate between system software and
application software. (2 Marks)
3) Differentiate between assemblers, compilers and
interpreters. (3 Marks)
4) What is the purpose of a translator? Explain the
different types of translators. (3 Marks)
5) How is compiler different from interpreter?
(3 Marks)
Types of
Programming Languages
Programming Languages
• The operations of computer are
controlled by a set of
instructions called a computer
program.
• The language used in the
communication of computer
instructions is known as the
programming language.
Programming Language
Hierarchy
Types of Programming
Languages
1) Low level Language
2) High level language
Types of Programming
Languages
1) Low level Language
» Machine Language
» Assembly(Symbolic) Language
2) High level language
Low Level Languages
• A low-level language is a type of
programming language that contains basic
instructions recognized by a computer.
• Low-level code is often cryptic and not
human-readable.
• Two common types of low-level
programming languages are assembly
language and machine language.
1)Machine Languages
• The machine-level language is a language that consists of a set
of instructions that are in the binary form 0 or 1.
• Computers can understand only machine instructions, which are
in binary digits 0 and 1, so the instructions given to the
computer can be only in binary codes.
• Creating a program in a machine-level language is a very
difficult task as it is not easy for the programmers to write the
program in machine instructions.
• It is error-prone as it is not easy to understand, and its
maintenance is also very high.
• The different processor architectures use different machine
codes.
• A machine-level language is not portable as each computer has
its machine instructions.
2) Assembly Languages
• Assembly language instructions are written in
English words, so it is easier to write and understand.
• The translator that converts the assembly code into
machine code is known as an assembler.
• The assembly language code is not portable because
the data is stored in computer registers, and the
computer has to know the different sets of registers.
High Level Languages
• The high-level language is a programming language
that allows a programmer to write the programs
which are independent of a particular type of
computer.
• It is considered as high-level because they are closer
to human languages than machine-level languages.
• When writing a program in a high-level language,
then the whole attention needs to be paid to the logic
of the problem.
• A compiler is required to translate a high-level
language into a low-level language.
Advantages of High Level Languages
1) The high-level language is easy to read,
write, and maintain as it is written in
English like words.
2) The high-level language is portable.
3) These languages are machine-independent.

Eg:- C, C++, Java, Python, JavaScript,PHP etc.


Questions
1) What is high level language? Give four
examples. (3 Marks)
2) Differentiate between assembly language
and high-level languages. (3 Marks)
Introduction to
Structured Approach
to
Programming
Structured Programming
• Defined as a programming approach
in which the program is made as a
single structure.
• The code will execute the
instruction one after another.
Algorithms
• An algorithm is a set of steps for solving
a particular problem.
• It is a precise specification of a finite
sequence of instructions to be carried out
in order to solve a given problem.
• It can written in any natural language like
English.
Properties of good Algorithms
1) Precision
2) Uniqueness
3) Finiteness
4) Input
5) Output
6) Generality
Properties of good Algorithms
1) Precision – Steps are precisely stated.
2) Uniqueness – Results of each step are
uniquely defined and depend on the input
and the result of preceding steps.
3) Finiteness – Algorithm stops after a finite
number of instructions are executed.
4) Input – Algorithm receives input.
5) Output – Algorithm produces output.
6) Generality – Algorithm applies to a set of
inputs.
Using Algorithm to solve Problems
Using Algorithm to solve Problems
• Identify the inputs and outputs.
• Identify any other data and constants
required to solve the problem.
• Identify what needs to be computed.
• Write the algorithm.
Common words used in an Algorithm
• Input : Read, Obtain, Get
• Output : Print, Write
• Compute : Compute, Calculate,
Determine
• Initialize : Set, Initialize
• Add one : Increment
• Subtract one : Decrement
Flow Charts
• A Flow Chart depicts pictorially the
sequence in which instructions are
carried out in an algorithm.
Symbols used in Flow Charts
Symbols used in Flow Charts(Contd…)
Symbols used in Flow Charts(Contd…)
Example
Give the algorithm and draw flow chart to
find the average of three numbers.

Problem Statement : Find the average


of three numbers
• Identify the inputs and outputs
Input : Three numbers

num1,num2,num3
Output : Average of three numbers
• Identify any other data and
constants required to solve the
problem
The average of numbers is defined
as,
Sum of all inputs
Total number of inputs
Here, other data to be computed is
Sum of all inputs, ie,
Sum = num1 + num2 + num3
• Identify what needs to be computed
The average of three numbers has to
be computed.
Sum
Average =
3
• Write the algorithm
Step 1 : Start
Step 2 : Read three numbers,
num1,num2,num3
Step 3 : Calculate the sum,
Sum = num1 + num2 + num3
Step 4 : Calculate the average,
Average = Sum/3
Step 5 : Print Average
Step 6 : Stop
which can also be written as,
Step 1 : Start
Step 2 : Read num1,num2,num3
Step 3 : Sum = num1 + num2 + num3
Step 4 : Average = Sum/3
Step 5 : Print Average
Step 6 : Stop
Algorithm Flow Chart
Step 1 : Start
Start
Step 2 : Read num1,num2,num3
Step 3 : Sum = num1 + num2 +
num3 Read num1,num2,num3

Step 4 : Average = Sum/3


Step 5 : Print Average Sum = num1 + num2 + num3
Step 6 : Stop
Average = Sum/3

Print Average

Stop
Question No. 1
Design an algorithm and flow chart to find
area of a circle.

Problem Statement : Find area of a circle


• Identify the inputs and outputs
Input : Radius, r
Output : Area of circle
• Identify any other data and
constants required to solve the
problem
The area of circle is defined as,
3.14 x radius x
radius
Here, no other data to be
computed.
• Identify what needs to be computed
The area of circle has to be
computed.
Area = 3.14 x r x r
• Write the algorithm
Step 1 : Start
Step 2 : Read radius, r
Step 3 : Calculate the area,
Area = 3.14 * r * r
Step 4 : Print Area
Step 5 : Stop
which can also be written as,
Step 1 : Start
Step 2 : Read radius, r
Step 3 : Area = 3.14 * r * r
Step 4 : Print Area
Step 5 : Stop
Algorithm Flow Chart
Step 1 : Start Start
Step 2 : Read radius, r
Step 3 : Area = 3.14 * r * r Read radius, r
Step 4 : Print Area
Step 5 : Stop Area = 3.14 * r * r

Print Area

Stop
Question No. 2
Write an algorithm and draw flow chart to
find area of a square.

Problem Statement : Find area of a


square
• Identify the inputs and outputs
Input : Length of side, a
Output : Area of square
• Identify any other data and
constants required to solve the
problem
The area of square is defined as,
Length of side x Length
of side
Here, no other data to be
computed.
• Identify what needs to be computed
The area of square has to be
computed.
Area = a x a
• Write the algorithm
Step 1 : Start
Step 2 : Read length of side, a
Step 3 : Area = a * a
Step 4 : Print Area
Step 5 : Stop
Algorithm Flow Chart
Step 1 : Start Start
Step 2 : Read length of side, a
Step 3 : Area = a * a Read length of side, a
Step 4 : Print Area
Step 5 : Stop Area = a * a

Print Area

Stop
Question No. 3
Write an algorithm and draw the flow chart
to find largest among two numbers.

Problem Statement : Find largest among


two numbers
• Identify the inputs and outputs
Input : Two numbers,
num1, num2
Output : Largest Number
• Identify any other data and constants
required to solve the problem
The lagrgest among two numbers is
obtained by comparing two numbers.

If first number is greater than the


second one, then first number will be
the largest. Otherwise, Second one.
Here, no other data to be
computed.
• Identify what needs to be computed
The largest number has to be
computed among two numbers.
If num1 > num2, then,
Largest = num1
Otherwise,
Largest = num2
• Write the algorithm
Step 1 : Start
Step 2 : Read num1, num2
Step 3 : If num1 > num2, then, go to
Step _, otherwise go to Step
_.
Step 4 : Largest = num1 go to Step _
Step 5 : Largest = num2
Step 6 : Print Largest
Step 7 : Stop
• Write the algorithm
Step 1 : Start
Step 2 : Read num1, num2
Step 3 : If num1 > num2, then, go to
Step 4, otherwise go to Step
5.
Step 4 : Largest = num1 go to Step 6
Step 5 : Largest = num2
Step 6 : Print Largest
Step 7 : Stop
Algorithm
Step 1 : Start
Step 2 : Read num1, num2
Step 3 : If num1 > num2, then, go to Step 4,
otherwise go to Step 5.
Step 4 : Largest = num1 go to Step 6
Step 5 : Largest = num2
Step 6 : Print Largest
Step 7 : Stop
Flow Chart
Algorithm
Step 1 : Start
Step 2 : Read num1, num2
Step 3 : If num1 > num2, then, go to Step 4,
otherwise go to Step 5.
Step 4 : Largest = num1 go to Step 6
Step 5 : Largest = num2
Step 6 : Print Largest
Step 7 : Stop
Flow Chart
Start
Algorithm
Step 1 : Start
Step 2 : Read num1, num2
Step 3 : If num1 > num2, then, go to Step 4,
otherwise go to Step 5.
Step 4 : Largest = num1 go to Step 6
Step 5 : Largest = num2
Step 6 : Print Largest
Step 7 : Stop
Flow Chart
Start

Read num1, num2


Algorithm
Step 1 : Start
Step 2 : Read num1, num2
Step 3 : If num1 > num2, then, go to Step 4,
otherwise go to Step 5.
Step 4 : Largest = num1 go to Step 6
Step 5 : Largest = num2
Step 6 : Print Largest
Step 7 : Stop
Flow Chart
Start

Read num1, num2

If
True False
num1> num2
Algorithm
Step 1 : Start
Step 2 : Read num1, num2
Step 3 : If num1 > num2, then, go to Step 4,
otherwise go to Step 5.
Step 4 : Largest = num1 go to Step 6
Step 5 : Largest = num2
Step 6 : Print Largest
Step 7 : Stop
Algorithm
Step 1 : Start
Step 2 : Read num1, num2
Step 3 : If num1 > num2, then, go to Step 4,
otherwise go to Step 5.
Step 4 : Largest = num1 go to Step 6
Step 5 : Largest = num2
Step 6 : Print Largest
Step 7 : Stop
Algorithm
Step 1 : Start
Step 2 : Read num1, num2
Step 3 : If num1 > num2, then, go to Step 4,
otherwise go to Step 5.
Step 4 : Largest = num1 go to Step 6
Step 5 : Largest = num2
Step 6 : Print Largest
Step 7 : Stop
Flow Chart
Start

Read num1, num2

If
True False
num1> num2

Largest = num1
Algorithm
Step 1 : Start
Step 2 : Read num1, num2
Step 3 : If num1 > num2, then, go to Step 4,
otherwise go to Step 5.
Step 4 : Largest = num1 go to Step 6
Step 5 : Largest = num2
Step 6 : Print Largest
Step 7 : Stop
Algorithm
Step 1 : Start
Step 2 : Read num1, num2
Step 3 : If num1 > num2, then, go to Step 4,
otherwise go to Step 5.
Step 4 : Largest = num1 go to Step 6
Step 5 : Largest = num2
Step 6 : Print Largest
Step 7 : Stop
Algorithm
Step 1 : Start
Step 2 : Read num1, num2
Step 3 : If num1 > num2, then, go to Step 4,
otherwise go to Step 5.
Step 4 : Largest = num1 go to Step 6
Step 5 : Largest = num2
Step 6 : Print Largest
Step 7 : Stop
Flow Chart
Start

Read num1, num2

If
True False
num1> num2

Largest = num1 Largest = num2


Algorithm
Step 1 : Start
Step 2 : Read num1, num2
Step 3 : If num1 > num2, then, go to Step 4,
otherwise go to Step 5.
Step 4 : Largest = num1 go to Step 6
Step 5 : Largest = num2
Step 6 : Print Largest
Step 7 : Stop
Algorithm
Step 1 : Start
Step 2 : Read num1, num2
Step 3 : If num1 > num2, then, go to Step 4,
otherwise go to Step 5.
Step 4 : Largest = num1 go to Step 6
Step 5 : Largest = num2
Step 6 : Print Largest
Step 7 : Stop
Algorithm
Step 1 : Start
Step 2 : Read num1, num2
Step 3 : If num1 > num2, then, go to Step 4,
otherwise go to Step 5.
Step 4 : Largest = num1 go to Step 6
Step 5 : Largest = num2
Step 6 : Print Largest
Step 7 : Stop
Flow Chart
Start

Read num1, num2

If
True False
num1> num2

Largest = num1 Largest = num2

Print
Largest
Algorithm
Step 1 : Start
Step 2 : Read num1, num2
Step 3 : If num1 > num2, then, go to Step 4,
otherwise go to Step 5.
Step 4 : Largest = num1 go to Step 6
Step 5 : Largest = num2
Step 6 : Print Largest
Step 7 : Stop
Flow Chart
Start

Read num1, num2

If
True False
num1> num2

Largest = num1 Largest = num2

Print
Largest
Stop
Question No. 4
Write an algorithm and draw the flow chart
to check whether the given number is odd or
even.

Problem Statement : Check whether the


given number is
odd or even
• Identify the inputs and outputs
Input : Number, n
Output : Odd or Even
• Identify any other data and constants
required to solve the problem
If the remainder when the number is
divided by 2 is 0, then print Even
number.
Otherwise, print OddModulus
Number. Operator
Produces the remainder of an
integer division.
Here, other data to be computed is
Remainder,ie,
Remainder = n % 2
• Identify what needs to be computed
Check whether the given number is
odd or even.
If Remainder == 0, then,
Print “Even Number”
Otherwise,
Print “Odd Number”
• Write the algorithm
Step 1 : Start
Step 2 : Read number, n
Step 3 : Remainder = n % 2
Step 4 : If Remainder == 0, then, go to
Step _, otherwise go to Step _.
Step 5 : Print “Even Number” go to
Step _.
Step 6 : Print “Odd Number”
Step 7 : Stop
• Write the algorithm
Step 1 : Start
Step 2 : Read number, n
Step 3 : Remainder = n % 2
Step 4 : If Remainder == 0, then, go to
Step 5, otherwise go to Step 6.
Step 5 : Print “Even Number” go to
Step 7.
Step 6 : Print “Odd Number”
Step 7 : Stop
Algorithm
Step 1 : Start
Step 2 : Read number, n
Step 3 : Remainder = n % 2
Step 4 : If Remainder == 0, then, go to Step 5,
otherwise go to Step 6.
Step 5 : Print “Even Number” go to Step 7.
Step 6 : Print “Odd Number”
Step 7 : Stop
Flow Chart
Algorithm
Step 1 : Start
Step 2 : Read number, n
Step 3 : Remainder = n % 2
Step 4 : If Remainder == 0, then, go to Step 5,
otherwise go to Step 6.
Step 5 : Print “Even Number” go to Step 7.
Step 6 : Print “Odd Number”
Step 7 : Stop
Flow Chart
Start
Algorithm
Step 1 : Start
Step 2 : Read number, n
Step 3 : Remainder = n % 2
Step 4 : If Remainder == 0, then, go to Step 5,
otherwise go to Step 6.
Step 5 : Print “Even Number” go to Step 7.
Step 6 : Print “Odd Number”
Step 7 : Stop
Flow Chart
Start

Read number, n
Algorithm
Step 1 : Start
Step 2 : Read number, n
Step 3 : Remainder = n % 2
Step 4 : If Remainder == 0, then, go to Step 5,
otherwise go to Step 6.
Step 5 : Print “Even Number” go to Step 7.
Step 6 : Print “Odd Number”
Step 7 : Stop
Flow Chart
Start

Read number, n

Remainder = n % 2
Algorithm
Step 1 : Start
Step 2 : Read number, n
Step 3 : Remainder = n % 2
Step 4 : If Remainder == 0, then, go to Step 5,
otherwise go to Step 6.
Step 5 : Print “Even Number” go to Step 7.
Step 6 : Print “Odd Number”
Step 7 : Stop
Flow Chart
Start

Read number, n

Remainder = n % 2
If
True False
Remainder == 0
Algorithm
Step 1 : Start
Step 2 : Read number, n
Step 3 : Remainder = n % 2
Step 4 : If Remainder == 0, then, go to Step 5,
otherwise go to Step 6.
Step 5 : Print “Even Number” go to Step 7.
Step 6 : Print “Odd Number”
Step 7 : Stop
Algorithm
Step 1 : Start
Step 2 : Read number, n
Step 3 : Remainder = n % 2
Step 4 : If Remainder == 0, then, go to Step 5,
otherwise go to Step 6.
Step 5 : Print “Even Number” go to Step 7.
Step 6 : Print “Odd Number”
Step 7 : Stop
Flow Chart
Start

Read number, n

Remainder = n % 2
If
True False
Remainder == 0

Print “Even Number”


Algorithm
Step 1 : Start
Step 2 : Read number, n
Step 3 : Remainder = n % 2
Step 4 : If Remainder == 0, then, go to Step 5,
otherwise go to Step 6.
Step 5 : Print “Even Number” go to Step 7.
Step 6 : Print “Odd Number”
Step 7 : Stop
Algorithm
Step 1 : Start
Step 2 : Read number, n
Step 3 : Remainder = n % 2
Step 4 : If Remainder == 0, then, go to Step 5,
otherwise go to Step 6.
Step 5 : Print “Even Number” go to Step 7.
Step 6 : Print “Odd Number”
Step 7 : Stop
Flow Chart
Start

Read number, n

Remainder = n % 2
If
True False
Remainder == 0

Print “Even Number” Print “Odd Number”


Algorithm
Step 1 : Start
Step 2 : Read number, n
Step 3 : Remainder = n % 2
Step 4 : If Remainder == 0, then, go to Step 5,
otherwise go to Step 6.
Step 5 : Print “Even Number” go to Step 7.
Step 6 : Print “Odd Number”
Step 7 : Stop
Algorithm
Step 1 : Start
Step 2 : Read number, n
Step 3 : Remainder = n % 2
Step 4 : If Remainder == 0, then, go to Step 5,
otherwise go to Step 6.
Step 5 : Print “Even Number” go to Step 7.
Step 6 : Print “Odd Number”
Step 7 : Stop
Flow Chart
Start

Read number, n

Remainder = n % 2
If
True False
Remainder == 0

Print “Even Number” Print “Odd Number”

Stop
Question No. 5
Write an algorithm and draw the flow chart
to print the first n natural numbers where n
is entered by a user.

Problem Statement : Print the first n


natural numbers.
• Identify the inputs and outputs
Input : Limit, n
Output : Print natural numbers
upto n
For example, if,
Input : n = 6
Output : 1
2
3
4
5
6
• Identify any other data and constants
required to solve the problem
Input upper limit to print natural number
from user. Store it in some variable n.
Set lower limit as 1.
Because we need to print natural numbers
from 1 to n.
Print current number. Then, increment it
by 1 to print next one.
Because difference between two natural
numbers is 1.
Repeat this process until n.
Because we need to print natural numbers
up to n.
Here, no other data to be computed.
• Identify what needs to be computed
Set lower limit, i = 1
Print i
Print newline, “\n”
i = i + 1 or Increment i
Repeat this process until i = n
• Write the algorithm
Step 1 : Start
Step 2 : Read upper limit, n
Step 3 : Set lower limit, i = 1
Step 4 : Print i
Step 5 : Print “\n”
Step 6 : i = i + 1
Step 7 : Repeat Step _ to Step _ until
i=n
Step 8 : Stop
• Write the algorithm
Step 1 : Start
Step 2 : Read upper limit, n
Step 3 : Set lower limit, i = 1
Step 4 : Print i
Step 5 : Print “\n”
Step 6 : i = i + 1
Step 7 : Repeat Step 4 to Step 6 until
i=n
Step 8 : Stop
Algorithm
Step 1 : Start
Step 2 : Read upper limit, n
Step 3 : Set lower limit, i = 1
Step 4 : Print i
Step 5 : Print “\n”
Step 6 : i = i + 1
Step 7 : Repeat Step 4 to Step 6 until i=n
Step 8 : Stop
Flow Chart
Algorithm
Step 1 : Start
Step 2 : Read upper limit, n
Step 3 : Set lower limit, i = 1
Step 4 : Print i
Step 5 : Print “\n”
Step 6 : i = i + 1
Step 7 : Repeat Step 4 to Step 6 until i=n
Step 8 : Stop
Flow Chart
Start
Algorithm
Step 1 : Start
Step 2 : Read upper limit, n
Step 3 : Set lower limit, i = 1
Step 4 : Print i
Step 5 : Print “\n”
Step 6 : i = i + 1
Step 7 : Repeat Step 4 to Step 6 until i=n
Step 8 : Stop
Flow Chart
Start

Read upper limit, n


Algorithm
Step 1 : Start
Step 2 : Read upper limit, n
Step 3 : Set lower limit, i = 1
Step 4 : Print i
Step 5 : Print “\n”
Step 6 : i = i + 1
Step 7 : Repeat Step 4 to Step 6 until i=n
Step 8 : Stop
Flow Chart
Start

Read upper limit, n

i=1
Algorithm
Step 1 : Start
Step 2 : Read upper limit, n
Step 3 : Set lower limit, i = 1
Step 4 : Print i
Step 5 : Print “\n”
Step 6 : i = i + 1
Step 7 : Repeat Step 4 to Step 6 until i=n
Step 8 : Stop
Flow Chart
Start

Read upper limit, n

i=1

Print i
Algorithm
Step 1 : Start
Step 2 : Read upper limit, n
Step 3 : Set lower limit, i = 1
Step 4 : Print i
Step 5 : Print “\n”
Step 6 : i = i + 1
Step 7 : Repeat Step 4 to Step 6 until i=n
Step 8 : Stop
Flow Chart
Start

Read upper limit, n

i=1

Print i

Print “\n”
Algorithm
Step 1 : Start
Step 2 : Read upper limit, n
Step 3 : Set lower limit, i = 1
Step 4 : Print i
Step 5 : Print “\n”
Step 6 : i = i + 1
Step 7 : Repeat Step 4 to Step 6 until i=n
Step 8 : Stop
Flow Chart
Start

Read upper limit, n


i=1
Print i
Print “\n”
i=i+1
Algorithm
Step 1 : Start
Step 2 : Read upper limit, n
Step 3 : Set lower limit, i = 1
Step 4 : Print i
Step 5 : Print “\n”
Step 6 : i = i + 1
Step 7 : Repeat Step 4 to Step 6 until i=n
Step 8 : Stop
Flow Chart
Start

Read upper limit, n


i=1
Print i
Print “\n”
i=i+1

False Check whether True


i=n Stop
Pseudocode
• Implementation of an algorithm in the
form of annotations and informative text
written in plain English.
• It has no syntax.
• Can’t be compiled or interpreted by the
computer.
Pseudocode (Contd…)
• Pseudocode is a simple way of writing
programming code in English.
• It uses short phrases to write code for
programs before you actually create it in
a specific language.
• Pseudocode makes creating programs
easier.
Why use pseudocode at all?
1)Better readability
2)Ease up code construction
3)A good middle point between flow
chart and code
4)Act as start point for
documentation
5)Easier bug detection and fixing
The main constructs of pseudocode
• The core of pseudocode is the
ability to represent six
programming constructs, always
written in uppercase.
• These constructs, also called
keywords, are used to describe the
control flow of the algorithm.
The main constructs of pseudocode

1)SEQUENCE
2)WHILE
3)REPEAT - UNTIL
4)FOR
5)IF - THEN - ELSE
6)CASE
The main constructs of pseudocode
1)SEQUENCE represents linear task sequentially
performed one after the other.
2)WHILE a loop with a condition at its
beginning.
3)REPEAT - UNTIL a loop with a condition at
the bottom.
4)FOR another way of looping.
5)IF - THEN - ELSE a conditional statement
changing the flow of the algorithm.
6)CASE the generalization form of IF - THEN -
ELSE.
Example
Give the algorithm, draw flow chart and
pseudocode to find the average of three
numbers.

Problem Statement : Find the average


of three numbers
Algorithm Flow Chart
Step 1 : Start
Start
Step 2 : Read num1,num2,num3
Step 3 : Sum = num1 + num2 +
num3 Read num1,num2,num3

Step 4 : Average = Sum/3


Step 5 : Print Average Sum = num1 + num2 + num3
Step 6 : Stop
Average = Sum/3

Print Average

Stop
Pseudocode
READ num1,num2,num3
Sum = num1 + num2 + num3
Average = Sum/3
PRINT Average
Question No. 1
Design an algorithm, draw flow chart and
pseudocode to find area of a circle.

Problem Statement : Find area of a circle


Algorithm Flow Chart
Step 1 : Start Start
Step 2 : Read radius, r
Step 3 : Area = 3.14 * r * r Read radius, r
Step 4 : Print Area
Step 5 : Stop Area = 3.14 * r * r

Print Area

Stop
Pseudocode
READ r
Area = 3.14 * r * r
PRINT Area
Question No. 2
Write an algorithm, draw flow chart and
pseudocode to find area of a square.

Problem Statement : Find area of a


square
Algorithm Flow Chart
Step 1 : Start Start
Step 2 : Read length of side, a
Step 3 : Area = a * a Read length of side, a
Step 4 : Print Area
Step 5 : Stop Area = a * a

Print Area

Stop
Pseudocode
READ a
Area = a * a
PRINT Area
Question No. 3
Write an algorithm, draw flow chart and
pseudocode to find largest among two
numbers.

Problem Statement : Find largest among


two numbers
Algorithm
Step 1 : Start
Step 2 : Read num1, num2
Step 3 : If num1 > num2, then, go to Step 4,
otherwise go to Step 5.
Step 4 : Largest = num1 go to Step 6
Step 5 : Largest = num2
Step 6 : Print Largest
Step 7 : Stop
Flow Chart
Start

Read num1, num2

If
True False
num1> num2

Largest = num1 Largest = num2

Print
Largest
Stop
Pseudocode
READ num1, num2
IF num1 > num2
THEN
Largest = num1
ELSE
Largest = num2
ENDIF
PRINT Largest
Question No. 4
Write an algorithm, draw flow chart and
pseudocode to check whether the given
number is odd or even.

Problem Statement : Check whether the


given number is
odd or even
Algorithm
Step 1 : Start
Step 2 : Read number, n
Step 3 : Remainder = n % 2
Step 4 : If Remainder == 0, then, go to Step 5,
otherwise go to Step 6.
Step 5 : Print “Even Number” go to Step 7.
Step 6 : Print “Odd Number”
Step 7 : Stop
Flow Chart
Start

Read number, n

Remainder = n % 2
If
True False
Remainder == 0

Print “Even Number” Print “Odd Number”

Stop
Pseudocode
READ n
Remainder = n % 2
IF Remainder == 0
THEN
PRINT “Even Number”
ELSE
PRINT “Odd Number”
ENDIF
Difference b/w Algorithm,Flow Chart and
pseudo Code
Sl. Algorithm Flow Chart Pseudo Code
No.

1 Sequence of Pictorial representation of an Rules of structured


instruction to solve algorithm using standard design and
the particular symbols programming
problem

2 It is a pure English It has symbols to represent the It is a pure English


language. It has no instruction. language. But, it
rules to write. has some set of
rules.
Linear Search
• A linear search or sequential search is a
method for finding an element within a
list.
• It sequentially checks each element of the
list until a match is found or the whole
list has been searched.
• It is the simplest searching algorithm that
searches for an element in a list in
sequential order.
How Linear Search works?
Index 0 1 2 3 4
A 2 4 0 1 9
Value A[0] A[1] A[2] A[3] A[4]
1 1 1 1
Doesn’tDoesn’t
matchDoesn’t
match match
Matches

Element to search : 1
Element is present at location 3.
Write an algorithm to search an element in
an array using linear search.

Problem Statement: To search an element


in an array using
linear search.
• Identify the inputs and outputs
Input : - Size of the array, n
- Elements of the
array,
a[0],a[1],a[2],
……….,a[n-1]
- Element to be
searched, k
Output : Print “Element is present at
Example
Index 0 1 2 3 4
a 2 4 0 1 9

Size of the array,


n=5
Example
Index 0 1 2 3 4
a 2 4 0 1 9
Value a[0] a[1] a[2] a[3] a[4]

Size of the array,


n=5
Element to be searched, k = 1
Index 0 1 2 3 4
a 2 4 0 1 9
Value a[0] a[1] a[2] a[3] a[4]
a[0]==ka[1]==ka[2]==k a[3]==k
Doesn’tDoesn’t
matchDoesn’t
match match
Matches

Element is present at location 3.


• Write the algorithm
Step 1 : Start
Step 2 : Read size of the array, n
Step 3 : Set, i = 0
Step 4 : Check whether i < n, if it is true,
then go to Step _. Otherwise, go
to Step _.
Step 5 : Read a[i]
Step 6 : i = i + 1 go to Step _
Step 7 : Read element to be searched, k
• Write the algorithm
Step 1 : Start
Step 2 : Read size of the array, n
Step 3 : Set, i = 0
Step 4 : Check whether i < n, if it is true,
then go to Step 5. Otherwise, go
to Step 7.
Step 5 : Read a[i]
Step 6 : i = i + 1 go to Step 4
Step 7 : Read element to be searched, k
Step 8 : Set j = 0
Step 9 : Check whether j < n, if it is true,
then go to Step _. Otherwise, go
to Step _.
Step 10 : If a[j] == k, then go to Step _.
Otherwise, go to Step _.
Step 11 : Print “Element is present at
position j” go to Step _
Step 12 : j = j + 1 go to Step _
Step 13 : If j == n, then go to Step _.
Otherwise, go to Step _.
Step 8 : Set j = 0
Step 9 : Check whether j < n, if it is true,
then go to Step 10. Otherwise,
go to Step 13.
Step 10 : If a[j] == k, then go to Step 11.
Otherwise, go to Step 12.
Step 11 : Print “Element is present at
position j” go to Step 15
Step 12 : j = j + 1 go to Step 9
Step 13 : If j == n, then go to Step 14.
Otherwise, go to Step 15.
Step 14 : Print “Element is not present”
Step 15 : Stop
• Write the algorithm
Step 1 : Start
Step 2 : Read size of the array, n
Step 3 : Set, i = 0
Step 4 : Check whether i < n, if it is true,
then go to Step 5. Otherwise, go
to Step 7.
Step 5 : Read a[i]
Step 6 : i = i + 1 go to Step 4
Step 7 : Read element to be searched, k
Step 8 : Set j = 0
Step 9 : Check whether j < n, if it is true,
then go to Step 10. Otherwise,
go to Step 13.
Step 10 : If a[j] == k, then go to Step 11.
Otherwise, go to Step 12.
Step 11 : Print “Element is present at
position j” go to Step 15
Step 12 : j = j + 1 go to Step 9
Step 13 : If j == n, then go to Step 14.
Otherwise, go to Step 15.
Step 14 : Print “Element is not present”
Step 15 : Stop
Draw the flow chart to search an element in
an array using linear search.
Algorithm
Step 1 : Start
Step 2 : Read size of the array, n
Step 3 : Set, i = 0
Step 4 : Check whether i < n, if it is true,
then go to Step 5. Otherwise, go
to Step 7.
Step 5 : Read a[i]
Step 6 : i = i + 1 go to Step 4
Step 7 : Read element to be searched, k
Step 8 : Set j = 0
Step 9 : Check whether j < n, if it is true,
then go to Step 10. Otherwise,
go to Step 13.
Step 10 : If a[j] == k, then go to Step 11.
Otherwise, go to Step 12.
Step 11 : Print “Element is present at
position j” go to Step 15
Step 12 : j = j + 1 go to Step 9
Step 13 : If j == n, then go to Step 14.
Otherwise, go to Step 15.
Step 14 : Print “Element is not present”
Step 15 : Stop
Flow Chart
Algorithm
Step 1 : Start
Step 2 : Read size of the array, n
Step 3 : Set, i = 0
Step 4 : Check whether i < n, if it is true,
then go to Step 5. Otherwise, go
to Step 7.
Step 5 : Read a[i]
Step 6 : i = i + 1 go to Step 4
Step 7 : Read element to be searched, k
Flow Chart
Start
Algorithm
Step 1 : Start
Step 2 : Read size of the array, n
Step 3 : Set, i = 0
Step 4 : Check whether i < n, if it is true,
then go to Step 5. Otherwise, go
to Step 7.
Step 5 : Read a[i]
Step 6 : i = i + 1 go to Step 4
Step 7 : Read element to be searched, k
Flow Chart
Start

Read size of the array, n


Algorithm
Step 1 : Start
Step 2 : Read size of the array, n
Step 3 : Set, i = 0
Step 4 : Check whether i < n, if it is true,
then go to Step 5. Otherwise, go
to Step 7.
Step 5 : Read a[i]
Step 6 : i = i + 1 go to Step 4
Step 7 : Read element to be searched, k
Flow Chart
Start

Read size of the array, n

i=0
Algorithm
Step 1 : Start
Step 2 : Read size of the array, n
Step 3 : Set, i = 0
Step 4 : Check whether i < n, if it is true,
then go to Step 5. Otherwise, go
to Step 7.
Step 5 : Read a[i]
Step 6 : i = i + 1 go to Step 4
Step 7 : Read element to be searched, k
Flow Chart
Start

Read size of the array, n

i=0

True Check whether False


i<n
Algorithm
Step 1 : Start
Step 2 : Read size of the array, n
Step 3 : Set, i = 0
Step 4 : Check whether i < n, if it is true,
then go to Step 5. Otherwise, go
to Step 7.
Step 5 : Read a[i]
Step 6 : i = i + 1 go to Step 4
Step 7 : Read element to be searched, k
Algorithm
Step 1 : Start
Step 2 : Read size of the array, n
Step 3 : Set, i = 0
Step 4 : Check whether i < n, if it is true,
then go to Step 5. Otherwise, go
to Step 7.
Step 5 : Read a[i]
Step 6 : i = i + 1 go to Step 4
Step 7 : Read element to be searched, k
Algorithm
Step 1 : Start
Step 2 : Read size of the array, n
Step 3 : Set, i = 0
Step 4 : Check whether i < n, if it is true,
then go to Step 5. Otherwise, go
to Step 7.
Step 5 : Read a[i]
Step 6 : i = i + 1 go to Step 4
Step 7 : Read element to be searched, k
Algorithm
Step 1 : Start
Step 2 : Read size of the array, n
Step 3 : Set, i = 0
Step 4 : Check whether i < n, if it is true,
then go to Step 5. Otherwise, go
to Step 7.
Step 5 : Read a[i]
Step 6 : i = i + 1 go to Step 4
Step 7 : Read element to be searched, k
Flow Chart
Start

Read size of the array, n

i=0

True Check whether False


i<n
Read a[i]

i=i+1
Algorithm
Step 1 : Start
Step 2 : Read size of the array, n
Step 3 : Set, i = 0
Step 4 : Check whether i < n, if it is true,
then go to Step 5. Otherwise, go
to Step 7.
Step 5 : Read a[i]
Step 6 : i = i + 1 go to Step 4
Step 7 : Read element to be searched, k
Algorithm
Step 1 : Start
Step 2 : Read size of the array, n
Step 3 : Set, i = 0
Step 4 : Check whether i < n, if it is true,
then go to Step 5. Otherwise, go
to Step 7.
Step 5 : Read a[i]
Step 6 : i = i + 1 go to Step 4
Step 7 : Read element to be searched, k
Algorithm
Step 1 : Start
Step 2 : Read size of the array, n
Step 3 : Set, i = 0
Step 4 : Check whether i < n, if it is true,
then go to Step 5. Otherwise, go
to Step 7.
Step 5 : Read a[i]
Step 6 : i = i + 1 go to Step 4
Step 7 : Read element to be searched, k
Flow Chart
Start

Read size of the array, n

i=0

True Check whether False


i<n
Read a[i]

i=i+1
Flow Chart
Read element to
be searched, k

False
Step 8 : Set j = 0
Step 9 : Check whether j < n, if it is true,
then go to Step 10. Otherwise,
go to Step 13.
Step 10 : If a[j] == k, then go to Step 11.
Otherwise, go to Step 12.
Step 11 : Print “Element is present at
position j” go to Step 15
Step 12 : j = j + 1 go to Step 9
Step 13 : If j == n, then go to Step 14.
Otherwise, go to Step 15.
Step 8 : Set j = 0
Step 9 : Check whether j < n, if it is true,
then go to Step 10. Otherwise,
go to Step 13.
Step 10 : If a[j] == k, then go to Step 11.
Otherwise, go to Step 12.
Step 11 : Print “Element is present at
position j” go to Step 15
Step 12 : j = j + 1 go to Step 9
Step 13 : If j == n, then go to Step 14.
Otherwise, go to Step 15.
Flow Chart
Read element to
be searched, k

j=0

False
Step 8 : Set j = 0
Step 9 : Check whether j < n, if it is true,
then go to Step 10. Otherwise,
go to Step 13.
Step 10 : If a[j] == k, then go to Step 11.
Otherwise, go to Step 12.
Step 11 : Print “Element is present at
position j” go to Step 15
Step 12 : j = j + 1 go to Step 9
Step 13 : If j == n, then go to Step 14.
Otherwise, go to Step 15.
Flow Chart
Read element to
be searched, k

j=0

Check
True whether
False
j<n
False
Step 8 : Set j = 0
Step 9 : Check whether j < n, if it is true,
then go to Step 10. Otherwise,
go to Step 13.
Step 10 : If a[j] == k, then go to Step 11.
Otherwise, go to Step 12.
Step 11 : Print “Element is present at
position j” go to Step 15
Step 12 : j = j + 1 go to Step 9
Step 13 : If j == n, then go to Step 14.
Otherwise, go to Step 15.
Step 8 : Set j = 0
Step 9 : Check whether j < n, if it is true,
then go to Step 10. Otherwise,
go to Step 13.
Step 10 : If a[j] == k, then go to Step 11.
Otherwise, go to Step 12.
Step 11 : Print “Element is present at
position j” go to Step 15
Step 12 : j = j + 1 go to Step 9
Step 13 : If j == n, then go to Step 14.
Otherwise, go to Step 15.
Step 8 : Set j = 0
Step 9 : Check whether j < n, if it is true,
then go to Step 10. Otherwise,
go to Step 13.
Step 10 : If a[j] == k, then go to Step 11.
Otherwise, go to Step 12.
Step 11 : Print “Element is present at
position j” go to Step 15
Step 12 : j = j + 1 go to Step 9
Step 13 : If j == n, then go to Step 14.
Otherwise, go to Step 15.
Flow Chart
Read element to
be searched, k

j=0

Check
True whether
False
j<n
False
If
True
a[j]==k

False
Step 8 : Set j = 0
Step 9 : Check whether j < n, if it is true,
then go to Step 10. Otherwise,
go to Step 13.
Step 10 : If a[j] == k, then go to Step 11.
Otherwise, go to Step 12.
Step 11 : Print “Element is present at
position j” go to Step 15
Step 12 : j = j + 1 go to Step 9
Step 13 : If j == n, then go to Step 14.
Otherwise, go to Step 15.
Step 8 : Set j = 0
Step 9 : Check whether j < n, if it is true,
then go to Step 10. Otherwise,
go to Step 13.
Step 10 : If a[j] == k, then go to Step 11.
Otherwise, go to Step 12.
Step 11 : Print “Element is present at
position j” go to Step 15
Step 12 : j = j + 1 go to Step 9
Step 13 : If j == n, then go to Step 14.
Otherwise, go to Step 15.
Flow Chart
Read element to
be searched, k

j=0

Check
True whether
False
j<n
False
If
True
a[j]==k

False
Print “Element is
present at position j”
Step 8 : Set j = 0
Step 9 : Check whether j < n, if it is true,
then go to Step 10. Otherwise,
go to Step 13.
Step 10 : If a[j] == k, then go to Step 11.
Otherwise, go to Step 12.
Step 11 : Print “Element is present at
position j” go to Step 15
Step 12 : j = j + 1 go to Step 9
Step 13 : If j == n, then go to Step 14.
Otherwise, go to Step 15.
Step 8 : Set j = 0
Step 9 : Check whether j < n, if it is true,
then go to Step 10. Otherwise,
go to Step 13.
Step 10 : If a[j] == k, then go to Step 11.
Otherwise, go to Step 12.
Step 11 : Print “Element is present at
position j” go to Step 15
Step 12 : j = j + 1 go to Step 9
Step 13 : If j == n, then go to Step 14.
Otherwise, go to Step 15.
Flow Chart
Read element to
be searched, k

j=0

Check
True whether
False
j<n
False
If
True
a[j]==k

False
Print “Element is
present at position j” j = j+1
Step 8 : Set j = 0
Step 9 : Check whether j < n, if it is true,
then go to Step 10. Otherwise,
go to Step 13.
Step 10 : If a[j] == k, then go to Step 11.
Otherwise, go to Step 12.
Step 11 : Print “Element is present at
position j” go to Step 15
Step 12 : j = j + 1 go to Step 9
Step 13 : If j == n, then go to Step 14.
Otherwise, go to Step 15.
Step 8 : Set j = 0
Step 9 : Check whether j < n, if it is true,
then go to Step 10. Otherwise,
go to Step 13.
Step 10 : If a[j] == k, then go to Step 11.
Otherwise, go to Step 12.
Step 11 : Print “Element is present at
position j” go to Step 15
Step 12 : j = j + 1 go to Step 9
Step 13 : If j == n, then go to Step 14.
Otherwise, go to Step 15.
Step 8 : Set j = 0
Step 9 : Check whether j < n, if it is true,
then go to Step 10. Otherwise,
go to Step 13.
Step 10 : If a[j] == k, then go to Step 11.
Otherwise, go to Step 12.
Step 11 : Print “Element is present at
position j” go to Step 15
Step 12 : j = j + 1 go to Step 9
Step 13 : If j == n, then go to Step 14.
Otherwise, go to Step 15.
Flow Chart
Read element to
be searched, k

j=0

Check
True whether
False
j<n
False If
If False
True j==n
a[j]==k
True
False
Print “Element is
present at position j” j = j+1
Step 8 : Set j = 0
Step 9 : Check whether j < n, if it is true,
then go to Step 10. Otherwise,
go to Step 13.
Step 10 : If a[j] == k, then go to Step 11.
Otherwise, go to Step 12.
Step 11 : Print “Element is present at
position j” go to Step 15
Step 12 : j = j + 1 go to Step 9
Step 13 : If j == n, then go to Step 14.
Otherwise, go to Step 15.
Step 8 : Set j = 0
Step 9 : Check whether j < n, if it is true,
then go to Step 10. Otherwise,
go to Step 13.
Step 10 : If a[j] == k, then go to Step 11.
Otherwise, go to Step 12.
Step 11 : Print “Element is present at
position j” go to Step 15
Step 12 : j = j + 1 go to Step 9
Step 13 : If j == n, then go to Step 14.
Otherwise, go to Step 15.
Step 14 : Print “Element is not present”
Step 15 : Stop
Flow Chart
Read element to
be searched, k

j=0

Check
True whether
False
j<n
False If
If False
True j==n
a[j]==k
True
False Print “Element
Print “Element is is not present”
present at position j” j = j+1
Step 8 : Set j = 0
Step 9 : Check whether j < n, if it is true,
then go to Step 10. Otherwise,
go to Step 13.
Step 10 : If a[j] == k, then go to Step 11.
Otherwise, go to Step 12.
Step 11 : Print “Element is present at
position j” go to Step 15
Step 12 : j = j + 1 go to Step 9
Step 13 : If j == n, then go to Step 14.
Otherwise, go to Step 15.
Step 14 : Print “Element is not present”
Step 15 : Stop
Flow Chart
Read element to
be searched, k

j=0

Check
True whether
False
j<n
False If
If False
True j==n
a[j]==k
True
False Print “Element
Print “Element is
is not present”
present at position j” j = j+1

Stop
Write the pseudocode to search an element
in an array using linear search.
Pseudocode
Algorithm
Step 1 : Start
Step 2 : Read size of the array, n
Step 3 : Set, i = 0
Step 4 : Check whether i < n, if it is true,
then go to Step 5. Otherwise, go
to Step 7.
Step 5 : Read a[i]
Step 6 : i = i + 1 go to Step 4
Step 7 : Read element to be searched, k
Algorithm
Step 1 : Start
Step 2 : Read size of the array, n
Step 3 : Set, i = 0
Step 4 : Check whether i < n, if it is true,
then go to Step 5. Otherwise, go
to Step 7.
Step 5 : Read a[i]
Step 6 : i = i + 1 go to Step 4
Step 7 : Read element to be searched, k
Pseudocode
READ n
Algorithm
Step 1 : Start
Step 2 : Read size of the array, n
Step 3 : Set, i = 0
Step 4 : Check whether i < n, if it is true,
then go to Step 5. Otherwise, go
to Step 7.
Step 5 : Read a[i]
Step 6 : i = i + 1 go to Step 4
Step 7 : Read element to be searched, k
Pseudocode
READ n
SET i = 0
Algorithm
Step 1 : Start
Step 2 : Read size of the array, n
Step 3 : Set, i = 0
Step 4 : Check whether i < n, if it is true,
then go to Step 5. Otherwise, go
to Step 7.
Step 5 : Read a[i]
Step 6 : i = i + 1 go to Step 4
Step 7 : Read element to be searched, k
Algorithm
Step 1 : Start
Step 2 : Read size of the array, n
Step 3 : Set, i = 0
Step 4 : Check whether i < n, if it is true,
then go to Step 5. Otherwise, go
to Step 7.
Step 5 : Read a[i]
Step 6 : i = i + 1 go to Step 4
Step 7 : Read element to be searched, k
Pseudocode
READ n
SET i = 0
WHILE i < n
READ a[i]
i=i+1
ENDWHILE
Algorithm
Step 1 : Start
Step 2 : Read size of the array, n
Step 3 : Set, i = 0
Step 4 : Check whether i < n, if it is true,
then go to Step 5. Otherwise, go
to Step 7.
Step 5 : Read a[i]
Step 6 : i = i + 1 go to Step 4
Step 7 : Read element to be searched, k
Pseudocode
READ n
SET i = 0
WHILE i < n
READ a[i]
i=i+1
ENDWHILE
READ k
Step 8 : Set j = 0
Step 9 : Check whether j < n, if it is true,
then go to Step 10. Otherwise,
go to Step 13.
Step 10 : If a[j] == k, then go to Step 11.
Otherwise, go to Step 12.
Step 11 : Print “Element is present at
position j” go to Step 15
Step 12 : j = j + 1 go to Step 9
Step 8 : Set j = 0
Step 9 : Check whether j < n, if it is true,
then go to Step 10. Otherwise,
go to Step 13.
Step 10 : If a[j] == k, then go to Step 11.
Otherwise, go to Step 12.
Step 11 : Print “Element is present at
position j” go to Step 15
Step 12 : j = j + 1 go to Step 9
Pseudocode
READ n
SET i = 0
WHILE i < n
READ a[i]
i=i+1
ENDWHILE
READ k
SET j = 0
Step 8 : Set j = 0
Step 9 : Check whether j < n, if it is true,
then go to Step 10. Otherwise,
go to Step 13.
Step 10 : If a[j] == k, then go to Step 11.
Otherwise, go to Step 12.
Step 11 : Print “Element is present at
position j” go to Step 15
Step 12 : j = j + 1 go to Step 9
Pseudocode
READ n
SET i = 0
WHILE i < n
READ a[i]
i=i+1
ENDWHILE
READ k
SET j = 0
WHILE j < n
IF a[j] == k
THEN
PRINT “Element is present at
position j”
BREAK
ENDIF
j=j+1
ENDWHILE
Step 13 : If j == n, then go to Step 14.
Otherwise, go to Step 15.
Step 14 : Print “Element is not present”
Step 15 : Stop
Step 13 : If j == n, then go to Step 14.
Otherwise, go to Step 15.
Step 14 : Print “Element is not present”
Step 15 : Stop
Pseudocode
READ n
SET i = 0
WHILE i < n
READ a[i]
i=i+1
ENDWHILE
READ k
SET j = 0
WHILE j < n
IF a[j] == k
THEN
PRINT “Element is present at position j”
BREAK
ENDIF
j=j+1
ENDWHILE
IF j == n
THEN
PRINT “Element is not present”
ENDIF
Pseudocode
READ n
SET i = 0
WHILE i < n
READ a[i]
i=i+1
ENDWHILE
READ k
SET j = 0
WHILE j < n
IF a[j] == k
THEN
PRINT “Element is present at position j”
BREAK
ENDIF
j=j+1
ENDWHILE
IF j == n
THEN
PRINT “Element is not present”
ENDIF
Bubble Sort
• Bubble sort, also referred to as
Comparison sort, is a simple sorting
algorithm.
• It repeatedly goes through the list,
compares adjacent elements and swaps
them if they are in the wrong order.
• Bubble sort is the easiest sorting
algorithm to implement.
• It is an in-place sorting algorithm.
In real life, bubble sort can be
visualised when people in a queue
wanting to be standing in a height wise
sorted manner swap their positions
among themselves until everyone is
standing based on increasing order of
heights.
How Bubble Sort works?
• Bubble sort uses multiple passes (scans)
through an array.
• In each pass, bubble sort compares the
adjacent elements of the array.
• It then swaps the two elements if they are
in the wrong order.
• In each pass, bubble sort places the next
largest element to its proper position.
• In short, it bubbles down the largest
element to its correct position.
Example
Index 0 1 2 3 4
A 14 33 27 35 10
Value A[0] A[1] A[2] A[3] A[4]
We need to sort this array in ascending order
using bubble sort.
Expected Output:
Index 0 1 2 3 4
A 10 14 27 33 35
Value A[0] A[1] A[2] A[3] A[4]
Input :
Index 0 1 2 3 4
A 14 33 27 35 10
Value A[0] A[1] A[2] A[3] A[4]
First Pass
We proceed with the first and second element.
ie, A[0] and A[1].
Check if A[0] > A[1]
=> 14 > 33
=> FALSE
So, no swapping happens and the array
remains the same.
Index 0 1 2 3 4
A 14 33 27 35 10
Value A[0] A[1] A[2] A[3] A[4]
We proceed with the second and third element.
ie, A[1] and A[2].
Check if A[1] > A[2]
=> 33 > 27
=> TRUE
So, we swap A[1] and A[2].
Index 0 1 2 3 4
A 14 33 27 35 10
Value A[0] A[1] A[2] A[3] A[4]
Swapping will be done with the help of a
third variable.
Index 0 1 2 3 4
A 14 33 27 35 10
Value A[0] A[1] A[2] A[3] A[4]

temp = A[1]
temp
Swapping will be done with the help of a
third variable.
Index 0 1 2 3 4
A 14 33 27 35 10
Value A[0] A[1] A[2] A[3] A[4]

temp = A[1]
temp 33
Swapping will be done with the help of a
third variable.
Index 0 1 2 3 4
A 14 33 27 35 10
Value A[0] A[1] A[2] A[3] A[4]

temp = A[1]
temp 33 A[1] = A[2]
Swapping will be done with the help of a
third variable.
Index 0 1 2 3 4
A 14 33 27 27 35 10
Value A[0] A[1] A[2] A[3] A[4]

temp = A[1]
temp 33 A[1] = A[2]
Swapping will be done with the help of a
third variable.
Index 0 1 2 3 4
A 14 27 27 35 10
Value A[0] A[1] A[2] A[3] A[4]

temp = A[1]
temp 33 A[1] = A[2]
Swapping will be done with the help of a
third variable.
Index 0 1 2 3 4
A 14 27 27 35 10
Value A[0] A[1] A[2] A[3] A[4]

temp = A[1]
temp 33 A[1] = A[2]
A[2] = temp
Swapping will be done with the help of a
third variable.
Index 0 1 2 3 4
A 14 27 27 33 35 10
Value A[0] A[1] A[2] A[3] A[4]

temp = A[1]
temp 33 A[1] = A[2]
A[2] = temp
Swapping will be done with the help of a
third variable.
Index 0 1 2 3 4
A 14 27 33 35 10
Value A[0] A[1] A[2] A[3] A[4]

temp = A[1]
temp 33 A[1] = A[2]
A[2] = temp
Swapping will be done with the help of a
third variable.
Index 0 1 2 3 4
A 14 27 33 35 10
Value A[0] A[1] A[2] A[3] A[4]
temp = A[1]
temp 33 A[1] = A[2]
A[2] = temp
Thus the array becomes:
Index 0 1 2 3 4
A 14 27 33 35 10
Value A[0] A[1] A[2] A[3] A[4]
We proceed with the third and fourth element.
ie, A[2] and A[3].
Check if A[2] > A[3]
=> 33 > 35
=> FALSE
So, no swapping happens and the array
remains the same.
Index 0 1 2 3 4
A 14 27 33 35 10
Value A[0] A[1] A[2] A[3] A[4]
We proceed with the fourth and fifth element.
ie, A[3] and A[4].
Check if A[3] > A[4]
=> 35 > 10
=> TRUE
So, we swap A[3] and A[4].
Index 0 1 2 3 4
A 14 27 33 35 10
Value A[0] A[1] A[2] A[3] A[4]
Swapping will be done with the help of a
third variable.
Index 0 1 2 3 4
A 14 27 33 35 10
Value A[0] A[1] A[2] A[3] A[4]

temp = A[3]
temp
Swapping will be done with the help of a
third variable.
Index 0 1 2 3 4
A 14 27 33 35 10
Value A[0] A[1] A[2] A[3] A[4]

temp = A[3]
temp 35
Swapping will be done with the help of a
third variable.
Index 0 1 2 3 4
A 14 27 33 35 10
Value A[0] A[1] A[2] A[3] A[4]

temp = A[3]
temp 35 A[3] = A[4]
Swapping will be done with the help of a
third variable.
Index 0 1 2 3 4
A 14 27 33 35 10 10
Value A[0] A[1] A[2] A[3] A[4]

temp = A[3]
temp 35 A[3] = A[4]
Swapping will be done with the help of a
third variable.
Index 0 1 2 3 4
A 14 27 33 10 10
Value A[0] A[1] A[2] A[3] A[4]

temp = A[3]
temp 35 A[3] = A[4]
Swapping will be done with the help of a
third variable.
Index 0 1 2 3 4
A 14 27 33 10 10
Value A[0] A[1] A[2] A[3] A[4]

temp = A[3]
temp 35 A[3] = A[4]
A[4] = temp
Swapping will be done with the help of a
third variable.
Index 0 1 2 3 4
A 14 27 33 10 10 35
Value A[0] A[1] A[2] A[3] A[4]

temp = A[3]
temp 35 A[3] = A[4]
A[4] = temp
Swapping will be done with the help of a
third variable.
Index 0 1 2 3 4
A 14 27 33 10 35
Value A[0] A[1] A[2] A[3] A[4]

temp = A[3]
temp 35 A[3] = A[4]
A[4] = temp
Swapping will be done with the help of a
third variable.
Index 0 1 2 3 4
A 14 27 33 10 35
Value A[0] A[1] A[2] A[3] A[4]
temp = A[3]
temp 35 A[3] = A[4]
A[4] = temp
Thus the array becomes:
Index 0 1 2 3 4
A 14 27 33 10 35
Value A[0] A[1] A[2] A[3] A[4]
Index 0 1 2 3 4
A 14 27 33 10 35
Value A[0] A[1] A[2] A[3] A[4]

Thus, marks the end of the first pass,


where the Largest element reaches its
final(last) position.
Second Pass
We proceed with the first and second element.
ie, A[0] and A[1].
Check if A[0] > A[1]
=> 14 > 27
=> FALSE
So, no swapping happens and the array
remains the same.
Index 0 1 2 3 4
A 14 27 33 10 35
Value A[0] A[1] A[2] A[3] A[4]
We proceed with the second and third element.
ie, A[1] and A[2].
Check if A[1] > A[2]
=> 27 > 33
=> FALSE
So, no swapping happens and the array
remains the same.
Index 0 1 2 3 4
A 14 27 33 10 35
Value A[0] A[1] A[2] A[3] A[4]
We proceed with the third and fourth element.
ie, A[2] and A[3].
Check if A[2] > A[3]
=> 33 > 10
=> TRUE
So, we swap A[2] and A[3].
Index 0 1 2 3 4
A 14 27 33 10 35
Value A[0] A[1] A[2] A[3] A[4]
Swapping will be done with the help of a
third variable.
Index 0 1 2 3 4
A 14 27 33 10 35
Value A[0] A[1] A[2] A[3] A[4]

temp = A[2]
temp
Swapping will be done with the help of a
third variable.
Index 0 1 2 3 4
A 14 27 33 10 35
Value A[0] A[1] A[2] A[3] A[4]

temp = A[2]
temp 33
Swapping will be done with the help of a
third variable.
Index 0 1 2 3 4
A 14 27 33 10 35
Value A[0] A[1] A[2] A[3] A[4]

temp = A[2]
temp 33 A[2] = A[3]
Swapping will be done with the help of a
third variable.
Index 0 1 2 3 4
A 14 27 33 10 10 35
Value A[0] A[1] A[2] A[3] A[4]

temp = A[2]
temp 33 A[2] = A[3]
Swapping will be done with the help of a
third variable.
Index 0 1 2 3 4
A 14 27 10 10 35
Value A[0] A[1] A[2] A[3] A[4]

temp = A[2]
temp 33 A[2] = A[3]
Swapping will be done with the help of a
third variable.
Index 0 1 2 3 4
A 14 27 10 10 35
Value A[0] A[1] A[2] A[3] A[4]

temp = A[2]
temp 33 A[2] = A[3]
A[3] = temp
Swapping will be done with the help of a
third variable.
Index 0 1 2 3 4
A 14 27 10 10 33 35
Value A[0] A[1] A[2] A[3] A[4]

temp = A[2]
temp 33 A[2] = A[3]
A[3] = temp
Swapping will be done with the help of a
third variable.
Index 0 1 2 3 4
A 14 27 10 33 35
Value A[0] A[1] A[2] A[3] A[4]

temp = A[2]
temp 33 A[2] = A[3]
A[3] = temp
Swapping will be done with the help of a
third variable.
Index 0 1 2 3 4
A 14 27 10 33 35
Value A[0] A[1] A[2] A[3] A[4]
temp = A[2]
temp 33 A[2] = A[3]
A[3] = temp
Thus the array becomes:
Index 0 1 2 3 4
A 14 27 10 33 35
Value A[0] A[1] A[2] A[3] A[4]
Index 0 1 2 3 4
A 14 27 10 33 35
Value A[0] A[1] A[2] A[3] A[4]

Thus marks the end of second pass,


where the second largest element in the
array has occupied its correct position.
Third Pass
We proceed with the first and second element.
ie, A[0] and A[1].
Check if A[0] > A[1]
=> 14 > 27
=> FALSE
So, no swapping happens and the array
remains the same.
Index 0 1 2 3 4
A 14 27 10 33 35
Value A[0] A[1] A[2] A[3] A[4]
We proceed with the second and third element.
ie, A[1] and A[2].
Check if A[1] > A[2]
=> 27 > 10
=> TRUE
So, we swap A[1] and A[2].
Index 0 1 2 3 4
A 14 27 10 33 35
Value A[0] A[1] A[2] A[3] A[4]
Swapping will be done with the help of a
third variable.
Index 0 1 2 3 4
A 14 27 10 33 35
Value A[0] A[1] A[2] A[3] A[4]

temp = A[1]
temp
Swapping will be done with the help of a
third variable.
Index 0 1 2 3 4
A 14 27 10 33 35
Value A[0] A[1] A[2] A[3] A[4]

temp = A[1]
temp 27
Swapping will be done with the help of a
third variable.
Index 0 1 2 3 4
A 14 27 10 33 35
Value A[0] A[1] A[2] A[3] A[4]

temp = A[1]
temp 27 A[1] = A[2]
Swapping will be done with the help of a
third variable.
Index 0 1 2 3 4
A 14 27 10 10 33 35
Value A[0] A[1] A[2] A[3] A[4]

temp = A[1]
temp 27 A[1] = A[2]
Swapping will be done with the help of a
third variable.
Index 0 1 2 3 4
A 14 10 10 33 35
Value A[0] A[1] A[2] A[3] A[4]

temp = A[1]
temp 27 A[1] = A[2]
Swapping will be done with the help of a
third variable.
Index 0 1 2 3 4
A 14 10 10 33 35
Value A[0] A[1] A[2] A[3] A[4]

temp = A[1]
temp 27 A[1] = A[2]
A[2] = temp
Swapping will be done with the help of a
third variable.
Index 0 1 2 3 4
A 14 10 10 27 33 35
Value A[0] A[1] A[2] A[3] A[4]

temp = A[1]
temp 27 A[1] = A[2]
A[2] = temp
Swapping will be done with the help of a
third variable.
Index 0 1 2 3 4
A 14 10 27 33 35
Value A[0] A[1] A[2] A[3] A[4]

temp = A[1]
temp 27 A[1] = A[2]
A[2] = temp
Swapping will be done with the help of a
third variable.
Index 0 1 2 3 4
A 14 10 27 33 35
Value A[0] A[1] A[2] A[3] A[4]
temp = A[1]
temp 27 A[1] = A[2]
A[2] = temp
Thus the array becomes:
Index 0 1 2 3 4
A 14 10 27 33 35
Value A[0] A[1] A[2] A[3] A[4]
Index 0 1 2 3 4
A 14 10 27 33 35
Value A[0] A[1] A[2] A[3] A[4]

Thus marks the end of third pass, where


the next largest element in the array has
occupied its correct position.
Fourth Pass
We proceed with the first and second element.
ie, A[0] and A[1].
Check if A[0] > A[1]
=> 14 > 10
=> TRUE
So, we swap A[0] and A[1].
Index 0 1 2 3 4
A 14 10 27 33 35
Value A[0] A[1] A[2] A[3] A[4]
Swapping will be done with the help of a
third variable.
Index 0 1 2 3 4
A 14 10 27 33 35
Value A[0] A[1] A[2] A[3] A[4]

temp = A[0]
temp
Swapping will be done with the help of a
third variable.
Index 0 1 2 3 4
A 14 10 27 33 35
Value A[0] A[1] A[2] A[3] A[4]

temp = A[0]
temp 14
Swapping will be done with the help of a
third variable.
Index 0 1 2 3 4
A 14 10 27 33 35
Value A[0] A[1] A[2] A[3] A[4]

temp = A[0]
temp 14 A[0] = A[1]
Swapping will be done with the help of a
third variable.
Index 0 1 2 3 4
A 14 10 10 27 33 35
Value A[0] A[1] A[2] A[3] A[4]

temp = A[0]
temp 14 A[0] = A[1]
Swapping will be done with the help of a
third variable.
Index 0 1 2 3 4
A 10 10 27 33 35
Value A[0] A[1] A[2] A[3] A[4]

temp = A[0]
temp 14 A[0] = A[1]
Swapping will be done with the help of a
third variable.
Index 0 1 2 3 4
A 10 10 27 33 35
Value A[0] A[1] A[2] A[3] A[4]

temp = A[0]
temp 14 A[0] = A[1]
A[1] = temp
Swapping will be done with the help of a
third variable.
Index 0 1 2 3 4
A 10 10 14 27 33 35
Value A[0] A[1] A[2] A[3] A[4]

temp = A[0]
temp 14 A[0] = A[1]
A[1] = temp
Swapping will be done with the help of a
third variable.
Index 0 1 2 3 4
A 10 14 27 33 35
Value A[0] A[1] A[2] A[3] A[4]

temp = A[0]
temp 14 A[0] = A[1]
A[1] = temp
Swapping will be done with the help of a
third variable.
Index 0 1 2 3 4
A 10 14 27 33 35
Value A[0] A[1] A[2] A[3] A[4]
temp = A[0]
temp 14 A[0] = A[1]
A[1] = temp
Thus the array becomes:
Index 0 1 2 3 4
A 10 14 27 33 35
Value A[0] A[1] A[2] A[3] A[4]
Index 0 1 2 3 4
A 10 14 27 33 35
Value A[0] A[1] A[2] A[3] A[4]

Thus marks the end of fourth pass, where


the next largest element in the array has
occupied its correct position.
Fifth Pass
We proceed with the first and second element.
ie, A[0] and A[1].
Check if A[0] > A[1]
=> 10 > 14
=> FALSE
So, no swapping happens and the array
remains the same.
Index 0 1 2 3 4
A 10 14 27 33 35
Value A[0] A[1] A[2] A[3] A[4]
Index 0 1 2 3 4
A 10 14 27 33 35
Value A[0] A[1] A[2] A[3] A[4]

Thus marks the end of fifth pass, where


the next largest element in the array has
occupied its correct position.
After the nth pass, the nth largest
element(smallest element) will be at nth last
position (1st position) in the array, where ‘n’ is
the size of the array.

After doing all the passes, the array will be


sorted.

Thus, the sorted array will look like this:


Index 0 1 2 3 4
A 10 14 27 33 35
Value A[0] A[1] A[2] A[3] A[4]
Assessment Question
Index 0 1 2 3 4
A 8 22 31 11 5
Value A[0] A[1] A[2] A[3] A[4]
Sort this array in ascending order using bubble
sort.
Expected Output:
Index 0 1 2 3 4
A 5 8 11 22 31
Value A[0] A[1] A[2] A[3] A[4]
Write an algorithm to sort an array in
ascending order using bubble sort.

Problem Statement: To sort an array in


ascending order
using bubble sort.
• Write the algorithm
Step 1 : Start
Step 2 : Read size of the array, n
Step 3 : Set, i = 0
Step 4 : Check whether i < n, if it is true,
then go to Step _. Otherwise, go
to Step _.
Step 5 : Read a[i]
Step 6 : i = i + 1 go to Step _
• Write the algorithm
Step 1 : Start
Step 2 : Read size of the array, n
Step 3 : Set, i = 0
Step 4 : Check whether i < n, if it is true,
then go to Step 5. Otherwise, go
to Step 7.
Step 5 : Read a[i]
Step 6 : i = i + 1 go to Step 4
Step 7 : Set j = 1
Step 8 : Check whether j < n, if it is true,
then go to Step _. Otherwise, go
to Step _.
Step 9 : Set k = 0
Step 10 : Check whether k < n - j, if it is
true, then go to Step _.
Otherwise, go to Step _.
Step 11 : If a[k] > a[k + 1], then go to
Step _. Otherwise, go to Step _.
Step 12 : temp = a[k]
Step 13 : a[k] = a[k + 1]
Step 14 : a[k + 1] = temp
Step 15 : k = k + 1 go to Step _.
Step 16 : j = j + 1 go to Step _.
Step 17 : Set l = 0
Step 18 : Check whether l < n, if it is true,
then go to Step _. Otherwise, go to
Step _.
Step 19 : Print a[l]
Step 20 : l = l + 1 go to Step _.
Step 21 : Stop
Step 7 : Set j = 1
Step 8 : Check whether j < n, if it is true,
then go to Step 9. Otherwise, go
to Step 17.
Step 9 : Set k = 0
Step 10 : Check whether k < n - j, if it is
true, then go to Step 11.
Otherwise, go to Step 16.
Step 11 : If a[k] > a[k + 1], then go to
Step 12. Otherwise, go to Step
15.
Step 12 : temp = a[k]
Step 13 : a[k] = a[k + 1]
Step 14 : a[k + 1] = temp
Step 15 : k = k + 1 go to Step 10.
Step 16 : j = j + 1 go to Step 8.
Step 17 : Set l = 0
Step 18 : Check whether l < n, if it is true,
then go to Step 19. Otherwise, go to
Step 21.
Step 19 : Print a[l]
Step 20 : l = l + 1 go to Step 18.
Step 21 : Stop
• Write the algorithm
Step 1 : Start
Step 2 : Read size of the array, n
Step 3 : Set, i = 0
Step 4 : Check whether i < n, if it is true,
then go to Step 5. Otherwise, go
to Step 7.
Step 5 : Read a[i]
Step 6 : i = i + 1 go to Step 4
Step 7 : Set j = 1
Step 8 : Check whether j < n, if it is true,
then go to Step 9. Otherwise, go
to Step 17.
Step 9 : Set k = 0
Step 10 : Check whether k < n - j, if it is
true, then go to Step 11.
Otherwise, go to Step 16.
Step 11 : If a[k] > a[k + 1], then go to
Step 12. Otherwise, go to Step
15.
Step 12 : temp = a[k]
Step 13 : a[k] = a[k + 1]
Step 14 : a[k + 1] = temp
Step 15 : k = k + 1 go to Step 10.
Step 16 : j = j + 1 go to Step 8.
Step 17 : Set l = 0
Step 18 : Check whether l < n, if it is true,
then go to Step 19. Otherwise, go to
Step 21.
Step 19 : Print a[l]
Step 20 : l = l + 1 go to Step 18.
Step 21 : Stop
Draw the flow chart to sort an array in
ascending order using bubble sort.
Algorithm
Step 1 : Start
Step 2 : Read size of the array, n
Step 3 : Set, i = 0
Step 4 : Check whether i < n, if it is true,
then go to Step 5. Otherwise, go
to Step 7.
Step 5 : Read a[i]
Step 6 : i = i + 1 go to Step 4
Step 7 : Set j = 1
Step 8 : Check whether j < n, if it is true,
then go to Step 9. Otherwise, go
to Step 17.
Step 9 : Set k = 0
Step 10 : Check whether k < n - j, if it is
true, then go to Step 11.
Otherwise, go to Step 16.
Step 11 : If a[k] > a[k + 1], then go to
Step 12. Otherwise, go to Step
15.
Step 12 : temp = a[k]
Step 13 : a[k] = a[k + 1]
Step 14 : a[k + 1] = temp
Step 15 : k = k + 1 go to Step 10.
Step 16 : j = j + 1 go to Step 8.
Step 17 : Set l = 0
Step 18 : Check whether l < n, if it is true,
then go to Step 19. Otherwise, go to
Step 21.
Step 19 : Print a[l]
Step 20 : l = l + 1 go to Step 18.
Step 21 : Stop
Flow Chart
Algorithm
Step 1 : Start
Step 2 : Read size of the array, n
Step 3 : Set, i = 0
Step 4 : Check whether i < n, if it is true,
then go to Step 5. Otherwise, go
to Step 7.
Step 5 : Read a[i]
Step 6 : i = i + 1 go to Step 4
Flow Chart
Start
Algorithm
Step 1 : Start
Step 2 : Read size of the array, n
Step 3 : Set, i = 0
Step 4 : Check whether i < n, if it is true,
then go to Step 5. Otherwise, go
to Step 7.
Step 5 : Read a[i]
Step 6 : i = i + 1 go to Step 4
Flow Chart
Start

Read size of the array, n


Algorithm
Step 1 : Start
Step 2 : Read size of the array, n
Step 3 : Set, i = 0
Step 4 : Check whether i < n, if it is true,
then go to Step 5. Otherwise, go
to Step 7.
Step 5 : Read a[i]
Step 6 : i = i + 1 go to Step 4
Flow Chart
Start

Read size of the array, n

i=0
Algorithm
Step 1 : Start
Step 2 : Read size of the array, n
Step 3 : Set, i = 0
Step 4 : Check whether i < n, if it is true,
then go to Step 5. Otherwise, go
to Step 7.
Step 5 : Read a[i]
Step 6 : i = i + 1 go to Step 4
Flow Chart
Start

Read size of the array, n

i=0

True Check whether False


i<n
Algorithm
Step 1 : Start
Step 2 : Read size of the array, n
Step 3 : Set, i = 0
Step 4 : Check whether i < n, if it is true,
then go to Step 5. Otherwise, go
to Step 7.
Step 5 : Read a[i]
Step 6 : i = i + 1 go to Step 4
Algorithm
Step 1 : Start
Step 2 : Read size of the array, n
Step 3 : Set, i = 0
Step 4 : Check whether i < n, if it is true,
then go to Step 5. Otherwise, go
to Step 7.
Step 5 : Read a[i]
Step 6 : i = i + 1 go to Step 4
Algorithm
Step 1 : Start
Step 2 : Read size of the array, n
Step 3 : Set, i = 0
Step 4 : Check whether i < n, if it is true,
then go to Step 5. Otherwise, go
to Step 7.
Step 5 : Read a[i]
Step 6 : i = i + 1 go to Step 4
Algorithm
Step 1 : Start
Step 2 : Read size of the array, n
Step 3 : Set, i = 0
Step 4 : Check whether i < n, if it is true,
then go to Step 5. Otherwise, go
to Step 7.
Step 5 : Read a[i]
Step 6 : i = i + 1 go to Step 4
Flow Chart
Start

Read size of the array, n

i=0

True Check whether False


i<n
Read a[i]

i=i+1
Algorithm
Step 1 : Start
Step 2 : Read size of the array, n
Step 3 : Set, i = 0
Step 4 : Check whether i < n, if it is true,
then go to Step 5. Otherwise, go
to Step 7.
Step 5 : Read a[i]
Step 6 : i = i + 1 go to Step 4
Algorithm
Step 1 : Start
Step 2 : Read size of the array, n
Step 3 : Set, i = 0
Step 4 : Check whether i < n, if it is true,
then go to Step 5. Otherwise, go
to Step 7.
Step 5 : Read a[i]
Step 6 : i = i + 1 go to Step 4
Step 7 : Set j = 1
Step 8 : Check whether j < n, if it is true,
then go to Step 9. Otherwise, go
to Step 17.
Step 9 : Set k = 0
Step 10 : Check whether k < n - j, if it is
true, then go to Step 11.
Otherwise, go to Step 16.
Step 11 : If a[k] > a[k + 1], then go to
Step 12. Otherwise, go to Step
15.
Step 7 : Set j = 1
Step 8 : Check whether j < n, if it is true,
then go to Step 9. Otherwise, go
to Step 17.
Step 9 : Set k = 0
Step 10 : Check whether k < n - j, if it is
true, then go to Step 11.
Otherwise, go to Step 16.
Step 11 : If a[k] > a[k + 1], then go to
Step 12. Otherwise, go to Step
15.
Flow Chart
Start

Read size of the array, n

i=0

True Check whether False


i<n
Read a[i]

i=i+1
j=1
Step 7 : Set j = 1
Step 8 : Check whether j < n, if it is true,
then go to Step 9. Otherwise, go
to Step 17.
Step 9 : Set k = 0
Step 10 : Check whether k < n - j, if it is
true, then go to Step 11.
Otherwise, go to Step 16.
Step 11 : If a[k] > a[k + 1], then go to
Step 12. Otherwise, go to Step
15.
Flow Chart
Start

Read size of the array, n

i=0

True Check whether False


i<n
Read a[i]

i=i+1
j=1

True Check whether False


j<n
Step 7 : Set j = 1
Step 8 : Check whether j < n, if it is true,
then go to Step 9. Otherwise, go
to Step 17.
Step 9 : Set k = 0
Step 10 : Check whether k < n - j, if it is
true, then go to Step 11.
Otherwise, go to Step 16.
Step 11 : If a[k] > a[k + 1], then go to
Step 12. Otherwise, go to Step
15.
Step 7 : Set j = 1
Step 8 : Check whether j < n, if it is true,
then go to Step 9. Otherwise, go
to Step 17.
Step 9 : Set k = 0
Step 10 : Check whether k < n - j, if it is
true, then go to Step 11.
Otherwise, go to Step 16.
Step 11 : If a[k] > a[k + 1], then go to
Step 12. Otherwise, go to Step
15.
Step 7 : Set j = 1
Step 8 : Check whether j < n, if it is true,
then go to Step 9. Otherwise, go
to Step 17.
Step 9 : Set k = 0
Step 10 : Check whether k < n - j, if it is
true, then go to Step 11.
Otherwise, go to Step 16.
Step 11 : If a[k] > a[k + 1], then go to
Step 12. Otherwise, go to Step
15.
Flow Chart
Start

Read size of the array, n

i=0

True Check whether False


i<n
Read a[i]

i=i+1
j=1

True Check whether False


j<n
k=0
Step 7 : Set j = 1
Step 8 : Check whether j < n, if it is true,
then go to Step 9. Otherwise, go
to Step 17.
Step 9 : Set k = 0
Step 10 : Check whether k < n - j, if it is
true, then go to Step 11.
Otherwise, go to Step 16.
Step 11 : If a[k] > a[k + 1], then go to
Step 12. Otherwise, go to Step
15.
Flow Chart
Start

Read size of the array, n

i=0

True Check whether False


i<n
Read a[i]

i=i+1
j=1

True Check whether False


j<n
k=0

Check whether False


k<n-j
True
Step 7 : Set j = 1
Step 8 : Check whether j < n, if it is true,
then go to Step 9. Otherwise, go
to Step 17.
Step 9 : Set k = 0
Step 10 : Check whether k < n - j, if it is
true, then go to Step 11.
Otherwise, go to Step 16.
Step 11 : If a[k] > a[k + 1], then go to
Step 12. Otherwise, go to Step
15.
Step 7 : Set j = 1
Step 8 : Check whether j < n, if it is true,
then go to Step 9. Otherwise, go
to Step 17.
Step 9 : Set k = 0
Step 10 : Check whether k < n - j, if it is
true, then go to Step 11.
Otherwise, go to Step 16.
Step 11 : If a[k] > a[k + 1], then go to
Step 12. Otherwise, go to Step
15.
Step 7 : Set j = 1
Step 8 : Check whether j < n, if it is true,
then go to Step 9. Otherwise, go
to Step 17.
Step 9 : Set k = 0
Step 10 : Check whether k < n - j, if it is
true, then go to Step 11.
Otherwise, go to Step 16.
Step 11 : If a[k] > a[k + 1], then go to
Step 12. Otherwise, go to Step
15.
Flow Chart
Start

Read size of the array, n

i=0

True Check whether False


i<n
Read a[i]

i=i+1
j=1
True Check whether False
j<n
k=0
Check whether False
k<n-j
True
If
a[k] > a[k + 1]
True

False
Step 7 : Set j = 1
Step 8 : Check whether j < n, if it is true,
then go to Step 9. Otherwise, go
to Step 17.
Step 9 : Set k = 0
Step 10 : Check whether k < n - j, if it is
true, then go to Step 11.
Otherwise, go to Step 16.
Step 11 : If a[k] > a[k + 1], then go to
Step 12. Otherwise, go to Step
15.
Step 7 : Set j = 1
Step 8 : Check whether j < n, if it is true,
then go to Step 9. Otherwise, go
to Step 17.
Step 9 : Set k = 0
Step 10 : Check whether k < n - j, if it is
true, then go to Step 11.
Otherwise, go to Step 16.
Step 11 : If a[k] > a[k + 1], then go to
Step 12. Otherwise, go to Step
15.
Step 12 : temp = a[k]
Step 13 : a[k] = a[k + 1]
Step 14 : a[k + 1] = temp
Step 15 : k = k + 1 go to Step 10.
Step 16 : j = j + 1 go to Step 8.
Step 17 : Set l = 0
Step 18 : Check whether l < n, if it is true,
then go to Step 19. Otherwise, go to
Step 21.
Step 19 : Print a[l]
Step 20 : l = l + 1 go to Step 18.
Step 21 : Stop
Step 12 : temp = a[k]
Step 13 : a[k] = a[k + 1]
Step 14 : a[k + 1] = temp
Step 15 : k = k + 1 go to Step 10.
Step 16 : j = j + 1 go to Step 8.
Step 17 : Set l = 0
Step 18 : Check whether l < n, if it is true,
then go to Step 19. Otherwise, go to
Step 21.
Step 19 : Print a[l]
Step 20 : l = l + 1 go to Step 18.
Step 21 : Stop
Flow Chart
Start

Read size of the array, n

i=0

True Check whether False


i<n
Read a[i]

i=i+1
j=1
True Check whether False
j<n
k=0
Check whether False
k<n-j
True
If
a[k] > a[k + 1]
True

False temp = a[k]


a[k] = a[k+1]
a[k+1] = temp
Step 7 : Set j = 1
Step 8 : Check whether j < n, if it is true,
then go to Step 9. Otherwise, go
to Step 17.
Step 9 : Set k = 0
Step 10 : Check whether k < n - j, if it is
true, then go to Step 11.
Otherwise, go to Step 16.
Step 11 : If a[k] > a[k + 1], then go to
Step 12. Otherwise, go to Step
15.
Step 7 : Set j = 1
Step 8 : Check whether j < n, if it is true,
then go to Step 9. Otherwise, go
to Step 17.
Step 9 : Set k = 0
Step 10 : Check whether k < n - j, if it is
true, then go to Step 11.
Otherwise, go to Step 16.
Step 11 : If a[k] > a[k + 1], then go to
Step 12. Otherwise, go to Step
15.
Step 12 : temp = a[k]
Step 13 : a[k] = a[k + 1]
Step 14 : a[k + 1] = temp
Step 15 : k = k + 1 go to Step 10.
Step 16 : j = j + 1 go to Step 8.
Step 17 : Set l = 0
Step 18 : Check whether l < n, if it is true,
then go to Step 19. Otherwise, go to
Step 21.
Step 19 : Print a[l]
Step 20 : l = l + 1 go to Step 18.
Step 21 : Stop
Step 12 : temp = a[k]
Step 13 : a[k] = a[k + 1]
Step 14 : a[k + 1] = temp
Step 15 : k = k + 1 go to Step 10.
Step 16 : j = j + 1 go to Step 8.
Step 17 : Set l = 0
Step 18 : Check whether l < n, if it is true,
then go to Step 19. Otherwise, go to
Step 21.
Step 19 : Print a[l]
Step 20 : l = l + 1 go to Step 18.
Step 21 : Stop
Flow Chart
Start

Read size of the array, n

i=0

True Check whether False


i<n
Read a[i]

i=i+1
j=1
True Check whether False
j<n
k=0
Check whether False
k<n-j
True
If
a[k] > a[k + 1]
True

False temp = a[k]


k=k+1 a[k] = a[k+1]
a[k+1] = temp
}
Step 12 : temp = a[k]
Step 13 : a[k] = a[k + 1]
Step 14 : a[k + 1] = temp
Step 15 : k = k + 1 go to Step 10.
Step 16 : j = j + 1 go to Step 8.
Step 17 : Set l = 0
Step 18 : Check whether l < n, if it is true,
then go to Step 19. Otherwise, go to
Step 21.
Step 19 : Print a[l]
Step 20 : l = l + 1 go to Step 18.
Step 21 : Stop
Flow Chart
Start

Read size of the array, n

i=0

True Check whether False


i<n
Read a[i]

i=i+1
j=1
True Check whether False
j<n
k=0
Check whether False
k<n-j
True
If
a[k] > a[k + 1]
True

False temp = a[k]


k=k+1 a[k] = a[k+1]
a[k+1] = temp
Step 7 : Set j = 1
Step 8 : Check whether j < n, if it is true,
then go to Step 9. Otherwise, go
to Step 17.
Step 9 : Set k = 0
Step 10 : Check whether k < n - j, if it is
true, then go to Step 11.
Otherwise, go to Step 16.
Step 11 : If a[k] > a[k + 1], then go to
Step 12. Otherwise, go to Step
15.
Step 7 : Set j = 1
Step 8 : Check whether j < n, if it is true,
then go to Step 9. Otherwise, go
to Step 17.
Step 9 : Set k = 0
Step 10 : Check whether k < n - j, if it is
true, then go to Step 11.
Otherwise, go to Step 16.
Step 11 : If a[k] > a[k + 1], then go to
Step 12. Otherwise, go to Step
15.
Step 12 : temp = a[k]
Step 13 : a[k] = a[k + 1]
Step 14 : a[k + 1] = temp
Step 15 : k = k + 1 go to Step 10.
Step 16 : j = j + 1 go to Step 8.
Step 17 : Set l = 0
Step 18 : Check whether l < n, if it is true,
then go to Step 19. Otherwise, go to
Step 21.
Step 19 : Print a[l]
Step 20 : l = l + 1 go to Step 18.
Step 21 : Stop
Step 12 : temp = a[k]
Step 13 : a[k] = a[k + 1]
Step 14 : a[k + 1] = temp
Step 15 : k = k + 1 go to Step 10.
Step 16 : j = j + 1 go to Step 8.
Step 17 : Set l = 0
Step 18 : Check whether l < n, if it is true,
then go to Step 19. Otherwise, go to
Step 21.
Step 19 : Print a[l]
Step 20 : l = l + 1 go to Step 18.
Step 21 : Stop
Flow Chart
Start

Read size of the array, n

i=0

True Check whether False


i<n
Read a[i]

i=i+1
j=1
True Check whether False
j<n
k=0
Check whether False
k<n-j
True j=j+1
If
a[k] > a[k + 1]
True

False temp = a[k]


k=k+1 a[k] = a[k+1]
a[k+1] = temp
Step 7 : Set j = 1
Step 8 : Check whether j < n, if it is true,
then go to Step 9. Otherwise, go
to Step 17.
Step 9 : Set k = 0
Step 10 : Check whether k < n - j, if it is
true, then go to Step 11.
Otherwise, go to Step 16.
Step 11 : If a[k] > a[k + 1], then go to
Step 12. Otherwise, go to Step
15.
Step 7 : Set j = 1
Step 8 : Check whether j < n, if it is true,
then go to Step 9. Otherwise, go
to Step 17.
Step 9 : Set k = 0
Step 10 : Check whether k < n - j, if it is
true, then go to Step 11.
Otherwise, go to Step 16.
Step 11 : If a[k] > a[k + 1], then go to
Step 12. Otherwise, go to Step
15.
Step 12 : temp = a[k]
Step 13 : a[k] = a[k + 1]
Step 14 : a[k + 1] = temp
Step 15 : k = k + 1 go to Step 10.
Step 16 : j = j + 1 go to Step 8.
Step 17 : Set l = 0
Step 18 : Check whether l < n, if it is true,
then go to Step 19. Otherwise, go to
Step 21.
Step 19 : Print a[l]
Step 20 : l = l + 1 go to Step 18.
Step 21 : Stop
Step 12 : temp = a[k]
Step 13 : a[k] = a[k + 1]
Step 14 : a[k + 1] = temp
Step 15 : k = k + 1 go to Step 10.
Step 16 : j = j + 1 go to Step 8.
Step 17 : Set l = 0
Step 18 : Check whether l < n, if it is true,
then go to Step 19. Otherwise, go to
Step 21.
Step 19 : Print a[l]
Step 20 : l = l + 1 go to Step 18.
Step 21 : Stop
Flow Chart
Start

Read size of the array, n

i=0

True Check whether False


i<n
Read a[i]

i=i+1
j=1
True Check whether False
j<n
k=0 l=0
Check whether False
k<n-j
True j=j+1
If
a[k] > a[k + 1]
True

False temp = a[k]


k=k+1 a[k] = a[k+1]
a[k+1] = temp
Step 12 : temp = a[k]
Step 13 : a[k] = a[k + 1]
Step 14 : a[k + 1] = temp
Step 15 : k = k + 1 go to Step 10.
Step 16 : j = j + 1 go to Step 8.
Step 17 : Set l = 0
Step 18 : Check whether l < n, if it is true,
then go to Step 19. Otherwise, go to
Step 21.
Step 19 : Print a[l]
Step 20 : l = l + 1 go to Step 18.
Step 21 : Stop
Flow Chart
Start

Read size of the array, n

i=0

True Check whether False


i<n
Read a[i]

i=i+1
j=1
True Check whether False
j<n
k=0 l=0
Check whether False False Check whether
k<n-j l<n
True j=j+1
True
If
a[k] > a[k + 1]
True

False temp = a[k]


k=k+1 a[k] = a[k+1]
a[k+1] = temp
Step 12 : temp = a[k]
Step 13 : a[k] = a[k + 1]
Step 14 : a[k + 1] = temp
Step 15 : k = k + 1 go to Step 10.
Step 16 : j = j + 1 go to Step 8.
Step 17 : Set l = 0
Step 18 : Check whether l < n, if it is true,
then go to Step 19. Otherwise, go to
Step 21.
Step 19 : Print a[l]
Step 20 : l = l + 1 go to Step 18.
Step 21 : Stop
Step 12 : temp = a[k]
Step 13 : a[k] = a[k + 1]
Step 14 : a[k + 1] = temp
Step 15 : k = k + 1 go to Step 10.
Step 16 : j = j + 1 go to Step 8.
Step 17 : Set l = 0
Step 18 : Check whether l < n, if it is true,
then go to Step 19. Otherwise, go to
Step 21.
Step 19 : Print a[l]
Step 20 : l = l + 1 go to Step 18.
Step 21 : Stop
Step 12 : temp = a[k]
Step 13 : a[k] = a[k + 1]
Step 14 : a[k + 1] = temp
Step 15 : k = k + 1 go to Step 10.
Step 16 : j = j + 1 go to Step 8.
Step 17 : Set l = 0
Step 18 : Check whether l < n, if it is true,
then go to Step 19. Otherwise, go to
Step 21.
Step 19 : Print a[l]
Step 20 : l = l + 1 go to Step 18.
Step 21 : Stop
Flow Chart
Start

Read size of the array, n

i=0

True Check whether False


i<n
Read a[i]

i=i+1
j=1
True Check whether False
j<n
k=0 l=0
Check whether False False Check whether
k<n-j l<n
True j=j+1
True
If
a[k] > a[k + 1]
True
Print a[l]
False temp = a[k]
k=k+1 a[k] = a[k+1] l=l+1
a[k+1] = temp
Step 12 : temp = a[k]
Step 13 : a[k] = a[k + 1]
Step 14 : a[k + 1] = temp
Step 15 : k = k + 1 go to Step 10.
Step 16 : j = j + 1 go to Step 8.
Step 17 : Set l = 0
Step 18 : Check whether l < n, if it is true,
then go to Step 19. Otherwise, go to
Step 21.
Step 19 : Print a[l]
Step 20 : l = l + 1 go to Step 18.
Step 21 : Stop
Step 12 : temp = a[k]
Step 13 : a[k] = a[k + 1]
Step 14 : a[k + 1] = temp
Step 15 : k = k + 1 go to Step 10.
Step 16 : j = j + 1 go to Step 8.
Step 17 : Set l = 0
Step 18 : Check whether l < n, if it is true,
then go to Step 19. Otherwise, go to
Step 21.
Step 19 : Print a[l]
Step 20 : l = l + 1 go to Step 18.
Step 21 : Stop
Step 12 : temp = a[k]
Step 13 : a[k] = a[k + 1]
Step 14 : a[k + 1] = temp
Step 15 : k = k + 1 go to Step 10.
Step 16 : j = j + 1 go to Step 8.
Step 17 : Set l = 0
Step 18 : Check whether l < n, if it is true,
then go to Step 19. Otherwise, go to
Step 21.
Step 19 : Print a[l]
Step 20 : l = l + 1 go to Step 18.
Step 21 : Stop
Flow Chart
Start

Read size of the array, n

i=0

True Check whether False


i<n
Read a[i]

i=i+1
j=1
True Check whether False
j<n
k=0 l=0
Check whether False False Check whether
k<n-j l<n
True j=j+1
True
If
a[k] > a[k + 1]
True
Print a[l]
False temp = a[k]
k=k+1 a[k] = a[k+1] l=l+1
a[k+1] = temp

Stop
Write the pseudocode to sort an array in
ascending order using bubble sort.
Pseudocode
Algorithm
Step 1 : Start
Step 2 : Read size of the array, n
Step 3 : Set, i = 0
Step 4 : Check whether i < n, if it is true,
then go to Step 5. Otherwise, go
to Step 7.
Step 5 : Read a[i]
Step 6 : i = i + 1 go to Step 4
Step 7 : Set j = 1
Step 8 : Check whether j < n, if it is true,
then go to Step 9. Otherwise, go
to Step 17.
Step 9 : Set k = 0
Step 10 : Check whether k < n - j, if it is
true, then go to Step 11.
Otherwise, go to Step 16.
Step 11 : If a[k] > a[k + 1], then go to
Step 12. Otherwise, go to Step
15.
Step 12 : temp = a[k]
Step 13 : a[k] = a[k + 1]
Step 14 : a[k + 1] = temp
Step 15 : k = k + 1 go to Step 10.
Step 16 : j = j + 1 go to Step 8.
Step 17 : Set l = 0
Step 18 : Check whether l < n, if it is true,
then go to Step 19. Otherwise, go to
Step 21.
Step 19 : Print a[l]
Step 20 : l = l + 1 go to Step 18.
Step 21 : Stop
Algorithm
Step 1 : Start
Step 2 : Read size of the array, n
Step 3 : Set, i = 0
Step 4 : Check whether i < n, if it is true,
then go to Step 5. Otherwise, go
to Step 7.
Step 5 : Read a[i]
Step 6 : i = i + 1 go to Step 4
Algorithm
Step 1 : Start
Step 2 : Read size of the array, n
Step 3 : Set, i = 0
Step 4 : Check whether i < n, if it is true,
then go to Step 5. Otherwise, go
to Step 7.
Step 5 : Read a[i]
Step 6 : i = i + 1 go to Step 4
Pseudocode
READ n
Algorithm
Step 1 : Start
Step 2 : Read size of the array, n
Step 3 : Set, i = 0
Step 4 : Check whether i < n, if it is true,
then go to Step 5. Otherwise, go
to Step 7.
Step 5 : Read a[i]
Step 6 : i = i + 1 go to Step 4
Pseudocode
READ n
SET i = 0
Algorithm
Step 1 : Start
Step 2 : Read size of the array, n
Step 3 : Set, i = 0
Step 4 : Check whether i < n, if it is true,
then go to Step 5. Otherwise, go
to Step 7.
Step 5 : Read a[i]
Step 6 : i = i + 1 go to Step 4
Pseudocode
READ n
SET i = 0
WHILE i < n
READ a[i]
i=i+1
ENDWHILE
Step 7 : Set j = 1
Step 8 : Check whether j < n, if it is true,
then go to Step 9. Otherwise, go
to Step 17.
Step 9 : Set k = 0
Step 10 : Check whether k < n - j, if it is
true, then go to Step 11.
Otherwise, go to Step 16.
Step 11 : If a[k] > a[k + 1], then go to
Step 12. Otherwise, go to Step
15.
Step 7 : Set j = 1
Step 8 : Check whether j < n, if it is true,
then go to Step 9. Otherwise, go
to Step 17.
Step 9 : Set k = 0
Step 10 : Check whether k < n - j, if it is
true, then go to Step 11.
Otherwise, go to Step 16.
Step 11 : If a[k] > a[k + 1], then go to
Step 12. Otherwise, go to Step
15.
Pseudocode
READ n
SET i = 0
WHILE i < n
READ a[i]
i=i+1
ENDWHILE
SET j = 1
Step 7 : Set j = 1
Step 8 : Check whether j < n, if it is true,
then go to Step 9. Otherwise, go
to Step 17.
Step 9 : Set k = 0
Step 10 : Check whether k < n - j, if it is
true, then go to Step 11.
Otherwise, go to Step 16.
Step 11 : If a[k] > a[k + 1], then go to
Step 12. Otherwise, go to Step
15.
Pseudocode
READ n
SET i = 0
WHILE i < n
READ a[i]
i=i+1
ENDWHILE
SET j = 1
WHILE j < n

ENDWHILE
Step 7 : Set j = 1
Step 8 : Check whether j < n, if it is true,
then go to Step 9. Otherwise, go
to Step 17.
Step 9 : Set k = 0
Step 10 : Check whether k < n - j, if it is
true, then go to Step 11.
Otherwise, go to Step 16.
Step 11 : If a[k] > a[k + 1], then go to
Step 12. Otherwise, go to Step
15.
Step 7 : Set j = 1
Step 8 : Check whether j < n, if it is true,
then go to Step 9. Otherwise, go
to Step 17.
Step 9 : Set k = 0
Step 10 : Check whether k < n - j, if it is
true, then go to Step 11.
Otherwise, go to Step 16.
Step 11 : If a[k] > a[k + 1], then go to
Step 12. Otherwise, go to Step
15.
Pseudocode
READ n
SET i = 0
WHILE i < n
READ a[i]
i=i+1
ENDWHILE
SET j = 1
WHILE j < n
SET k = 0
ENDWHILE
Step 7 : Set j = 1
Step 8 : Check whether j < n, if it is true,
then go to Step 9. Otherwise, go
to Step 17.
Step 9 : Set k = 0
Step 10 : Check whether k < n - j, if it is
true, then go to Step 11.
Otherwise, go to Step 16.
Step 11 : If a[k] > a[k + 1], then go to
Step 12. Otherwise, go to Step
15.
Step 7 : Set j = 1
Step 8 : Check whether j < n, if it is true,
then go to Step 9. Otherwise, go
to Step 17.
Step 9 : Set k = 0
Step 10 : Check whether k < n - j, if it is
true, then go to Step 11.
Otherwise, go to Step 16.
Step 11 : If a[k] > a[k + 1], then go to
Step 12. Otherwise, go to Step
15.
Pseudocode
READ n
SET i = 0
WHILE i < n
READ a[i]
i=i+1
ENDWHILE
SET j = 1
WHILE j < n
SET k = 0
WHILE k < n - j

ENDWHILE
ENDWHILE
Step 7 : Set j = 1
Step 8 : Check whether j < n, if it is true,
then go to Step 9. Otherwise, go
to Step 17.
Step 9 : Set k = 0
Step 10 : Check whether k < n - j, if it is
true, then go to Step 11.
Otherwise, go to Step 16.
Step 11 : If a[k] > a[k + 1], then go to
Step 12. Otherwise, go to Step
15.
Pseudocode
READ n
SET i = 0
WHILE i < n
READ a[i]
i=i+1
ENDWHILE
SET j = 1
WHILE j < n
SET k = 0
WHILE k < n - j
IF a[k] > a[k + 1]
THEN

ENDIF
ENDWHILE
ENDWHILE
Step 7 : Set j = 1
Step 8 : Check whether j < n, if it is true,
then go to Step 9. Otherwise, go
to Step 17.
Step 9 : Set k = 0
Step 10 : Check whether k < n - j, if it is
true, then go to Step 11.
Otherwise, go to Step 16.
Step 11 : If a[k] > a[k + 1], then go to
Step 12. Otherwise, go to Step
15.
Step 12 : temp = a[k]
Step 13 : a[k] = a[k + 1]
Step 14 : a[k + 1] = temp
Step 15 : k = k + 1 go to Step 10.
Step 16 : j = j + 1 go to Step 8.
Step 17 : Set l = 0
Step 18 : Check whether l < n, if it is true,
then go to Step 19. Otherwise, go to
Step 21.
Step 19 : Print a[l]
Step 20 : l = l + 1 go to Step 18.
Step 21 : Stop
Step 12 : temp = a[k]
Step 13 : a[k] = a[k + 1]
Step 14 : a[k + 1] = temp
Step 15 : k = k + 1 go to Step 10.
Step 16 : j = j + 1 go to Step 8.
Step 17 : Set l = 0
Step 18 : Check whether l < n, if it is true,
then go to Step 19. Otherwise, go to
Step 21.
Step 19 : Print a[l]
Step 20 : l = l + 1 go to Step 18.
Step 21 : Stop
Pseudocode
READ n
SET i = 0
WHILE i < n
READ a[i]
i=i+1
ENDWHILE
SET j = 1
WHILE j < n
SET k = 0
WHILE k < n - j
IF a[k] > a[k + 1]
THEN
temp = a[k]
a[k] = a[k + 1]
a[k + 1] = temp
ENDIF
ENDWHILE
ENDWHILE
Step 7 : Set j = 1
Step 8 : Check whether j < n, if it is true,
then go to Step 9. Otherwise, go
to Step 17.
Step 9 : Set k = 0
Step 10 : Check whether k < n - j, if it is
true, then go to Step 11.
Otherwise, go to Step 16.
Step 11 : If a[k] > a[k + 1], then go to
Step 12. Otherwise, go to Step
15.
Step 12 : temp = a[k]
Step 13 : a[k] = a[k + 1]
Step 14 : a[k + 1] = temp
Step 15 : k = k + 1 go to Step 10.
Step 16 : j = j + 1 go to Step 8.
Step 17 : Set l = 0
Step 18 : Check whether l < n, if it is true,
then go to Step 19. Otherwise, go to
Step 21.
Step 19 : Print a[l]
Step 20 : l = l + 1 go to Step 18.
Step 21 : Stop
Pseudocode
READ n
SET i = 0
WHILE i < n
READ a[i]
i=i+1
ENDWHILE
SET j = 1
WHILE j < n
SET k = 0
WHILE k < n - j
IF a[k] > a[k + 1]
THEN
temp = a[k]
a[k] = a[k + 1]
a[k + 1] = temp
ENDIF
ENDWHILE
ENDWHILE
WHILE j < n
SET k = 0
WHILE k < n - j
IF a[k] > a[k + 1]
THEN
temp = a[k]
a[k] = a[k + 1]
a[k + 1] = temp
ENDIF
k=k+1
ENDWHILE
ENDWHILE
Step 7 : Set j = 1
Step 8 : Check whether j < n, if it is true,
then go to Step 9. Otherwise, go
to Step 17.
Step 9 : Set k = 0
Step 10 : Check whether k < n - j, if it is
true, then go to Step 11.
Otherwise, go to Step 16.
Step 11 : If a[k] > a[k + 1], then go to
Step 12. Otherwise, go to Step
15.
Step 12 : temp = a[k]
Step 13 : a[k] = a[k + 1]
Step 14 : a[k + 1] = temp
Step 15 : k = k + 1 go to Step 10.
Step 16 : j = j + 1 go to Step 8.
Step 17 : Set l = 0
Step 18 : Check whether l < n, if it is true,
then go to Step 19. Otherwise, go to
Step 21.
Step 19 : Print a[l]
Step 20 : l = l + 1 go to Step 18.
Step 21 : Stop
Pseudocode
READ n
SET i = 0
WHILE i < n
READ a[i]
i=i+1
ENDWHILE
SET j = 1
WHILE j < n
SET k = 0
WHILE k < n - j
IF a[k] > a[k + 1]
THEN
temp = a[k]
a[k] = a[k + 1]
a[k + 1] = temp
ENDIF
k=k+1
ENDWHILE
ENDWHILE
WHILE j < n
SET k = 0
WHILE k < n - j
IF a[k] > a[k + 1]
THEN
temp = a[k]
a[k] = a[k + 1]
a[k + 1] = temp
ENDIF
k=k+1
ENDWHILE
j=j+1
ENDWHILE
Step 12 : temp = a[k]
Step 13 : a[k] = a[k + 1]
Step 14 : a[k + 1] = temp
Step 15 : k = k + 1 go to Step 10.
Step 16 : j = j + 1 go to Step 8.
Step 17 : Set l = 0
Step 18 : Check whether l < n, if it is true,
then go to Step 19. Otherwise, go to
Step 21.
Step 19 : Print a[l]
Step 20 : l = l + 1 go to Step 18.
Step 21 : Stop
Pseudocode
READ n
SET i = 0
WHILE i < n
READ a[i]
i=i+1
ENDWHILE
SET j = 1
WHILE j < n
SET k = 0
WHILE k < n - j
IF a[k] > a[k + 1]
THEN
temp = a[k]
a[k] = a[k + 1]
a[k + 1] = temp
ENDIF
k=k+1
ENDWHILE
j=j+1
ENDWHILE
SET l = 0
Step 12 : temp = a[k]
Step 13 : a[k] = a[k + 1]
Step 14 : a[k + 1] = temp
Step 15 : k = k + 1 go to Step 10.
Step 16 : j = j + 1 go to Step 8.
Step 17 : Set l = 0
Step 18 : Check whether l < n, if it is true,
then go to Step 19. Otherwise, go to
Step 21.
Step 19 : Print a[l]
Step 20 : l = l + 1 go to Step 18.
Step 21 : Stop
Pseudocode
READ n
SET i = 0
WHILE i < n
READ a[i]
i=i+1
ENDWHILE
SET j = 1
WHILE j < n
SET k = 0
WHILE k < n - j
IF a[k] > a[k + 1]
THEN
temp = a[k]
a[k] = a[k + 1]
a[k + 1] = temp
ENDIF
k=k+1
ENDWHILE
j=j+1
ENDWHILE
SET l = 0
WHILE l < n
PRINT a[l]
l=l+1
ENDWHILE
Pseudocode
READ n
SET i = 0
WHILE i < n
READ a[i]
i=i+1
ENDWHILE
SET j = 1
WHILE j < n
SET k = 0
WHILE k < n - j
IF a[k] > a[k + 1]
THEN
temp = a[k]
a[k] = a[k + 1]
a[k + 1] = temp
ENDIF
k=k+1
ENDWHILE
j=j+1
ENDWHILE
SET l = 0
WHILE l < n
PRINT a[l]
l=l+1
ENDWHILE
Thank you…….

You might also like