0% found this document useful (0 votes)
51 views8 pages

LESSON 2 Algorithm Basics

Hhh

Uploaded by

graciamark009
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
51 views8 pages

LESSON 2 Algorithm Basics

Hhh

Uploaded by

graciamark009
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 8

IT 107 – Data Structure and Algorithms

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

Lesson 2 – Algorithm Basics | Page 1 of 8


the languages we are mainly interested in, namely the overlap of C and Java, with the advantage that they can
easily be inserted into runnable programs.
ACTIVATING
PRIOR KNOWLEDGE
If an 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. Kindly give examples of algorithms that is not related to programming.
Create also an algorithm that will solve the new enrollment procedure of old students in UCU.
CATEGORIES OF
ALGORITHMS
From the data structure point of view, following are some important categories of algorithms.
• Search - Algorithm to search an item in a data structure.
• Sort - Algorithm to sort items in a certain order.
• Insert - Algorithm to insert item in a data structure.
• Update - Algorithm to update an existing item in a data structure.
• Delete - Algorithm to delete an existing item from a data structure.

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.

How to Write an Algorithm?


3 Basic Control Structures of a Structured Algorithm
 sequence – steps are performed in a strictly sequential manner.
 selection - one of several alternative action is selected and executed.
 repetition – one or more steps is performed repeatedly. As applications are getting complex and data
rich, there are three common problems that applications face now-a-days.

How to create programs:


Requirements:
Information given (input) and the results to be produced (output) must be explicitly specified.
Understand the problem.
Design:
Write an algorithm that will solve the problem according to the requirement.
Analysis:
Trying to come up with another algorithm and compare it with the previous one.
Refinement and Coding:
A representation is chosen and a complete version of the program is made.
Verification:
Consist of 3 aspects:

Lesson 2 – Algorithm Basics | Page 2 of 8


• Program
• Proving
• Testing and Debugging

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.

Lesson 2 – Algorithm Basics | Page 3 of 8


3. Use parameters to pass information to and from subprograms.
4. Use constant identifiers to improve readability, flexibility and portability.
5. Strive for simplicity and clarity.
6. Comments should be used to explain key segments and/or segments whose purpose or design is not
obvious.
7. Use meaningful identifiers.
8. Put each statement on a separate line.
9. Use uppercase and lowercase letters in a way that contributes to the program’s readability.
10. Use spaces between the items in a statement to make it more readable.
How to analyze
programs:
Two Phases:
1. Priori Estimates:
Obtain a function which bounds the algorithm’s complexity time. The amount of time a single execution
will take or the number of times a statement is executed. However, it is possible to know the exact amount
of time to execute any command unless:
a. the machine we are executing is known;
b. machine instruction set
c. time required by each machine instruction;
d. translation of the compiler from source to machine language.
2. Posteriori Estimates:
Something to do with memory spaces consumed.
Example:
Use of arrays vs. linked-list
Sample
Algorithm
Scenario: You have a friend arriving at the airport and your friend needs to get from the airport to your house.
Here are four different algorithms that you might give your friend for getting to your home:
• The taxi algorithm:
1. Go to the taxi stand.
2. Get in a taxi.
3. Give the driver my address.

• The call-me algorithm:


1. When your plane arrives, call my cell phone.
2. Meet me outside baggage claim.

• The rent-a-car algorithm:


1. Take the shuttle to the rental car place.
2. Rent a car.
3. Follow the directions to get to my house.

• The bus algorithm:


1. Outside baggage claim, catch bus number 70.
2. Transfer to bus 14 on Main Street.
3. Get off on Elm street.
4. Walk two blocks north to my house.

Lesson 2 – Algorithm Basics | Page 4 of 8


Mathematical Functions
Floor of x ( ⎣ x ⎦ ) - greatest integer less than or equal to x, x is any real number
e.g.: ⎣ 3.14 ⎦ = 3
⎣ 1/2 ⎦ = 0
⎣ -1/2 ⎦ = -1

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

Modulo - given any two real numbers x and y,


 x mod y = x if y = 0
=x-y*⎣x/y⎦ if y <> 0
 take the remainder after performing the division
e.g.: 10 mod 3 = 1
24 mod 8 = 0
-5 mod 7 = -5
Identities
◦ ⎡ x ⎤ = ⎣ x ⎦ if and only if x is an integer
◦ ⎡ x ⎤ = ⎣ x ⎦ + 1 if and only if x is not an integer
◦ ⎣-x⎦=-⎡x⎤
◦ ⎣ x ⎦ + ⎣ y ⎦ <= ⎣ x + y⎦
◦ x = ⎣ x ⎦ + x mod 1
◦ z ( x mod y ) = zx mod zy

Applying Algorithms in numerical calculations:

Lesson 2 – Algorithm Basics | Page 5 of 8


LEARNING
ACTIVITY 2
Algorithms applied in numerical calculations
Objective: Apply the characteristics and the 3 basic control structures of algorithms.
Task: Write an algorithm for each of the following mathematical expressions.
1. (3 – 2 / 2) * 6 + 3 – (4 * 5) * 7
2. 8 * 3 / 5 + (2 – 3) * 1 / 5
3. (4 + (3 * 4) / 2) + (3 – (7 – 6 * 2) + 1)
Congratulations! You can now proceed to the next module that would guide you on how to apply arrays in
programming.

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

How to create programs:


Requirements:
Information given (input) and the results to be produced (output) must be explicitly specified.
Understand the problem.
Design:
Write an algorithm that will solve the problem according to the requirement.
Analysis:
Trying to come up with another algorithm and compare it with the previous one.
Refinement and Coding:
A representation is chosen and a complete version of the program is made.
Verification:
Consist of 3 aspects:

Lesson 2 – Algorithm Basics | Page 6 of 8


• Program
• Proving
• Testing and Debugging

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.
3. Use parameters to pass information to and from subprograms.
4. Use constant identifiers to improve readability, flexibility and portability.
5. Strive for simplicity and clarity.
6. Comments should be used to explain key segments and/or segments whose purpose or design is not
obvious.
7. Use meaningful identifiers.
8. Put each statement on a separate line.
9. Use uppercase and lowercase letters in a way that contributes to the program’s readability.
10. Use spaces between the items in a statement to make it more readable.

How to analyze programs:


Two Phases:
1. Priori Estimates:
Obtain a function which bounds the algorithm’s complexity time.
a. the machine we are executing is known;
b. machine instruction set
c. time required by each machine instruction;
d. translation of the compiler from source to machine language.

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:

JASMIN C. DE GUZMAN, LPT, MIT


Faculty, College of Information and
Technology Education

ELMER D. VALDEZ, MIT

Lesson 2 – Algorithm Basics | Page 7 of 8


Faculty, College of Information and
Technology Education

LORIE JELENE R. MILLON-PARAGAS, LPT, MIT


Faculty, College of Information and
Technology Education

ARNEL B. OCAY, DIT


Faculty, College of Information and
Technology Education

JESSE JAY R. FERNANDEZ, MIT


Faculty, College of Information and
Technology Education

Lesson 2 – Algorithm Basics | Page 8 of 8

You might also like