0% found this document useful (0 votes)
8 views

Algorithms

algorithm notes python

Uploaded by

bruintjiesivhan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Algorithms

algorithm notes python

Uploaded by

bruintjiesivhan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Algorithms

An algorithm is a sequence of logical instructions for carrying out a task. In computing, algorithms
are needed to design computer programs.

Designing an algorithm
An algorithm is a plan, a logical step-by-step process for solving a problem. Algorithms are
normally written as a flowchart or in pseudocode.
A flowchart is a diagram that shows a process, made up of boxes representing steps, decisions,
inputs and outputs.
Pseudocode is a method of writing up a set of instructions for a computer program using plain
English. This is a good way of planning a program before coding.

When designing an algorithm there are two main areas to look at:

 the big picture - What is the final goal?


 the individual stages – What hurdles need to be overcome on the way to the goal?

Understanding the problem

Before an algorithm can be designed, it is important to check that the problem is completely
understood. There are a number of basic things to know in order to really understand the problem:

 What are the inputs into the problem?


 What will be the outputs of the problem?
 In what order do instructions need to be carried out?
 What decisions need to be made in the problem?
 Are any areas of the problem repeated?
Let us take an example of a real-life situation for creating algorithm. Here is the algorithm for going
to the market to purchase a pen:

Before designing an algorithm it is important to first understand what the problem is. Algorithms
can be designed using pseudocode or a flowchart, and the standard notations of each should be
known.

Pseudocode

Most programs are developed using programming languages. These languages have
specific syntax that must be used so that the program will run properly. Pseudocode is not a
programming language. It is a simple way of describing a set of instructions that does not have to
use specific syntax.

Common pseudocode notation

There is no strict set of standard notations for pseudocode, but some of the most widely
recognised are:

 INPUT – indicates a user will be inputting something


 OUTPUT – indicates that an output will appear on the screen
 WHILE – a loop (iteration that has a condition at the beginning)
 FOR – a counting loop (iteration)
 REPEAT – UNTIL – a loop (iteration) that has a condition at the end
 IF – THEN – ELSE – a decision (selection) in which a choice is made
 any instructions that occur inside a selection or iteration are usually indented

Using pseudocode

Pseudocode can be used to plan out programs. Planning a program that asks people what the
best subject they take is, would look like this in pseudocode:

REPEAT
OUTPUT 'What is the best subject you take?'
INPUT user inputs the best subject they take
STORE the user's input in the answer variable
IF answer = 'Computer Science' THEN
OUTPUT 'Of course it is!'
ELSE OUTPUT 'Try again!'
UNTIL answer = 'Computer Science'

Flowcharts

A flowchart is a diagram that represents a set of instructions. Flowcharts normally use standard
symbols to represent the different types of instructions. These symbols are used to construct the
flowchart and show the step-by-step solution to the problem.
Common flowchart symbols
Using flowcharts

General example of a flowchart structure to illustrate the use of the various symbols:

Flowchart for going to the market to purchase a pen:


We will discuss the diamond symbol in more detail later.

Another example of a flowchart is that of asking people what the best subject they take
is, would look like this as a flowchart:

Examples of simple Flowcharts and Pseudocodes:


Here are a few examples of how to write pseudocodes and flowcharts:
Decision Control Structures

Up until now, there is a symbol in drawing up algorithms that we have not used: the diamond
shape:

The diamond shape is used to make decisions, and has one input and 2 outputs. The outputs are
binary, ie either TRUE or FALSE or YES or NO. The complete structure of the decision block is:

FALSE Decision TRUE

Decision control structures are used when the next step to be executed depends upon a criterion.
This criterion is usually one or more Boolean expressions that must be evaluated. A Boolean
expression always evaluates to “true” or “false”. One set of statements is executed if the criteria
is “true” and another set executed if the criteria evaluates to “false”.

The decision structure is also used in repetition control or loop. Repetition control structure is
used when a set of statements in to be repeated many times. The number of repetitions might be
known before it starts or may depend on the value of an expression.

A simple everyday example of the use of a decision structure when we want to go to the shop to
buy a pen is shown in the figure below:
Notice that the decision structure has two outputs, which can either be TRUE or FALSE. If TRUE
then one set of actions is carried. If FALSE, then a different set of actions is carried out.

Example: Write an algorithm to determine if a number is even or not.

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.

remainder = n  2
n 2
In a repetition loop, we carry out the same set of activities until a condition is met.

Example: In the previous notes we found the sum of two numbers. How do we find the sum of the
first 50 numbers?
Summing two numbers was easy, but how about 50? Do you have to write 50 blocks to solve this
task? No,

You might also like