Lecture 4 - Introduction To Algorithms
Lecture 4 - Introduction To Algorithms
Algorithms
1
Introduction to Algorithms
Problems, Algorithms and Programs
Programmers deal with:
Problems,
Algorithms and
Computer programs.
2
Problems
3
Algorithms and Programs
• An algorithm gives us a “recipe” for solving the problem by
performing a series of steps, where each step is completely
understood and achievable.
4
Programs
A computer program is a concrete representation of an
algorithm in some programming language.
Naturally, there are many programs that are instances of the
same algorithms, since any modern programming language
5
ALGORITHMS …
An algorithm is the series of steps that are used to
solve a programming problem.
It may also be considered as a plan for solving a
programming problem that specifies the sequence
of steps used in the solution.
The following are mandatory features that an
algorithm must have:
(I) An algorithm must have a start and an end
point.
(II)Every step must be performable:-
(A) using a finite amount of resources
(B) in a finite amount of time.
6
Qualities of a good algorithm.
performed next.
It must be composed of a finite number of steps.
It must terminate.
7
ALGORITHMS DESIGN TOOLS
8
ALGORITHMS …
Flow Charts
A flowchart is a diagram that depicts the “flow” of a
program. They are typically used to show a
program’s logic.
Flowcharts are a modeling technique that were
introduced in the 1940/50s and popularized for
structured development in the 1970s
Can be implemented using any programming
language we want i.e. flow charts are language
independent
Flow charts are also easy for non-programmers to
understand.
9
ALGORITHMS …
10
ALGORITHMS DESIGN TOOLS
Flow Chart
Each symbol contains information on what must be
done at that particular point
The arrow shows the order in which the
instructions must be executed
Input/ Output Start/Stop
11
Stepping Through the Flowchart
Your gross
pay is 800 START
Process box
Use not more than two statements in each box.
Sum:= Sum + 1;
Count:= Count + 1;
13
ALGORITHMS …
14
Connectors
START A
END
A
ALGORITHMS …
Pseudocode
Pseudocode is a narrative representation of an
algorithm using English like statements.
Flowcharts were the first design tool to be widely
used, but unfortunately they do not reflect some of
the concepts of structured programming very well.
Pseudocode is a newer tool and has features that
make it more reflective of the structured concepts.
The drawback is that the narrative presentation is
not as easy to understand or follow.
ALGORITHMS …
Task List
Pseudocode
• Read name, hours worked, rate of
pay READ name, hoursWorked,
payRate
• Perform calculations
gross = hoursWorked * payRate
• gross = hours worked * rate of
pay WRITE name, hoursWorked,
gross
• Write name, hours worked, gross
Capitalize Initial Keyword
Pseudocode
READ name, hoursWorked, payRate
gross = hoursWorked * payRate
WRITE name, hoursWorked, gross
Indent to Show Hierarchy
Flowchart Pseudocode
Disadvantages: Disadvantages:
Hard to modify Not visual
Structured design No accepted standard,
elements not varies from company to
implemented company
Special software required
Program control structures
Programming constructs are the building
blocks of a structured program. There are
only three programming control structures.
These are:
Sequence
Selection (Decision/branching)
Iteration (Looping/Repetition)
Sequence Structure
NO YES
Decision Structure
NO YES
x < y?
Process A Process B
Decision Structure
• The flowchart segment below shows how a decision structure
is expressed in C++ as an if/else statement.
C/C++ Code
NO YES if (x < y)
x < y? a = x * 2;
Calculate a
as x * 2.
Repetition Structure
• A repetition structure represents part of the program that
repeats. This type of structure is commonly known as a loop.
Repetition Structure
• A loop tests a condition, and if the condition exists, it
performs an action. Notice the use of the diamond symbol
Then it tests the condition again. If the condition still exists,
the action is repeated. This continues until the condition no
longer exists.
Repetition Structure
• In the flowchart segment, the question “is x < y?” is asked. If
the answer is yes, then Process A is performed. The question
“is x < y?” is asked again. Process A is repeated as long as x is
less than y. When x is no longer less than y, the repetition
stops and the structure is exited.
YES
x < y? Process
A
Repetition Structure
• The flowchart segment below shows a repetition structure
expressed in C++ as a while loop.
C/C++ Code
while (x < y)
YES {
x < y? Add 1 to x x++;
}
Controlling a Repetition Structure
YES
x < y? Display x Add 1 to x
A Pre-Test Repetition Structure
YES
x < y? Display x Add 1 to x
A Post-Test Repetition Structure
YES
x < y?
A Post-Test Repetition Structure
C Code
Display
x do
{
Add 1 to x
printf(“X is %x,x)
x++;
} while (x < y);
YES
x < y?
Combining Structures
• Structures are commonly combined to create more complex
algorithms.
• The flowchart segment below combines a decision structure
with a sequence structure.
YES
x < y? Display x Add 1 to x
Combining Structures
• This flowchart segment shows two decision structures
combined.
NO YES
x>
min?
Display “x is NO YES
outside limits.”
x<
max?
Display “x is “x is within
outside limits.” limits.”
SAMPLE ALGORITHMS …
44
SAMPLE ALGORITHMS …
Algorithm For Computing The
… Factorial Of A Number Less
Than 12
Pseudocode
START
Variables: Count, Number, Factorial
Count:=0;
Number:=-10;
Factorial:=1;
DO WHILE (Number <0 or Number>12)
PROMPT ‘Enter a number between 0 and 12’
READ Number
If Number <0 or Number>12
WRITE ‘Number should be 0..12’
ENDWHILE
45