LESSON 2 Algorithm Basics
LESSON 2 Algorithm Basics
LEARNING
OBJECTIVES
After studying this lesson, you as a student should be able to:
a. Use the guiding principles of algorithms
b. Demonstrate the importance of algorithms in the creation of programs
c. Apply flowcharting and pseudo codes before the actual creation of programs
TOPIC
OUTLINE
1. Characteristics of Algorithms
2. Guidelines for a well-structured program
3. Aspects of an algorithm
4. Analysis of Algorithms
5. Creating Pseudo codes and Flowcharts
6. How to create programs based on effective algorithms
OVERVIEW
Algorithm is a step-by-step procedure, which defines a set of instructions to be executed in a certain order to get
the desired output. Algorithms are generally created independent of underlying languages, i.e. an algorithm can
be implemented in more than one programming language.
Also, an algorithm for a particular task can be defined as a finite sequence of instructions, each of which has a
clear meaning and can be performed with a finite amount of effort in a finite length of time". As such, an algorithm
must be precise enough to be understood by human beings. However, in order to be executed by a computer, we
will generally need a program that is written in a rigorous formal language; and since computers are quite
inflexible compared to the human mind, programs usually need to contain more details than algorithms. Here we
shall ignore most of those programming details and concentrate on the design of algorithms rather than programs.
The task of implementing the discussed algorithms as computer programs is important, of course, but these notes
will concentrate on the theoretical aspects and leave the practical programming aspects to be studied elsewhere.
Having said that, we will often find it useful to write down segments of actual programs in order to clarify and test
certain theoretical aspects of algorithms and their data structures. It is also worth bearing in mind the distinction
between different programming paradigms: Imperative Programming describes computation in terms of
instructions that change the program/data state, whereas Declarative Programming specifies what the program
should accomplish without describing how to do it. These notes will primarily be concerned with developing
algorithms that map easily onto the imperative programming approach.
Algorithms can obviously be described in plain English, and we will sometimes do that. However, for computer
scientists it is usually easier and clearer to use something that comes somewhere in between formatted English
and computer program code, but is not runnable because certain details are omitted. This is called pseudocode,
which comes in a variety of forms. Often these notes will present segments of pseudocode that are very similar to
Characteristics of an Algorithm
Not all procedures can be called an algorithm. An algorithm should have the following characteristics.
Unambiguous - Algorithm should be clear and unambiguous. Each of its steps, and their inputs/outputs
should be clear and must lead to only one meaning.
Input - An algorithm should have 0 or more well-defined inputs.
Output - An algorithm should have 1 or more well-defined outputs, and should
match the desired output.
Finiteness - Algorithms must terminate after a finite number of steps.
Feasibility - Should be feasible with the available resources.
Independent - An algorithm should have step-by-step directions, which should be independent of any
programming code.
Example 1:
Problem: Accept N inputs and get their sum
Requirement:
Input: Set of N integers
Output: Sum of N integers
Design:
Set a temporary variable to 0.
For each value entered, add the value to the temporary variable.
Refinement and Coding:
tempsum = 0;
for(i=1; i<=n; i++)
{
scanf(x);
tempsum=tempsum + x;
}
LEARNING
ACTIVITY 1
Creating a Program
Objective: Solve the given problem below by applying the steps on how to create a program.
Task: Accept a final grade and output whether that final grade is a passing or a failing grade.
Tools and resources: Pen or pencil and paper.
Aspects of Algorithm
Specification (What is it supposed to do?) – This should formalize the crucial details of the problem that
the algorithm is intended to solve. Sometimes that will be based on a particular representation of the
associated data, and sometimes it will be presented more abstractly. Typically, it will have to specify how
the inputs and outputs of the algorithm are related, though there is no general requirement that the
specification is complete or non- ambiguous.
Verification (Does it really do what it is supposed to do?) - For more complicated specifications and/or
algorithms, the fact that an algorithm satisfies its specification may not be obvious at all. In this case, we
need to spend some effort verifying whether the algorithm is indeed correct. In general, testing on a few
particular inputs can be enough to show that the algorithm is incorrect. However, since the number of
different potential inputs for most algorithms is infinite in theory, and huge in practice, more than just
testing on particular cases is needed to be sure that the algorithm satisfies its specification. We need
correctness proofs.
Performance Analysis (How efficiently does it do it?) - The efficiency or performance of an algorithm
relates to the resources required by it, such as how quickly it will run, or how much computer memory it
will use. This will usually depend on the problem instance size, the choice of data representation, and the
details of the algorithm. Indeed, this is what normally drives the development of new data structures and
algorithms.
Guidelines of a
well-structured program
1. Use the basic control structures when developing each program module.
2. Use local variables with in a subprogram.
Ceiling of x ( ⎡ x ⎤ ) - smallest integer greater than or equal to x, where x is any real number
e.g.: ⎡ 3.14 ⎤ = 4
⎡ 1/2 ⎤ = 1
⎡ -1/2 ⎤ = 0
SUMMARY
Let us see if you can remember the main points raised in this lesson. Below is a summary of these points:
Algorithm is a step-by-step procedure, which defines a set of instructions to be executed in a certain order to get
the desired output. Algorithms are generally created independent of underlying languages, i.e. an algorithm can
be implemented in more than one programming language.
Categories of Algorithms
From the data structure point of view, following are some important categories of algorithms.
• Search
• Sort
• Insert
• Update
• Delete
Characteristics of an Algorithm
Not all procedures can be called an algorithm. An algorithm should have the following characteristics.
Unambiguous
Input
Output
Finiteness
Feasibility
Independent
2. Posteriori Estimates:
Something to do with memory spaces consumed.
Example:
Use of arrays vs. linked-list
REFERENCES
Data Structures and Algorithms, Bullinaria (2019)
Data Structures and Algorithm offline Tutorial, Onan Mobile Software, 2019
Data Structures and Algorithms in C++ (2nd Edition), Goodrich et al. (2011)
Data Structures and Algorithm, Tutorials Point Simply Easy Learning, www.tutorialspoint.com
OpenDSA Data Structures and Algorithms Modules Collection, https://opendsa-
server.cs.vt.edu/ODSA/Books/Everything/html
Data Structures and Algorithms (Level 1/C), Dr John A. Bullinaria, https://www.cs.bham.ac.uk/~jxb/dsa.html
Prepared by: