Professional Documents
Culture Documents
Algorithms
BS (SE)-3rd Section-E
Instructor:
Mr. Wahab Khan
MS (Software Engineering), CASE Islamabad,
Master in Computer Science (Gold Medalist),
M.Ed, B.Ed
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
Search
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)
Merge
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.
ALGORITHM NOTATION
A complete algorithmic notation is given below.
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
Array
An array is a list of a finite number of homogeneous data elements
such that
i)
ii)
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
(1, 0)
(1, 1)
(1, 2)
Chapter Three
STACKS
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
Step-2:
Step-3:
12
- 3
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.
A+B
Prefix
(Polish
notation)
+AB
Postfix
(Reverse Polish
notation)
AB*
A*B
*AB
AB*
A/B
/AB
AB/
A-B
-AB
Infix
AB-
STACK
12
12, 6
2
-1
-1, 4
-1, 4, 5
-1, 20
19
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:
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
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.
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.