You are on page 1of 13

Data Structures and

Algorithms
BS (SE)-3rd Section-E

Student Name______________________________ F/Name___


______________________________
Batch__________________ Section ___________________ Roll No
___________________________

Instructor:
Mr. Wahab Khan
MS (Software Engineering), CASE Islamabad,
Master in Computer Science (Gold Medalist),
M.Ed, B.Ed

SARHAD UNIVERSITY OF SCIENCE AND


INFORMATION TECHNOLOGY

Data Structures and Algorithms

Chapter One

Data Structure
In data structure, we study that how many ways are there to organize
data. It means the different possible ways of representing the data

items in Computer memory.


Logical or mathematical model of a particular organization of data is
called a data structure .The choice of a particular data model
depends on two considerations.
o it must be rich enough in structure to represent the actual
relationships of the data in the real world.
o the structure should be simple enough that one can effectively

process the data when necessary


Data structure Is a particular way of storing and organizing data in a

computer so that it can be used efficiently


Different kinds of data structures are suited to different kinds of
applications and some are highly specialized to specific tasks

Data Structure Operations


Following are the major operations:
Traverse Accessing each record exactly once so that certain items
in the
record may be processed. (This accessing and processing is

Search

sometimes called "visiting" the record.)


Finding the location of the record with a given key value, or

finding
the locations of all records that satisfy one or more

conditions
Insert
Adding a new record to the structure
Delete
Removing a Record from the data structure
Sort
Arranging the records in some logical order.

(e.g.

alphabetically
according to some NAME key, or in numerical order
according to some NUMBER key, such as account number)

Wahab Khan- SARHAD University, Peshawar (Deptt Comp Science)


Page 2

Data Structures and Algorithms

Merge

Combining the records in two different sorted files into a

single
sorted file.Other operations, e.g., copying and
concatenation, are also used.

Algorithm
An algorithm is a finite set of instructions, which accomplishes a
particular task.
OR
An algorithm is a finite step-by-step list of well-defined instructions for
solving a particular problem.
Algorithms must satisfy the following criteria.

Input: The algorithms must have input values from the specified set.

Output: Each algorithm is written to produce the output.

Definiteness: Each instruction must be clear and unambiguous.

Finiteness: It means that algorithm must terminate in the finite number


of steps.

Effectiveness: Every algorithm must be effective which performs some


specific Action on the given data (i.e. means to produce a meaningful
result).

ALGORITHM NOTATION
A complete algorithmic notation is given below.

Name of algorithm: Every algorithm is given an identifying name,


written in capital letters.

Wahab Khan- SARHAD University, Peshawar (Deptt Comp Science)


Page 3

Data Structures and Algorithms

Introductory Comments: The algorithm name is followed by a brief

description of the tasks the Algorithms perform.


Steps: The algorithm is made of a sequence of numbered steps.
Comments: An algorithm step may terminate with a comment enclosed
in bracket, which is used to help the reader better understand that
step.

Wahab Khan- SARHAD University, Peshawar (Deptt Comp Science)


Page 4

Data Structures and Algorithms

Algorithm: AVERAGE
This algorithm reads four marks denoted by M 1, M2, M3, M4 and
compute. The
average grade, placing it in average. All variables are assumed to be real.
(l) [Input individual marks]
Read (Ml, M2, M3, M4)
2) [Compute average grade]
Average = (M 1+M2+M3+M4)/4
3) [Output Result]
Print Average
4) [Finish]
Exit

Wahab Khan- SARHAD University, Peshawar (Deptt Comp Science)


Page 5

Data Structures and Algorithms

Array
An array is a list of a finite number of homogeneous data elements
such that
i)

The elements of the array are referenced respectively by an

ii)

index set consisting of "n" consecutive numbers.


The elements of an array are stored respectively in successive
memory locations.

An array is a powerful data structure for storing and manipulating large


blocks of data. It is a group of related memory locations. These
locations are referred to by one name.
There are different types of arrays but the most commonly used are:
I)
II)

One Dimensional arrays(Linear Array)


Two Dimensional arrays
One Dimensional arrays
The simplest type of data structure is a Linear or one-dimensional
array. It is like a list of data items. For one dimensional array, a single
subscript or index is used to access its elements.

Two-Dimensional Arrays
The two dimensional array consists of rows and columns. It is also. Called
table or matrix. The elements of a two-dimensional array are referenced by
two subscripts or index values. A matrix with the same number of rows and
columns is called square matrix.
The elements of the array are denoted as:
A [ i, j]
i represents the row number
j represents the column number
a two-dimensional array abc having 2 rows and 3 columns is shown below
abc
(0, 0)
(0, 1)
(0, 2)
Wahab Khan- SARHAD University, Peshawar (Deptt Comp Science)
Page 6

Data Structures and Algorithms

(1, 0)

(1, 1)

(1, 2)

The total number of elements of a two-dimensional array having m rows


and n columns is m x n. For example, an array having 2 rows and 4
columns has 2 x 4 = 8 elements.

Chapter Three
STACKS

A stack is a special kind of linear list in which only two operations,


insertion and deletion,
can be performed. These operations may occur only at its top end. The item
in it are stored and retrieved in last In First Out (LIFO) manner.
Examples: a stack of dishes, a stack of coins and a stack of folded towels.

Or:
A stack is a list of elements in which an element may be inserted or
deleted only at one end, called the top of the stack. This means, in
particular, that elements are removed from a stack in the reverse order of
that in which they were inserted into the stack.
(a) Push is the term used to insert an element into a stack.
(b)Pop is the term used to delete an element from a stack.
These terms are used only with stacks, not with other data structures.

Evaluation of Expressions
An arithmetic expression is made up of operands, arithmetic operators
and parentheses. The operands may be numeric variables or numeric
constants. Following are some examples of arithmetic expressions:
A+B, X*Y, A*(B-C)/2, (A+B)^2
The arithmetic operators +, -, * and / are the same that are used in
ordinary algebra. The operator ^ is used as to compute the exponential. This
operator is not available in C++. Instead pow function is used to calculate
the exponential. The expression is always evaluated from left to right. The
order in which the expression is evaluated is:
o
o
o
o

If the expression has parenthesis, then they are evaluated first.


Exponential (^) is given highest priority.
Multiplication (*) and division (/) have the next highest priority.
Addition (+) and subtraction (-) have the lowest priority.

Example: Describe the steps to evaluate the following expression.


Wahab Khan- SARHAD University, Peshawar (Deptt Comp Science)
Page 7

Data Structures and Algorithms

2^3 + 6*2 - 9/3


Step-1:

The expression is evaluated from left to right. Exponential is


evaluated first. After evaluating the exponential, the expression
becomes:
= 8 + 6*2 - 9/3

Step-2:

Multiplication and division are performed nest and the expression


becomes:
=

Step-3:

12

- 3

Addition and subtraction are performed last, from left to right,


and the final result is;
= 17

Wahab Khan- SARHAD University, Peshawar (Deptt Comp Science)


Page 8

Data Structures and Algorithms

Polish Notation
In most arithmetic expressions, the arithmetic operator is placed
between two operands, e.g. x+y, x/y. The type of notation is called infix
notation. In polish notation, however, the arithmetic operator is placed
before its two operands. Following are some examples of polish notation.
Infix notation

Polish notation

i)

X+Y

+XY

ii)

X/Y

/XY

iii)

(X+Y) *Z

*+XYZ

iv)

X+(Y*Z)

+X*YZ

Since the operators are used before the operands in polish notation, it
is also called prefix notation. The polish notation is named in honour of
polish mathematics Jan Lukasiewiez. Parentheses are not used in polish
notation.

Reverse Polish Notation or Postfix Notation


In this notation the arithmetic operator is placed after the operands.
This notation is also known as suffix notation. The following table
shows expressions in infix notation and equivalent prefix and postfix
notations.

A+B

Prefix
(Polish
notation)
+AB

Postfix
(Reverse Polish
notation)
AB*

A*B

*AB

AB*

A/B

/AB

AB/

A-B

-AB

Infix

AB-

The computer evaluate an expression given in infix notation by


converting it into postfix notation. The stack is used to perform this
operation.
The following steps are taken to evaluate a postfix expression:

Wahab Khan- SARHAD University, Peshawar (Deptt Comp Science)


Page 9

Data Structures and Algorithms

1. The expression is scanned from left to right until the end of


the expression.
2. When an operands is encountered, it is pushed into stack.
3. When an operator is encountered, then:
The top two operands of stack are removed.
The arithmetic operation is performed.
The computed result is pushed back to the stack.
4. When end of the expression is reached, the top value from
the stack is picked. It is the computed value of the
expression.
Example:
Consider the following arithmetic expression P written in postfix
notation:
P:
5, 6, 2, +, *, 12, 4, /,
Symbol
STACK
Scanned
5
(1)5
5, 6
(2)6
5, 6, 2
(3)2
5, 8
(4)+
(5)*
40
(6)12
40, 12
(7)4
40, 12, 4
(8)/
40, 3
(9)
37
Example: Evaluate expression 12, 6, 1, 3, -, 4, 5, *, +,
Symbol
Scanned
(1)12
(2)6
(3)/
(4)3
(5)4
(6)5
(7)*
(8)+
(9)

STACK
12
12, 6
2
-1
-1, 4
-1, 4, 5
-1, 20
19

Wahab Khan- SARHAD University, Peshawar (Deptt Comp Science)


Page 10

Data Structures and Algorithms

Chapter Four

QUEUES
A queue is a linear list of elements in which deletions can take

place only at once end, called the front, and insertions can take place
only at the other end, called the rear, the terms front and rearare
used in describing a linear list only when it is implemented as a queue.
Queues are also called first-in first-out (FIFO) lists, since the first
element in a queue will be the first element out of the queue. In other
words, the order in which elements enter a queue is the order in which
they leave. This contrasts with stacks, which are last-in first-out (LIFO)
lists.
For example:

The people waiting in line at a bank form a queue,

where the first person in line is the first person to be waited on; and so
on. An important example of a queue in computer science occurs in a
timesharing system, in which programs with the same priority form a
queue while waiting to be executed.
SEARCHING & SORTING
Searching &sorting are two most important operations that are frequently
performed on data structures. These are fundamental operations in computer
science. These are mostly performed on data structures like arrays, linked lists.
SEARCHING
Computer systems are often used to store large amounts of data from which
individual records are retrieved according to some search criterion. The process of
finding a specific data item or record from a list is called searching.
The efficient storage of data to facilitate fast searching is an important task. All
other operations like inserting, deletion, etc. are dependent on this operation. For
example, to delete a data item from a list, its position is first located in the list and
then the deletion operation is performed.
The search is successful if the specified data item or record is found during
searching process. Search operation terminates when it is successful. If the
specified data is not found then the search is unsuccessful. Different techniques are
used to carry out search operations. The commonly used searching methods are:
Wahab Khan- SARHAD University, Peshawar (Deptt Comp Science)
Page 11

Data Structures and Algorithms

Sequential Search
Binary Search

Sequential Search
The sequential search is a simple and straightforward technique to search a
specified item in an unordered list. The specified value is searched in the list
sequentially, i.e. starting from the first element to the last element in the list in a
sequence. When the required value is found, search operation stops.
The sequential search is a slow process. It is used for small amounts of data.
This method is not recommended for large amount of data.

BINARY SEARCH
It is a more efficient technique to search a specific item from list of item.
It is mostly used for relatively large lists or table of records that are sorted in
ascending or descending order.
A binary search begins by searching the required value from the middle of
the list. If the required value is in the middle of the list then search process
terminates at that point. If the list is sorted in ascending order and the required
value is greater than the value at the middle, the control goes to the higher value to
search the required value. Similarly, if the list is sorted in ascending order and the
required value is less than the value at the middle, the control goes to the lesser
values to search the required value. In both cases, half of the list is searched to find
the required value.

Wahab Khan- SARHAD University, Peshawar (Deptt Comp Science)


Page 12

Data Structures and Algorithms


Searching is a process of finding an element within the list of elements stored in any
order or randomly. Searching is divided into two categories linear and
binarysearch. Linear searching is the basic and simple method of searching. Less
time is taken by binary search to search an element from the sorted list of
elements. So it is t say that binary search method is more efficient than the linear
search. The only drawback between the two searches is that when where there is no
prerequisite for the linear search.

Sorting
Introduction
Sorting is a basic operation in computer science. Sorting refers to the operation of
arranging data in some given sequence i.e., increasing order or decreasing order.
Sorting is categorized as Internal Sorting and External Sorting. By internal
sorting means we are arranging the numbers within the array obnly which is in
computer primary memory, whereas the external sorting is the sorting of numbers
from the external file by reading it from secondary memory.

Wahab Khan- SARHAD University, Peshawar (Deptt Comp Science)


Page 13

You might also like