Unit 1
Unit 1
Primitive Data Structure: The Data structures that are directly processed by machine using its instructions
are known as primitive data structure.
Integer:
Real:
Character:
Logical:
A logical data item is a primitive data structure that can assume the values of either “true” or
“false”.
Pointer:
Insert and delete operation are time consuming, searching and sorting are fast.
(B) Stack: A stack is a linear list in which insertion and deletion operations are performed at
only one end of the list. It is LIFO (Last In First Out).
C) Queue: A queue is a linier list in which insertion is performed at one end called rear and deletion is
performed at another end of the list called front.
(i) Information
(ii) Address, which contains the address of the next node.
Prepared By: Department of Computer Engineering Page 3
Subject Name: Data Structure And Algorithm Unit No: 1 Subject Code: 4330704
(A) Tree: It consists of nodes connected by edge. Nodes represent data items. The node
represent by circle.
(B) Graph: This data structure contains relationship between pairs of elements.
Graphs are many types.
1. Directed graph
2. Undirected graph
3. Mixed Graph
4. Weighted graph
There are various types of Data Structure which are classified according to several ways, such
are
Linear Data Structure: Logical relation among the data item is linear. Ex:
Array,Stack,Queue
Non Linear Data Structure: Logical relation among the data item is not linear.
Ex: Array
Non-Homogeneous Data Structure: It contains different data type. Ex:Structure,class
1.3 Primitive and non-primitive data structures
Characteristics of Algorithm
1. Name of Algorithm: Every Algorithm is given an identifying name, written in capital letters.
2. Steps: The algorithm is made of sequence of steps.
Prepared By: Department of Computer Engineering Page 5
Subject Name: Data Structure And Algorithm Unit No: 1 Subject Code: 4330704
b) If (condition)
then
else
5. Repeat Statement
a) Repeat while(condition)
b) Repeat thru step No for variable name=1,2,…N
c) Case Statement
Select case
Case value 1:
Case value 2:
Case value N:
Default:
6. GOTO Statement: It is used for unconditional transfer of controls.
7. Exit Statement: It is used to terminate an algorithm.
Time complexity: Time complexity is defined as the amount of time taken by an algorithm to run,
as a function of the length of the input.
Complexity: Complexity of an algorithm is a function describing the efficiency of the algorithm
in terms of the amount of data.
Space Complexity: It is a function describing the amount of memory an algorithm takes in terms of
the amount of input to the algorithm.
Big oh notation (O): It is define as upper bound and upper bound on an algorithm is the most
amount of time required ( the worst case performance). Big oh notation is used to describe
asymptotic upper bound.
In computer science, big O notation is used to classify algorithms according to how their run
Big Theta Notation (Θ):It is define as tightest bound and tightest bound is the best of all the worst
case times that the algorithm can take.
Worst Case Complexity: Slowest time to complete with chosen input.
When the algorithm takes maximum no of steps on any instance of size n. Example: In linear
search, If searching element is at last position Or In ascending order sorting, if all the elements
are in descending order then time taken by the algorithm is more.
When the algorithm takes minimum no of steps on any instance of size n. Example: In linear
search, If searching element is at First position Or In ascending order sorting, if all the
elements are in ascending order then time taken by the algorithm is less.
Average case: In average case analysis, we take all possible inputs and calculate the computing
time for all of the inputs.
1.7 Array
One dimensio nal array can be declared as:
Example: - int a [20].
Representation of an array in memory locat ion is shown below:
L0 + (I - L) * s
L0 is the starting (Base) address or address of first element in the array.
I is the array index.
L is the starting index o f an array.
S is the size.
If we want to find the address of A [2] then it is calculated as: Loc (A [2]) =
Base + (I –– L) * s
= 2000 + (2-0) * 2
= 2000 + 4
= 2004
Row-major Arrays: If two dimensio nal array elements store sequent ially row by row then it is called
Row major array.
Example: A two dimensio nal array consist of two rows and three columns is storedsequentially in
The address of element A[i, j] can be obtained by evaluat ing expressio n:
Loc (A [i, j]) = Base + ((i – L1) * n + (j – L2))* s
for example the address of element
=3000 + (3+2)*2
=3000 + 10
=3010
Column-major Arrays: If two dimensio nal array elements store sequentiallycolumn by co lumn then it
is called Co lumn major array.
Example: A two dimensional array consist of two rows and three columns isstored sequentially
in row major order as:
A[0][0]
A[1][0]
A[0][1]
A[1][1]
A[0][2]
A[1][2]
The address of element A[i, j] can be obtained by evaluat ing expressio n:
Loc (A [i, j]) = Base + ((j – L2) * m + (i – L1))* s
of rows.
for example the address of element A[1,2] in a[2][3] is calculated as:A [1, 2] = = Base +
((j –– L2) * m + (i –– L1))* s
=3000 + (4+1)*2
=3000 + 10
=3010
Characteristics of Array.
Array store elements that have same data type.
Array store elements in subsequent memory location.
Array size should be ment ion in the declarat ion.
Array name represent the address of starting elements.
Two dimensio nal array elements are stored row by row in subsequentmemory locat ion.
Insert and Delete operation are slower.
Searching and sorting are faster.
We can use one name for similar objects and save them wit h same name butdifferent indexes.
2- D array are used to represent matrices.
It can be used to implement other data structure like linked list, stack,queue ,tree, graph etc.
SEQENTIAL_SEARCH (L, N, X): These funct ion searches the list L consist ofN elements for the
value o f X.
i0
Flag1
1. [Search the List]
Repeat thru step 3 for i=0, 1, N-1
2. [Comparison]
3. If (L[i]= X )
5. [Finished]
Exit
Advantage:
Disadvantage:
(1) Search Time is not unique, if value is in start it takes less time, but if value isnear to end it takes
more time.
(2) As the size of data increase, the search time also linearly increase.
BINARY_SEARCH (L, N, X)
LOW, HIGH and MIDDLE denotes the lower, upper and middle limit of the list.
[Init ialize]LOW0
HIGHN-1
Flag1
1. [Perform Search]
Repeat thru step 4 while LOW ≤ HIGH
3. MIDDLE
[[((LLO
OWW + HIGH)/2]
4. [Compare]
If X < L [MIDDLE] then
HIGH
MIDDLE – 1
LOWMIDDLE+1
Else
Flag0
Return (MIDDLE)
5. If (Flag=1)
Write “Unsuccessful Search”
6. [Finished]
Exit.