Chapter 5
Algorithm and Software
1
Contents
5.1. Problem Solving on Computers
5.2. Algorithm.
5.3. Representation of algorithm
5.4. Programs and Programming Languages
5.5. Classification of Computer Software
5.6. Disciplines of Computer Science
2
5.1. Problem Solving
• The art of finding a solution to a perplexing question
• How to solve it?
• Understanding the problem
• Devising a plan
• Carrying out the plan
• Looking back
3
Problem Solving on Computers
• Problem Solving
• Computer Problem-Solving
• The process of solving problem with computers
4
Computer Problem-Solving
Three important phases:
• Algorithm Development: analyze, propose algorithm, test
algorithm
• Implementation: code, test
• Maintenance:use, modify the program to meet chaining
requirement or to correct errors.
5
The process of solving problem with computers
6
What’s an Algorithm?
• Shampoo Algorithm
Step 1: Wet hair
Step 2: Lather
Step 3: Rinse
Step 4: Repeat
• Is this enough information?
7
5.2. Algorithm
• What’s an Algorithm?
• Which step will be repeated? How many times do we need
to repeat it?
• Shampoo Algorithm (Revision #1):
Step 1: Wet hair
Step 2: Lather
Step 3: Rinse
Step 4: Repeat Steps 1-4
• How many times do we repeat step 1? Infinitely? Or at
most 2 times?
.-. 8
What’s an Algorithm?
• Keep a count of the number of times to repeat the steps
• Repeat the algorithm at most 2 times
• Shampoo Algorithm (Revision #2)
Step 1: Set count = 0
Step 2: Wet hair
Step 3: Lather
Step 4: Rinse
Step 5: Increment count (increase its value by 1)
Step 6: If count < 2 repeat steps 2-6
Step 7: EXIT
• Execute steps 2-6 two times
.-. 9
Algorithm definition
• Unambiguous instructions for solving a problem or
subproblem in a finite amount of time using a finite
amount of data
.-. 10
Characteristics of algorithms
• Exactness
• Effectiveness
• Guaranteed termination
• Generality
.-. 11
5.3. Algorithm representation
• Simple algorithms can be expressed using
• Flowchart
• Pseudo code
• Natural Language
• Programming Language
.-. 12
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
.-. 13
Review of Flowchart Elements
Start or stop
Process
Input or output
Decision
Flow line
.-. 14
Sequences
A sequence of instructions that are executed in the
precise order they are written in:
statement block 1 statement block 1
statement block 2
statement block 3 statement block 2
statement block 3
.-. 15
Conditionals
Select between alternate courses of action depending
upon the evaluation of a condition
If ( condition = true )
statement block 1
True False
condition
[Else
statement block 2]
End if statement statement
block 1 block 2
.-. 16
Loops
Loop through a set of statements as long as a condition is
true
Loop while ( condition = true )
statement block
End Loop
statement
condition
block True
False
.-. 17
Directions for pseudo code
• No rule for writing pseudo code
• A Pascal-like language
• Use syntax structures of Pascal together with
descriptions in natural languages.
.-. 18
Pseudo code for assignment
• Purpose:
• Set a value for a variable
• Expression:
Max := a1
Max a1
n n + 1
19
Pseudo code for Conditionals
if <condition> then <action>
endif
or
if < condition > then < action >
else < action >
endif
20
Pseudo code for Loops
• while <condition> do
<actions>
end while
• repeat
<actions>
until <condition>
• for <variable> <initial value> to <last value> do
<action>
end for
• for <variable> <initial value> downto <last value> do
<action>
end for
21
Pseudo code for Unconditional Jump
Jump to statement with label L
goto L
22
Pseudo code Functions/procedures
• Function declaration
Function <identifier>(<list of parameters>)
action with parameters
return <value>
End Function
• Call a function
[Call] <Function>(< list of arguments >)
23
Algorithm
Convert a decimal number into
binary
.-. 24
Convert 75 to Binary
2 75 remainder
2 37 1
2 18 1
2 9 0
2 4 1
2 2 0
2 1 0
0 1
25
Description in English
Input: Decimal integer x, x > 0
Output: binary string y equivalent to x
Method:
Step1: Input x
Step 2: y :=‘’
Step 3: if (x=0) go to step 9
Step 4: quotient := x div 2
Step 5: remainder:= x mod 2
Step 6: y:= CONCAT (remainder,y)
Step 7: x:= quotient
Step 8: go to step 3
Step 9: print y
Step10: Stop
26
Notation
• ‘’ empty string
• div integer division
• mod modulus (return remainder of a division)
• CONCAT concatenation of two strings, for instance,
concatenation of string ‘park’ and ‘ing’ is ‘parking’
• := is the assignment
.-. 27
Flowchart of decimal to binary conversion
Start
Get x
y:=‘’
Yes quotient := x div2 y:= CONCAT(remainder, y)
x >0? remainder:= x mod 2 x = quotient
No
Print y x is the decimal number
y is the binary equivalent
Stop
.-. 28
Solution in Pseudo Code
Input: Decimal integer x, x > 0
Output: binary string y equivalent to x
Method:
input x
y := ‘’
repeat
quotient := x div2
remainder:= x mod 2
y := CONCAT( remainder, y )
x := quotient
until x= 0
print y
stop
.-.
29
Question
Is this the only possible algorithm for converting a decimal
number into a binary representation?
If not, then is this the best?
In terms of speed?
In terms of memory requirement?
In terms of ease of implementation?
You must ask these questions after writing any algorithm!
.-. 30
Tips on Writing Good Pseudo Code
• Use indention for improved clarity
• Do not put “code” in pseudo code – make your pseudo
code language independent
• Don’t write pseudo code for yourself – write it in an
unambiguous fashion so that anyone with a reasonable
knowledge can understand and implement it
• Be consistent
• Prefer formulas over natural language descriptions
.-. 31
Another Example: Sorting
Sort the following objects w.r.t. their heights
.-. 32
Expected Result
.-. 33
Selection sort
• The algorithm maintains two subarrays in a given array.
1) The subarray which is already sorted.
2) Remaining subarray which is unsorted.
• In every iteration of selection sort, the minimum element
(considering ascending order) from the unsorted
subarray is picked and moved to the sorted subarray.
34
Sorting (ascending order) Selection sort algorithm
List Step A Step B Step C Step D
3 3
2
1 1 1 1 1
5 5 3
2
5 2 2 2
2 2
3 5
3 5
3 3 3
6 6 6 6 6
5 5
1 1
2 3
2 3
5 5
6 6
35
Example of selection sort
• Assume array A contains 5 elements: 3 5 2 6 1
• Step A: Find the minimum element in A[0]...A[4] and place it
at the beginning of the list
15362
• Step B: Find the minimum element in A[1]...A[4] and place it
at the beginning of unsorted list A[1] ..A[4]
12563
• Step C: Find the minimum element in arr[2...4] and place it at
the beginning of unsorted list A[2]...A[4]
12365
• Step D: Find the minimum element in arr[3...4] and place it at
beginning of A[3] and A[4]
12356
36
Flowchart for the Sorting Process
A is an array containing the
heights.
n is the total number of
objects in the list.
Indexes start
from 1, not like in C language
Is this the only possible algorithm for sorting a list?
Certainly not!
In fact this one (called the “selection sort”) is probably the
worst (reasonable) algorithm for sorting a list – it is just
too slow
You will learn a lot more about sorting in your future
courses
.-. 38
Pros and Cons of Flowcharts
• Symbols are quite intuitive and almost universally
understood
• Graphical nature makes the process of explaining an
algorithm quite straightforward
• The process of writing an algorithm is too cumbersome
• Converting graphical form into code is not straight
forward
• Another kind of flowcharts – Structured Flowcharts
.-. 39
Pros and Cons of Pseudo Code
• Closer in form to real code
• Use pseudo code as a starting point or outline for writing
real code
• Converting each line into real code is not easy
• No standard rules exist for writing pseudo code
.-. 40
5.4.Program and Programming Languages
• Program
• Programming languages
• Software Development Cycle
41
Program
• A sequence of instructions written to perform a specified
task
.-. 42
Programming Language
• An artificial language that can be used to control the
behavior of a machine, particularly a computer.
• Is defined through the use of syntactic and semantic
rules
• Facilitate the task of organizing and manipulating
information
• Express algorithms precisely.
.-. 43
Machine Language
• System of instructions directly executed by a computer's
CPU
• The lowest-level of abstraction
• Instructions are patterns of bits corresponding to
different commands of the machine.
.-. 44
Assembly
• Low-level language
• Implements a symbolic representation of the numeric
machine codes of a particular CPU architecture.
• Is specific to a certain physical or virtual computer
architecture
• Assembler : a program to translate assembly language
statements into the target computer's machine code.
.-. 45
High-level languages
• More abstract, easier to use,
• More portable across platforms.
• Examples: Pascal, C, Python, Java, Visual Basic, SQL, . . . .
• Abstract away CPU operations
• They have been implemented by translating to machine
languages
.-. 46
Software Development Cycle
Source Program
Compile
Library routines
Edit Link
Other object files
Think Load
Execute
.-. 47
5.5. Classification of Computer Software
• System software
• Application software
.-. 48
5.5. Classification of Computer Software
• Application software
• Programs that help us to solve real-world problems
• System software
• Programs that manage the computer system and interact
with the hardware
.-. 49
Application Software
• Application software is the software that has made
using computers indispensable and popular
• Common application software
• Word processors
• Electronic spreadsheet
• Graphic software
• Web browsers
• Multimedia software
• Education software
• Simulation software
.-. 50
System Software
• Operating systems
• Translation systems
• Utilities
.-. 51
Translation System
• Set of programs used to develop software
• A key component of a translation system is a translator
• Some parts of translators
• Compiler
• Converts from one language to another
• Linker
• Combines resources
• Examples
• Microsoft Visual C++, CBuilder
• Performs compilation, linking, and other activities.
.-. 52
Types of Translators
• Compiler is a program that translate source code
from a high-level programming language to a lower
level language (e.g., assembly language or machine
language)
• Interpreter is a program that translates and executes
source language statements one line at a time
.-. 53
Utilities
• Virus Scanning software
• Backup Software
• Scan Disk
• Disk Defragmenter
.-. 54
5.6.Disciplines of Computer Science
• Information Systems
• Artificial Intelligence
• Simulation
.-. 55
Information Systems
• Software that helps us manage and analyze data
• Data reflect almost every aspect of our lives
• Database : a structured set of data
• Typical tools to construct information systems :
electronic spreadsheets , database management systems
.-. 56
Artificial Intelligence (AI)
• The study of computer systems that model and apply the
intelligence of the human mind
• Aspects of AI
• Knowledge representation
• Expert Systems
• Neural networks
• Natural Language Processing
• Robotics
.-. 57
Simulation
• Developing a model of a complex system and
experimenting with the model to observe the result
• Examples:
• Queuing Systems
• Weather Forcasting
• Computer Aid Design (CAD)
• Embedded System
.-. 58