Sei sulla pagina 1di 3

CDS1101 - DATA STRUCTURES AND ALGORITHMS

Assistant Prof. Roy B. Callope


Lecture 1 – Introduction to Data Structures and Algorithms

What is an algorithm?
 In mathematics and computer science, an algorithm is a step-by-step procedure for
calculations. Algorithms are used for calculation, data processing and automated reasoning.
 Is a finite set of instructions that specify a sequence of operations to be carried out in order
to solve a specific problem or class of problems.

Criteria for algorithms


1. Input - There are zero or more quantities which are externally supplied.
2. Output - At least one quantity is produced.
3. Definiteness - Each instruction must be clear and unambiguous.
4. Finiteness - If we trace out the instructions of an algorithm, then for all cases, the algorithm
will terminate after a finite number of steps.
5. Effectiveness – Every instruction must be sufficiently basic that it can, in principle, be
carried out by a person using only pencil and paper.

Algorithm 1: Find the Largest Value Among N Integers


1. Input the number of integers to compare; call it N.
2. Input the first integer; call it NUM1.
3. Input the second integer; call it LARGE.
4. Set up a counter representing the number of integers that have been read; call it COUNT.
Set COUNT to 2.
5. Compare NUM1 with LARGE; if NUM1 is greater than LARGE, set LARGE to NUM1.
6. If COUNT equals N, display the value of LARGE and exit. Otherwise, increment COUNT by 1
and input the next integer to be compared and store it in NUM1.
7. Return to Step 5.

Algorithm 2: Trace the output


1. Set N to 5.
2. Set Y to 0.
3. Input the first integer; call it X.
4. Set up a counter representing the number of integers that have been read; call it COUNT. Set
COUNT to 1.
5. Set Y to (Y + X).
6. If COUNT equals N, display the value of Y and exit. Otherwise, increment COUNT by 1 and
input the next integer to be compared and store it in X.
7. Return to Step 5.
Input numbers: 16, 9, 8, 5, 4

Representation of Algorithms
1. Pseudocode – is an English-like representation of the code required for an algorithm. It is
part English, part structured code. The English part provides a related syntax that is easy to
read. The code part consists of an extended version of the basic algorithmic constructs –
sequence, selection and iteration.

Page 1 of 3
Cup_of_Coffee()
{
Fill kettle with water
Turn stove on
Put kettle on stove
While (water is not boiling)
{
*/Do nothing. Repeat loop until water has boiled. */
}
Turn stove off
Fill mug with boiled water
Put one (1) teaspoon of coffee in mug
If (coffee with sugar) then
{
Put two (2) teaspoons of sugar in mug
}
If (coffee with milk) then
{
Pour one (1) tablespoon of milk in mug
}
Stir contents of mug
}
2. Flowchart – graphical representation of the algorithm

Page 2 of 3
Data Structure - In computer science, a data structure is a data organization, management,
and storage format that enables efficient access and modification. More precisely, a data
structure is a collection of data values, the relationships among them, and the functions or
operations that can be applied to the data.
1. Arrays - is a data structure, which can store a fixed-size collection of elements of the
same data type
2. Linked Lists - A linked list is a linear data structure where each element is a
separate object. Each element (we will call it a node) of a list is comprising of two
items - the data and a reference to the next node. The last node has a reference to
null. The entry point into a linked list is called the head of the list.
3. Queues (FIFO) - Queue is an abstract data structure, somewhat similar to Stacks.
Unlike stacks, a queue is open at both its ends. One end is always used to insert data
(enqueue) and the other is used to remove data (dequeue). Queue follows First-In-
First-Out methodology, i.e., the data item stored first will be accessed first.
4. Stacks (LIFO) - A stack is a basic data structure that can be logically thought of as a
linear structure represented by a real physical stack or pile, a structure where
insertion and deletion of items takes place at one end called top of the stack. The
basic concept can be illustrated by thinking of your data set as a stack of plates or
books where you can only take the top item off the stack in order to remove things
from it.
5. Trees - A tree is a collection of nodes connected by directed (or undirected) edges. A
tree is a nonlinear data structure, compared to arrays, linked lists, stacks and queues
which are linear data structures. A tree can be empty with no nodes or a tree is a
structure consisting of one node called the root and zero or one or more subtrees.

Page 3 of 3

Potrebbero piacerti anche