DS Intro
DS Intro
to
Data Structures
structure.
Sort − Algorithm to sort items in a certain order.
data structure.
Delete − Algorithm to delete an existing item from a
data structure.
Data abstraction
Data Type
A data type is a collection of objects and a set of
operations that act on those objects.
For example, the data type int consists of the objects
{0, +1, -1, +2, -2, …,} and the operations +, -, *, /, and
%.
The data types of C
The basic data types: char, int, float and double
The group data types: array and struct
The pointer data type
The user-defined types
Data abstraction
Abstract Data Type
An abstract data type(ADT) is a data type
that is organized in such a way that
the specification of the objects and
the operations on the objects is separated
from
the representation of the objects and
the implementation of the operations.
Data Types & Data Structures
Structured Data types: can be broken into
component parts. E.g. an object, array, set, file, etc.
Example: a student object.
Name A H M A D
Age 20
Branch C S C
A Component part
7
Types of Data Structures
Array
Linked List
Queue Stack
Tree
Algorithm: SUM(A, B)
Step 1 - START
Step 2 - C ← A + B + 10
Step 3 - Stop
Here we have three variables A, B, and C and one constant.
Hence S(P) = 1 + 3.
Now, space depends on data types of given variables and constant types and it will
be multiplied accordingly.
Time Complexity
Time complexity of an algorithm represents the amount of time
required by the algorithm to run to completion.
It must terminate
It must be unambiguous
in a systematic way
Using pseudo-code, it is easier for a non-
Expressions
Standard mathematical symbols are used
o
Left arrow sign (←) as the assignment operator in assignment
statements
o
Equal sign (=) as the equality relation in Boolean expressions
o
For example
Sum ← 0
Sum ← Sum + 5
if condition then
true-actions
[else
false-actions]
We use indentation to indicate what actions should be included in the true-
Loops (Repetition)
Pre-condition loops
o
While loops
while condition do actions
Loops (Repetition)
Pre-condition loops
o
For loops
for variable-increment-definition do actions
For example
Loops (Repetition)
Post-condition loops
o
Do loops
do actions while condition
For example
do
print “Welcome to CSE!”
counter ← counter + 1
while counter < 5
Primitive DS Non-Primitive DS
For(i=0;i<=9;i++)
printf(“%d”,arr[i]);
Arrays
If we are reading or writing two-dimensional
array it would require two loops.
And similarly the array of a N dimension
are:
Creation of an array
Traversing an array
Arrays
Insertion of new element
Deletion of required element
Modification of an element
Merging of arrays
Lists
A lists (Linear linked list) can be defined as a
collection of variable number of data items.
Lists are the most commonly used non-
primitive data structures.
An element of list must contain at least two
fields, one for storing data or information and
other for storing address of next element.
As you know for storing address we have a
special data structure of list the address must be
pointer type.
Lists Technically each such element is referred to
as a node, therefore a list can be defined as
a collection of nodes as show bellow:
PUSH POP
[STACK]
Stack
The stack can be implemented into two ways:
Using arrays (Static implementation)
front rear
Queue
The queue can be implemented into two ways:
Using arrays (Static implementation)
A root
B C
D E F G
Graph
Graph is a mathematical non-linear data structure capable
of representing many kind of physical structures.
It has found application in Geography, Chemistry and
Engineering sciences.
Definition: A graph G(V,E) is a set of vertices V and a set
of edges E.
Graph
An edge connects a pair of vertices and many have weight
such as length, cost and another measuring instrument for
according the graph.
Vertices on the graph are shown as point or circles and
edges are drawn as arcs or line segment.
Graph
Example of graph:
6
v2 v5
v1 v3
10
v1 8 11
15
9 v2
v3 v4 v4
Undirected graph
Simple graph
Weighted graph
Connected graph
Non-connected graph
Performance analysis
For any value of n, the running time of an algorithm does not cross the
time provided by O(g(n)).
f(n) = (g(n)) (read as “f of n is theta of g of n”) iff there exist positive constants
c1, c2, and n0 such that c1g(n) f(n) c2g(n) for all n, n n0.
The above expression can be described as a
function f(n) belongs to the set Θ(g(n)) if there
exist positive constants c1 and c2 such that it can
be sandwiched between c1g(n) and c2g(n), for
sufficiently large n.