Unit 1 - Basic Concepts of Ds
Unit 1 - Basic Concepts of Ds
Introduction to Data
1
Structure
TOPICS TO BE
1. Data Structure Basic Concepts
COVERED….
2. Types of dat a structures
3. Primitive and non-primitive data structures
4. Introduction to Algorithms
5. Key features of a n algorithm
6. Analysis Terms
7. Array
8. Overview of various array operations.
9. Searching a n element into a n array –
i. Linear Search
ii. Binary Search
2
1.1 DATA STRUCTURE BASIC
CONCEPTS
Data Structure: Data structure is a way to
storing and organizing data in a computer so
t ha t it can be used efficiently.
4
1.3 PRIMITIVE AND NON-
PRIMITIVE DATA STRUCTURES
PRIMITIVE DATA STRUCTURE
The Data structures t h a t are directly processed by
machine using its instructions are known as
primitive dat a structure.
Following are the primitive data structure:
1. Integer:
Integer represents numerical values.
It can be + or -.
No of student in a class.
format.
3. Character:
Character da t a structure is used to store nonnumeric
information.
It can be letters [A-Z], [a-z], digits and special
symbols.
A character is represented in memory as a sequence
bits. 6
5. Pointer:
1. Array:
Array is a n ordered set which consist of a fixed
number of object.
Insertion and deletion operation can be performed
on array.
2. List:
List is a n ordered set which consist of variable
number of elements or object. 8
Insertion and deletion can be performed on
list.
CONT
…
List is classified into two categories:
1. Linear Data structure(Linear List)
2. Non linear Data structure(Non Linear List)
Node
11
CONT
…
NON LINEAR DATA STRUCTURE
In the Non linier data structure processing of
d a t a items is not possible in linier fashion.
Examples of the non linier data structure
are:
(A)Tree
(A) Tree: (B)InGraph
t r ee data contains hierarchical
VPMP
relationship.
VPMP
CE EE 12
ME
CONT
…
(B) Graph: this da t a structure contains relationship
between pairs of elements.
3. File:
A file is a large list t ha t is stored in the external
memory of computer.
A file may be used as a repository for list items
commonly called records.
13
OPERATION OF DATA
STRUCTURE
1. Traversing: Access each element of the
data structure and doing some processing
over the data.
2. Inserting: Insertion of new elements into
the dat a structure.
3. Deleting: Deletion of specific elements.
4. Searching: Searching for a specific element.
5. Sorting: Sorting the data in ascending
or descending ordered.
6. Merging: Combination of two data
structure.
14
1.4 INTRODUCTION TO
ALGORITHMS
Algorithm is a stepwise solution to a problem.
Algorithm is a finite set of instructions t h a t is
followed to accomplish a particular task.
In mathematics and computing, a n algorithm is a
procedure for accomplishing some task which will
terminate in a defined end-state.
15
CONT
…
Properties required for algorithm.
1. Finiteness: “An algorithm must
always terminate after a finite number
of steps”.
2. Definiteness: “Each steps of algorithm must
be precisely defined”.
3. Input: “quantities which are given to it
initially before the algorithm begins”.
4. Output: “quantities which have a
specified relation to the input”.
5. Effectiveness: “all the operations to be
performed in the algorithm m ust be
sufficient”. 16
1.5 KEY FEATURES OF
ALGORITHM
Name of Algorithm: Every Algorithm is given
a n identifying name, written in capital letters.
Steps: The algorithm is made of sequence of
steps.
Assignment Statements:The assignment
statement is indicated by arrow.Ex:X10
If Statements:
1. If (condition)
then
2. If (condition)
then 17
else
CONT
…Repeat Statement
Repeat while(condition)
Repeat t h r u step No for variable name=1,2,…N
Case Statement
Select case
Case value 1:
Case value 2:
Case value N:
Default:
GOTO Statement: It is used for unconditional
transfer of controls.
Exit Statement: It is used to terminate an 18
algorithm.
Example:
Write Algorithm to compute the average of n
elements.
Steps:
20
CONT
…
Asymptotic Notation: Is a way of expressing the
cost of a n algorithm.
Worst Case Complexity: Slowest time to
complete with chosen input.
When the algorithm takes maximum no of steps
on any instance of size n.
22
CONT…
Location(Address) of A[i] in the array is calculated
as:
24
CONT
…
26
CONT…
Two Dimensional Array (Storage Representation)
Column-major Arrays: If two dimensional array
element store sequentially column by column then it is
calld Column major array.
27
CONT
…
29
1.9 SEARCHING AN ELEMENT INTO
AN Searching:
ARRAY Search is used to find whether the
desired dat a is present in set of data or not.If it
is present, the search operation provides
us its position.
There are two types of searching technique.
L INEAR SEARCH / S EQUENTIAL S EARCH
B INARY S EARCH
30
SEQUENTIAL SEARCH OR LINEAR
SEARCH
This method is used to find out element in an
unordered list.
Suppose the list L consist of N elements and we
want to find the element from the list.
In this technique first the value of the element is
compared with the value of the first element in
the list, if match is found then the search is
terminated.
If match is not found then next element from the
list is fetched and compared with the element.
This process is repeated until match is found or
all the element in the list is compared.
Consider the following example:
The list L consist of 5 elements as shown below: 31
CONT
…
SEQENTIAL_SEARCH(K, N, X)
These function searches the list K consist of N
elements for the value of X.
Step 1: [Initialize search]
I1
K [N+1] X
Step 2:[Search the Vector]
Repeat while K [I] ≠ X
II+1
Step 3: [Successful Search?]
If I = N+1 then
Write “Unsuccessful Search”
Return 0
Else 32
Writ
e
BINARY
SEARCH
This method is used to find out element in an
ordered list.
It is very efficient method to search element.
interval.
CONT
…
If the value of the middle element is smaller then
the element to be searched then the element will
exist in upper half of the list. So take low = mid +
1 and find value of the middle in this new
interval.
This process is repeated until entire list is
searched or the element is found.
Suppose we want to find the element 275 in the
above list.
First, mid = (low + high)/2
= (1+10)/2
=5
34
CONT
…Here the value of middle element is 318 which is
greater then 275 so the element is in the lower
half of the list.
Take high = mid – 1 = 5-1=4
= (1 + 4)/2
=2
Here the value of the middle element is 151
which is smaller then 275 so the element is in the
upper half of the list.
Take low = mid + 1 = 2+1 = 3
Now mid = (low + high)/2
35
= (3+4)/2
=3
CONT
…
Here the value of the middle element is 203
which is smaller then 275 so the element is in the
upper half of the list.
Take low = mid + 1 = 3+1 = 4
= (4+4)/2
=4
Here the value of the middle element is 275
which we want to find.
36
BINARY_SEARCH(K, N, X)
These function searches the list K consist of N
elements for the value of X.
LOW, HIGH and MIDDLE denotes the lower,
upper and middle limit of the list.
Step 1.[Initialize]
LOW1
HIGHN
Step 2. [Perform
Search]
Repeat
t h r u step 4
while LOW
≤ HIGH 37