Algorithm and flowchart with pseudo code

Introduction to Algorithms
Solving Problems (1)
When faced with a problem:
1. We first clearly define the problem
2. Think of possible solutions
3. Select the one that we think is the best
under the prevailing circumstances
4. And then apply that solution
5. If the solution works as desired, fine;
else we go back to step 2
Solving Problems (2)
 It is quite common to first solve a problem for a particular case
 Then for another
 And, possibly another
 And watch for patterns and trends that emerge
 And to use the knowledge form those patterns and trends in coming up with
a general solution
Solving Problems (3)
 It helps if we have experienced that problem or similar ones before
 Generally, there are many ways of solving a given problem; the best problem-
solvers come-up with the most appropriate solution more often than not!
 The process that can be used to solve a problem is termed as the “algorithm”
Examples
 Addition
 Conversion from decimal to binary
 The process of boiling an egg
 The process of mailing a letter
 Sorting
 Searching
Let us write down the algorithm for a problem
that is familiar to us
Converting a decimal number into binary
Convert 75 to Binary
752
37 12
18 12
9 02
4 12
2 02
1 02
0 1
1001011
remainder
Algorithm for Decimal-to-Binary Conversion
1. Write the decimal number
2. Divide by 2; write quotient and remainder
3. Repeat step 2 on the quotient; keep on repeating until the quotient becomes
zero
4. Write all remainder digits in the reverse order (last remainder first) to form the
final result
Points to Note:
1. The process consists of repeated application of simple steps
2. All steps are unambiguous (clearly defined)
3. We are capable of doing all those steps
4. Only a limited no. of steps needs to be taken
5. Once all those steps are taken according to the prescribed sequence, the
required result will be found
6. Moreover, the process will stop at that point
Algorithm (Better Definition)
1st Definition:
Sequence of steps that can be taken to solve a problem
Better Definition:
A precise sequence of a limited number of unambiguous, executable
steps that terminates in the form of a solution
Three Requirements:
1. Sequence is:
a. Precise
b. Consists of a limited number of steps
2. Each step is:
a. Unambiguous
b. Executable
3. The sequence of steps terminates in the form of a solution
Why Algorithms are Useful?
 Once we find an algorithm for solving a problem, we do not need to re-
discover it the next time we are faced with that problem
 Once an algorithm is known, the task of solving the problem reduces to
following (almost blindly and without thinking) the instructions precisely
 All the knowledge required for solving the problem is present in the algorithm
Analysis of Algorithms
 Analysis in the context of algorithms is concerned with predicting the resources
that are requires:
 Computational time
 Memory
 Bandwidth
 Logic functions
 However, Time – generally measured in terms of the number of steps required to
execute an algorithm - is the resource of most interest
 By analyzing several candidate algorithms, the most efficient one(s) can be
identified
Selecting Among Algorithms
When choosing among competing, successful solutions to a problem, choose the
one which is the least complex
This principle is called the “Ockham’s Razor,” after William of Ockham - famous
13-th century English philosopher
Syntax & Semantics
An algorithm is “correct” if its:
 Semantics are correct
 Syntax is correct
Semantics:
The concept embedded in an
algorithm (the soul!)
Syntax:
The actual representation of an
algorithm (the body!)
WARNINGS:
1. An algorithm can be
syntactically correct, yet
semantically incorrect –
very dangerous situation!
2. Syntactic correctness is
easier to check as
compared with semantic
Now onto Algorithm Representation
 We have said enough about algorithms – their definition, their types, etc.
 But, how do we actually represent them?
 Generally, SW developers represent them in one of three forms:
 Pseudo code
 Flowcharts
 Actual code
Pseudo Code
 Language that is typically used for writing algorithms
 Similar to a programming language, but not as rigid
 The method of expression most suitable for a given situation is used:
 At times, plain English
 At others, a programming language like syntax
Flowchart
 A graphical representation of a process (e.g. an algorithm), in which graphic
objects are used to indicate the steps & decisions that are taken as the process
moves along from start to finish
 Individual steps are represented by boxes and other shapes on the flowchart, with
arrows between those shapes indicating the order in which the steps are taken
Start or stop
Process
Input or output
Connector
Decision
Flow line
Off-page connector
Flowchart
Elements
Algorithm Building Blocks
All problems can be solved by employing any one of the following
building blocks or their combinations
1. Sequences
2. Conditionals
3. Loops
Sequences
A sequence of instructions that are executed in the precise order they
are written in:
statement block 1
statement block 2
statement block 3
statement block 1
statement block 2
statement block 3
Conditionals
Select between alternate courses of action depending upon the evaluation of a
condition
If ( condition = true )
statement block 1
Else
statement block 2
End if statement
block 1
condition
True False
statement
block 2
Loops
Loop through a set of statements as long as a condition is true
Loop while ( condition = true )
statement block
End Loop
condition
True
False
statement
block
Problem Statement
Convert a decimal number into binary
Convert 75 to Binary
752
37 12
18 12
9 02
4 12
2 02
1 02
0 1
1001011
remainder
Solution in Pseudo Code
1. Let the decimal number be an integer x, x > 0
2. Let the binary equivalent be an empty string y
3. Repeat while x > 0 {
Determine the quotient & remainder of x ÷ 2
y = CONCATENATE( remainder, y )
x = quotient
}
4. Print y
5. Stop
Start
Find quotient
& remainder
of x ÷ 2
Get x
x>0 ?
Print y
Stop
y = CONC(remainder, x)
x = quotient
x is the decimal number
y is the binary equivalent
Flowchart of Decimal
to Binary Conversion
Yes
No
Another Example: Sorting
Sort the following objects w.r.t. their heights
Expected Result
Strategy
There are many strategies for solving this problem. We demonstrate a simple one:
Repeat the following steps while the list is un-
sorted:
Start with the first object in the list
Swap it with the one next to it if they are in the wrong
order
Repeat the same with the next to the first object
Keep on repeating until you reach the last object in the
list
Back to the Objects to be Sorted
Sorting: Step A1
Sorting: Step A1
Swap? Yes
Sorting: Step A2
Sorting: Step A2
Swap? Yes
Sorting: Step A3
Sorting: Step A3
Swap? No
Sorting: After Step A7
Q: Is the list sorted?
A: No
Sorting: Step B1
Sorting: Step B1
Swap? Yes
Sorting: Step B2
Sorting: Step B2
Swap? No
Sorting: After Step B7
Q: Is the list sorted?
A: No
Sorting: Step C1
Sorting: Step C1
Swap? No
Sorting: After Step C7
Q: Is the list sorted?
A: Yes
Algorithm and flowchart with pseudo code
Algorithm and flowchart with pseudo code
 A number is even if it can be divided by 2 without remainder. Such numbers
are 2, 4, 6, 8.. and so on. The numbers that leave a remainder are called
odd. They are 1, 3, 5, 7.. and so on.
 In programming we find the remainder of a division with the operator %. Also
we use the double equals “==” to compare values for equality.
Algorithm and flowchart with pseudo code
 Summing two numbers was easy – the calculation was one block from the flow
chart. But how about 50? Do you have to write 50 blocks to solve this task?
Happily – no.
 You can automate this process by repeatedly incrementing the value of a
variable and checking it every time if it exceeds the last value – 50. Then sum
that number every step and... there you go! This construction is called loop.
Algorithm and flowchart with pseudo code
Find the biggest of 100 prices and reduce it
by 10%
1 de 56

Recomendados

Pseudo code por
Pseudo codePseudo code
Pseudo codeArindam Ghosh
8.9K visualizações17 slides
Algorithm and flowchart por
Algorithm and flowchartAlgorithm and flowchart
Algorithm and flowchartSachin Goyani
7.1K visualizações34 slides
Algorithm and flowchart por
Algorithm and flowchartAlgorithm and flowchart
Algorithm and flowchartRabin BK
10.7K visualizações17 slides
Algorithms and Flowcharts por
Algorithms and FlowchartsAlgorithms and Flowcharts
Algorithms and FlowchartsDeva Singh
309.9K visualizações31 slides
Basics of c++ Programming Language por
Basics of c++ Programming LanguageBasics of c++ Programming Language
Basics of c++ Programming LanguageAhmad Idrees
47.8K visualizações78 slides
Unit 1-problem solving with algorithm por
Unit 1-problem solving with algorithmUnit 1-problem solving with algorithm
Unit 1-problem solving with algorithmrajkumar1631010038
6.9K visualizações26 slides

Mais conteúdo relacionado

Mais procurados

Context free grammar por
Context free grammar Context free grammar
Context free grammar Mohammad Ilyas Malik
12.3K visualizações31 slides
INTRODUCTION TO C PROGRAMMING por
INTRODUCTION TO C PROGRAMMINGINTRODUCTION TO C PROGRAMMING
INTRODUCTION TO C PROGRAMMINGAbhishek Dwivedi
208.4K visualizações142 slides
Algorithm Design & Implementation por
Algorithm Design & ImplementationAlgorithm Design & Implementation
Algorithm Design & ImplementationGaditek
3.9K visualizações19 slides
Sorting por
SortingSorting
SortingAshim Lamichhane
43.7K visualizações62 slides
Control statements por
Control statementsControl statements
Control statementsKanwalpreet Kaur
15.9K visualizações28 slides
Chapter 5 por
Chapter 5Chapter 5
Chapter 5Hajar Len
4.2K visualizações38 slides

Mais procurados(20)

Context free grammar por Mohammad Ilyas Malik
Context free grammar Context free grammar
Context free grammar
Mohammad Ilyas Malik12.3K visualizações
INTRODUCTION TO C PROGRAMMING por Abhishek Dwivedi
INTRODUCTION TO C PROGRAMMINGINTRODUCTION TO C PROGRAMMING
INTRODUCTION TO C PROGRAMMING
Abhishek Dwivedi208.4K visualizações
Algorithm Design & Implementation por Gaditek
Algorithm Design & ImplementationAlgorithm Design & Implementation
Algorithm Design & Implementation
Gaditek3.9K visualizações
Sorting por Ashim Lamichhane
SortingSorting
Sorting
Ashim Lamichhane43.7K visualizações
Control statements por Kanwalpreet Kaur
Control statementsControl statements
Control statements
Kanwalpreet Kaur15.9K visualizações
Chapter 5 por Hajar Len
Chapter 5Chapter 5
Chapter 5
Hajar Len4.2K visualizações
Presentation on Logical Operators por Sanjeev Budha
Presentation on Logical OperatorsPresentation on Logical Operators
Presentation on Logical Operators
Sanjeev Budha5.3K visualizações
Clipping por AMIT VIRAMGAMI
ClippingClipping
Clipping
AMIT VIRAMGAMI6.7K visualizações
Polygon filling algorithm por Aparna Joshi
Polygon filling algorithmPolygon filling algorithm
Polygon filling algorithm
Aparna Joshi8.8K visualizações
Conditional statement in c por Muthuganesh S
Conditional statement in cConditional statement in c
Conditional statement in c
Muthuganesh S280 visualizações
Algorithm and c language por kamalbeydoun
Algorithm and c languageAlgorithm and c language
Algorithm and c language
kamalbeydoun1.3K visualizações
Dijkstra algorithm por are you
Dijkstra algorithmDijkstra algorithm
Dijkstra algorithm
are you1.7K visualizações
Chess board problem(divide and conquer) por RASHIARORA8
Chess board problem(divide and conquer)Chess board problem(divide and conquer)
Chess board problem(divide and conquer)
RASHIARORA811.9K visualizações
Moore and mealy machine por Mian Munib
Moore and mealy machineMoore and mealy machine
Moore and mealy machine
Mian Munib30.8K visualizações
Hashing por Amar Jukuntla
HashingHashing
Hashing
Amar Jukuntla17.5K visualizações
Pseudocode por Harsha Madushanka
PseudocodePseudocode
Pseudocode
Harsha Madushanka8.3K visualizações
Types of algorithms por Amelita Martinez
Types of algorithmsTypes of algorithms
Types of algorithms
Amelita Martinez3K visualizações
Bottom - Up Parsing por kunj desai
Bottom - Up ParsingBottom - Up Parsing
Bottom - Up Parsing
kunj desai4.3K visualizações
Control statements por raksharao
Control statementsControl statements
Control statements
raksharao644 visualizações

Similar a Algorithm and flowchart with pseudo code

ALGORITHM AND FLOW CHART por
ALGORITHM AND FLOW CHARTALGORITHM AND FLOW CHART
ALGORITHM AND FLOW CHARTDereck Downing
48 visualizações20 slides
Algorithm Design and Complexity - Course 1&2 por
Algorithm Design and Complexity - Course 1&2Algorithm Design and Complexity - Course 1&2
Algorithm Design and Complexity - Course 1&2Traian Rebedea
29.6K visualizações53 slides
Daa chapter4 por
Daa chapter4Daa chapter4
Daa chapter4B.Kirron Reddi
30 visualizações13 slides
Algorithms notes 2 tutorials duniya por
Algorithms notes 2   tutorials duniyaAlgorithms notes 2   tutorials duniya
Algorithms notes 2 tutorials duniyaTutorialsDuniya.com
124 visualizações50 slides
Algorithms and how to write an algorithms por
Algorithms and how to write an algorithmsAlgorithms and how to write an algorithms
Algorithms and how to write an algorithmsAhmed Nobi
226 visualizações20 slides
Daa unit 1 por
Daa unit 1Daa unit 1
Daa unit 1jinalgoti
350 visualizações44 slides

Similar a Algorithm and flowchart with pseudo code(20)

ALGORITHM AND FLOW CHART por Dereck Downing
ALGORITHM AND FLOW CHARTALGORITHM AND FLOW CHART
ALGORITHM AND FLOW CHART
Dereck Downing48 visualizações
Algorithm Design and Complexity - Course 1&2 por Traian Rebedea
Algorithm Design and Complexity - Course 1&2Algorithm Design and Complexity - Course 1&2
Algorithm Design and Complexity - Course 1&2
Traian Rebedea29.6K visualizações
Daa chapter4 por B.Kirron Reddi
Daa chapter4Daa chapter4
Daa chapter4
B.Kirron Reddi30 visualizações
Algorithms notes 2 tutorials duniya por TutorialsDuniya.com
Algorithms notes 2   tutorials duniyaAlgorithms notes 2   tutorials duniya
Algorithms notes 2 tutorials duniya
TutorialsDuniya.com124 visualizações
Algorithms and how to write an algorithms por Ahmed Nobi
Algorithms and how to write an algorithmsAlgorithms and how to write an algorithms
Algorithms and how to write an algorithms
Ahmed Nobi226 visualizações
Daa unit 1 por jinalgoti
Daa unit 1Daa unit 1
Daa unit 1
jinalgoti350 visualizações
Types of Algorithms.ppt por ALIZAIB KHAN
Types of Algorithms.pptTypes of Algorithms.ppt
Types of Algorithms.ppt
ALIZAIB KHAN9 visualizações
Visual Problem Solving (Lecture) por Lawrence Wachs
Visual Problem Solving (Lecture)Visual Problem Solving (Lecture)
Visual Problem Solving (Lecture)
Lawrence Wachs250 visualizações
01 Notes Introduction Analysis of Algorithms Notes por Andres Mendez-Vazquez
01 Notes Introduction Analysis of Algorithms Notes01 Notes Introduction Analysis of Algorithms Notes
01 Notes Introduction Analysis of Algorithms Notes
Andres Mendez-Vazquez561 visualizações
Ic lecture6 architecture and algo por AttaullahRahimoon
Ic lecture6 architecture and algoIc lecture6 architecture and algo
Ic lecture6 architecture and algo
AttaullahRahimoon16 visualizações
Chapter 3 introduction to algorithms handouts (with notes) por mailund
Chapter 3 introduction to algorithms handouts (with notes)Chapter 3 introduction to algorithms handouts (with notes)
Chapter 3 introduction to algorithms handouts (with notes)
mailund118 visualizações
C LANGUAGE-FLOWCHARTS,PSEUDOCODE,ALGORITHMS APPROCHES por HarshJha34
C LANGUAGE-FLOWCHARTS,PSEUDOCODE,ALGORITHMS APPROCHESC LANGUAGE-FLOWCHARTS,PSEUDOCODE,ALGORITHMS APPROCHES
C LANGUAGE-FLOWCHARTS,PSEUDOCODE,ALGORITHMS APPROCHES
HarshJha34951 visualizações
Practical 01 (detailed) por Muhammadalizardari
Practical 01 (detailed)Practical 01 (detailed)
Practical 01 (detailed)
Muhammadalizardari451 visualizações
Architecture Algorithm Definition por Gaditek
Architecture Algorithm DefinitionArchitecture Algorithm Definition
Architecture Algorithm Definition
Gaditek1.9K visualizações
Chapter-9.01-COMPUTING-(E)-Theory por STUDY INNOVATIONS
Chapter-9.01-COMPUTING-(E)-TheoryChapter-9.01-COMPUTING-(E)-Theory
Chapter-9.01-COMPUTING-(E)-Theory
STUDY INNOVATIONS5 visualizações
Introduction to Programming por Prof Ansari
Introduction to ProgrammingIntroduction to Programming
Introduction to Programming
Prof Ansari95 visualizações
Algorithm & data structure lec2 por Abdul Khan
Algorithm & data structure lec2Algorithm & data structure lec2
Algorithm & data structure lec2
Abdul Khan3K visualizações
Sienna 1 intro por chidabdu
Sienna 1 introSienna 1 intro
Sienna 1 intro
chidabdu438 visualizações

Último

Digital Watermarking Of Audio Signals.pptx por
Digital Watermarking Of Audio Signals.pptxDigital Watermarking Of Audio Signals.pptx
Digital Watermarking Of Audio Signals.pptxAyushJaiswal781174
11 visualizações25 slides
MSA Website Slideshow (16).pdf por
MSA Website Slideshow (16).pdfMSA Website Slideshow (16).pdf
MSA Website Slideshow (16).pdfmsaucla
64 visualizações8 slides
CHEMICAL KINETICS.pdf por
CHEMICAL KINETICS.pdfCHEMICAL KINETICS.pdf
CHEMICAL KINETICS.pdfAguedaGutirrez
12 visualizações337 slides
What is Unit Testing por
What is Unit TestingWhat is Unit Testing
What is Unit TestingSadaaki Emura
23 visualizações25 slides
DESIGN OF SPRINGS-UNIT4.pptx por
DESIGN OF SPRINGS-UNIT4.pptxDESIGN OF SPRINGS-UNIT4.pptx
DESIGN OF SPRINGS-UNIT4.pptxgopinathcreddy
19 visualizações47 slides
Machine learning in drug supply chain management during disease outbreaks: a ... por
Machine learning in drug supply chain management during disease outbreaks: a ...Machine learning in drug supply chain management during disease outbreaks: a ...
Machine learning in drug supply chain management during disease outbreaks: a ...IJECEIAES
12 visualizações17 slides

Último(20)

Digital Watermarking Of Audio Signals.pptx por AyushJaiswal781174
Digital Watermarking Of Audio Signals.pptxDigital Watermarking Of Audio Signals.pptx
Digital Watermarking Of Audio Signals.pptx
AyushJaiswal78117411 visualizações
MSA Website Slideshow (16).pdf por msaucla
MSA Website Slideshow (16).pdfMSA Website Slideshow (16).pdf
MSA Website Slideshow (16).pdf
msaucla64 visualizações
CHEMICAL KINETICS.pdf por AguedaGutirrez
CHEMICAL KINETICS.pdfCHEMICAL KINETICS.pdf
CHEMICAL KINETICS.pdf
AguedaGutirrez12 visualizações
What is Unit Testing por Sadaaki Emura
What is Unit TestingWhat is Unit Testing
What is Unit Testing
Sadaaki Emura23 visualizações
DESIGN OF SPRINGS-UNIT4.pptx por gopinathcreddy
DESIGN OF SPRINGS-UNIT4.pptxDESIGN OF SPRINGS-UNIT4.pptx
DESIGN OF SPRINGS-UNIT4.pptx
gopinathcreddy19 visualizações
Machine learning in drug supply chain management during disease outbreaks: a ... por IJECEIAES
Machine learning in drug supply chain management during disease outbreaks: a ...Machine learning in drug supply chain management during disease outbreaks: a ...
Machine learning in drug supply chain management during disease outbreaks: a ...
IJECEIAES12 visualizações
What is Whirling Hygrometer.pdf por IIT KHARAGPUR
What is Whirling Hygrometer.pdfWhat is Whirling Hygrometer.pdf
What is Whirling Hygrometer.pdf
IIT KHARAGPUR 11 visualizações
Literature review and Case study on Commercial Complex in Nepal, Durbar mall,... por AakashShakya12
Literature review and Case study on Commercial Complex in Nepal, Durbar mall,...Literature review and Case study on Commercial Complex in Nepal, Durbar mall,...
Literature review and Case study on Commercial Complex in Nepal, Durbar mall,...
AakashShakya1266 visualizações
Searching in Data Structure por raghavbirla63
Searching in Data StructureSearching in Data Structure
Searching in Data Structure
raghavbirla635 visualizações
NEW SUPPLIERS SUPPLIES (copie).pdf por georgesradjou
NEW SUPPLIERS SUPPLIES (copie).pdfNEW SUPPLIERS SUPPLIES (copie).pdf
NEW SUPPLIERS SUPPLIES (copie).pdf
georgesradjou15 visualizações
DevOps to DevSecOps: Enhancing Software Security Throughout The Development L... por Anowar Hossain
DevOps to DevSecOps: Enhancing Software Security Throughout The Development L...DevOps to DevSecOps: Enhancing Software Security Throughout The Development L...
DevOps to DevSecOps: Enhancing Software Security Throughout The Development L...
Anowar Hossain12 visualizações
START Newsletter 3 por Start Project
START Newsletter 3START Newsletter 3
START Newsletter 3
Start Project5 visualizações
Investigation of Physicochemical Changes of Soft Clay around Deep Geopolymer ... por AltinKaradagli
Investigation of Physicochemical Changes of Soft Clay around Deep Geopolymer ...Investigation of Physicochemical Changes of Soft Clay around Deep Geopolymer ...
Investigation of Physicochemical Changes of Soft Clay around Deep Geopolymer ...
AltinKaradagli9 visualizações
Generative AI Models & Their Applications por SN
Generative AI Models & Their ApplicationsGenerative AI Models & Their Applications
Generative AI Models & Their Applications
SN6 visualizações
Design of machine elements-UNIT 3.pptx por gopinathcreddy
Design of machine elements-UNIT 3.pptxDesign of machine elements-UNIT 3.pptx
Design of machine elements-UNIT 3.pptx
gopinathcreddy32 visualizações
Taking out the Trash (And the Recyclables]: RFID and the Handling of Municipa... por ijseajournal
Taking out the Trash (And the Recyclables]: RFID and the Handling of Municipa...Taking out the Trash (And the Recyclables]: RFID and the Handling of Municipa...
Taking out the Trash (And the Recyclables]: RFID and the Handling of Municipa...
ijseajournal5 visualizações
zincalume water storage tank design.pdf por 3D LABS
zincalume water storage tank design.pdfzincalume water storage tank design.pdf
zincalume water storage tank design.pdf
3D LABS5 visualizações

Algorithm and flowchart with pseudo code

  • 2. Solving Problems (1) When faced with a problem: 1. We first clearly define the problem 2. Think of possible solutions 3. Select the one that we think is the best under the prevailing circumstances 4. And then apply that solution 5. If the solution works as desired, fine; else we go back to step 2
  • 3. Solving Problems (2)  It is quite common to first solve a problem for a particular case  Then for another  And, possibly another  And watch for patterns and trends that emerge  And to use the knowledge form those patterns and trends in coming up with a general solution
  • 4. Solving Problems (3)  It helps if we have experienced that problem or similar ones before  Generally, there are many ways of solving a given problem; the best problem- solvers come-up with the most appropriate solution more often than not!  The process that can be used to solve a problem is termed as the “algorithm”
  • 5. Examples  Addition  Conversion from decimal to binary  The process of boiling an egg  The process of mailing a letter  Sorting  Searching
  • 6. Let us write down the algorithm for a problem that is familiar to us Converting a decimal number into binary
  • 7. Convert 75 to Binary 752 37 12 18 12 9 02 4 12 2 02 1 02 0 1 1001011 remainder
  • 8. Algorithm for Decimal-to-Binary Conversion 1. Write the decimal number 2. Divide by 2; write quotient and remainder 3. Repeat step 2 on the quotient; keep on repeating until the quotient becomes zero 4. Write all remainder digits in the reverse order (last remainder first) to form the final result
  • 9. Points to Note: 1. The process consists of repeated application of simple steps 2. All steps are unambiguous (clearly defined) 3. We are capable of doing all those steps 4. Only a limited no. of steps needs to be taken 5. Once all those steps are taken according to the prescribed sequence, the required result will be found 6. Moreover, the process will stop at that point
  • 10. Algorithm (Better Definition) 1st Definition: Sequence of steps that can be taken to solve a problem Better Definition: A precise sequence of a limited number of unambiguous, executable steps that terminates in the form of a solution
  • 11. Three Requirements: 1. Sequence is: a. Precise b. Consists of a limited number of steps 2. Each step is: a. Unambiguous b. Executable 3. The sequence of steps terminates in the form of a solution
  • 12. Why Algorithms are Useful?  Once we find an algorithm for solving a problem, we do not need to re- discover it the next time we are faced with that problem  Once an algorithm is known, the task of solving the problem reduces to following (almost blindly and without thinking) the instructions precisely  All the knowledge required for solving the problem is present in the algorithm
  • 13. Analysis of Algorithms  Analysis in the context of algorithms is concerned with predicting the resources that are requires:  Computational time  Memory  Bandwidth  Logic functions  However, Time – generally measured in terms of the number of steps required to execute an algorithm - is the resource of most interest  By analyzing several candidate algorithms, the most efficient one(s) can be identified
  • 14. Selecting Among Algorithms When choosing among competing, successful solutions to a problem, choose the one which is the least complex This principle is called the “Ockham’s Razor,” after William of Ockham - famous 13-th century English philosopher
  • 15. Syntax & Semantics An algorithm is “correct” if its:  Semantics are correct  Syntax is correct Semantics: The concept embedded in an algorithm (the soul!) Syntax: The actual representation of an algorithm (the body!) WARNINGS: 1. An algorithm can be syntactically correct, yet semantically incorrect – very dangerous situation! 2. Syntactic correctness is easier to check as compared with semantic
  • 16. Now onto Algorithm Representation  We have said enough about algorithms – their definition, their types, etc.  But, how do we actually represent them?  Generally, SW developers represent them in one of three forms:  Pseudo code  Flowcharts  Actual code
  • 17. Pseudo Code  Language that is typically used for writing algorithms  Similar to a programming language, but not as rigid  The method of expression most suitable for a given situation is used:  At times, plain English  At others, a programming language like syntax
  • 18. Flowchart  A graphical representation of a process (e.g. an algorithm), in which graphic objects are used to indicate the steps & decisions that are taken as the process moves along from start to finish  Individual steps are represented by boxes and other shapes on the flowchart, with arrows between those shapes indicating the order in which the steps are taken
  • 19. Start or stop Process Input or output Connector Decision Flow line Off-page connector Flowchart Elements
  • 20. Algorithm Building Blocks All problems can be solved by employing any one of the following building blocks or their combinations 1. Sequences 2. Conditionals 3. Loops
  • 21. Sequences A sequence of instructions that are executed in the precise order they are written in: statement block 1 statement block 2 statement block 3 statement block 1 statement block 2 statement block 3
  • 22. Conditionals Select between alternate courses of action depending upon the evaluation of a condition If ( condition = true ) statement block 1 Else statement block 2 End if statement block 1 condition True False statement block 2
  • 23. Loops Loop through a set of statements as long as a condition is true Loop while ( condition = true ) statement block End Loop condition True False statement block
  • 24. Problem Statement Convert a decimal number into binary
  • 25. Convert 75 to Binary 752 37 12 18 12 9 02 4 12 2 02 1 02 0 1 1001011 remainder
  • 26. Solution in Pseudo Code 1. Let the decimal number be an integer x, x > 0 2. Let the binary equivalent be an empty string y 3. Repeat while x > 0 { Determine the quotient & remainder of x ÷ 2 y = CONCATENATE( remainder, y ) x = quotient } 4. Print y 5. Stop
  • 27. Start Find quotient & remainder of x ÷ 2 Get x x>0 ? Print y Stop y = CONC(remainder, x) x = quotient x is the decimal number y is the binary equivalent Flowchart of Decimal to Binary Conversion Yes No
  • 28. Another Example: Sorting Sort the following objects w.r.t. their heights
  • 30. Strategy There are many strategies for solving this problem. We demonstrate a simple one: Repeat the following steps while the list is un- sorted: Start with the first object in the list Swap it with the one next to it if they are in the wrong order Repeat the same with the next to the first object Keep on repeating until you reach the last object in the list
  • 31. Back to the Objects to be Sorted
  • 39. Q: Is the list sorted? A: No
  • 45. Q: Is the list sorted? A: No
  • 49. Q: Is the list sorted? A: Yes
  • 52.  A number is even if it can be divided by 2 without remainder. Such numbers are 2, 4, 6, 8.. and so on. The numbers that leave a remainder are called odd. They are 1, 3, 5, 7.. and so on.  In programming we find the remainder of a division with the operator %. Also we use the double equals “==” to compare values for equality.
  • 54.  Summing two numbers was easy – the calculation was one block from the flow chart. But how about 50? Do you have to write 50 blocks to solve this task? Happily – no.  You can automate this process by repeatedly incrementing the value of a variable and checking it every time if it exceeds the last value – 50. Then sum that number every step and... there you go! This construction is called loop.
  • 56. Find the biggest of 100 prices and reduce it by 10%