0% found this document useful (0 votes)
34 views39 pages

Unit 1 - Basic Concepts of Ds

Uploaded by

Ilesh Shah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views39 pages

Unit 1 - Basic Concepts of Ds

Uploaded by

Ilesh Shah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 39

UNIT:1

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.

 Logical relation among the data items.

 The logical or mathematical model of a data


organization is called data structure.

 Data structure means Data + Algorithm(logical


arrangement)
3
1.2 TYPES OF DATA STRUCTURES

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.

 The sign - magnitude method and 1's

complement method are used to represent


integer numbers.
5
 Ex: 1000.
CONT
2.…
Real:
 The number having fractional part is
called real number.
 Ex: 123.45

 Real numbers are also stored in scientific

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

 commonly known character set supported


CONT

4. Logical:
 A logical dat a item is a primitive data structure t ha t
can assume the values of either “true” or “false”.
 Most commonly used logical operators Are AND, OR
and NOT.

5. Pointer:

 Pointer is a variable which points to the memory


address. This memory address is the location of other
variable in memory.
 Another characteristic is t h a t it provides faster
7
insertion and deletion of elements.
CONT

NON PRIMITIVE DATA STRUCTURE
 The dat a structures t h a t are not directly processed
by machine using its instructions are known as non
primitive d a t a structure.
 Following are the non primitive data structure:

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)

It describe has follow

3. Linear Data structure(Linear List)


 In the linear dat a structure processing of data items
is possible in linear fashion.
 Data are processed one by one sequentially.
 Examples of the linear da t a structure are:

(A) Array (B) Stack (C) Queue (D) Linked List

(A) Array: Array is a n ordered set which consist of a 9

fixed number of object.


CONT

(B) Stack: A stack is a linier list in which insertion
and deletion operations are performed a t only one
end of the list.

(C) Queue: A queue is a linier list in which insertion


is performed a t one end called rear and deletion is
performed a t another end of the list called front.
CONT

(D)Linked list: A linked list is a


collection of nodes.
Each node has two fields:
1) Information
2) Address, which contains the address of
the
next node.

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:X10
 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:

ALGORITHM: FIND AVERAGE OF N NUMBER


Assume t h a t array contains n integer values.
Step 1: [INITIALIZE] sum ←0.
Step 2: [PERFORM SUM OPERATION]
Repeat steps from i=0 to n-1
sum←sum+a[i]
Step 3: [CALCULATE AVERAGE]
avg←sum/n
Step 4: Write avg
Step 5: [FINISHED]
Exit 19
1.6 ANALYSIS TERMS
 Complexity: Complexity of a n algorithm is a
function describing the efficiency of the algorithm
in terms of the amount of data.

 Time complexity: It is a function describing the


amount of time a n algorithm takes in terms of the
amount of input to the algorithm

 Space Complexity: It is a function describing the


amount of memory a n algorithm takes in terms of
the amount of input to the algorithm.

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.

 Best Case Complexity: Fastest time to complete


with chosen input.
When the algorithm takes minimum no of steps
on any instance of size n.
 Average case: Arithmetic mean.

Run the algorithm many times using many


different inputs, compute the total running time
and divide by the no of trials. 21
1.7 ARRAY
 One dimensional array can be declared as
int a[20].
Storage Representation of One Dimensional
Array:

22
CONT…
Location(Address) of A[i] in the array is calculated
as:

L0 is the starting(Base) address or address of first


element in the array.
I is the array index.
L is the starting index of a n 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
23
= 2004
CONT…
Two Dimensional Array (Storage Representation)
 Row-major Arrays: If two dimensional array element
store sequentially row by row then it is calld Row major
array.

24
CONT

The address of element A[i, j] can be obtained by


evaluating expression:
Loc (A [i, j]) = Base + ((i – L1) * n + (j – L2))*
s
i is the row index.
j is the column index.
m is the no. of rows.
n is the no of column.
S is size. 25
L1 is the lower
bound for row.
CONT…
 for example the address of element A[1,2] in a[2][3]
is calculated as:
A [1, 2] = Base + ((i – L1) * n + (j – L2))* s
Here, n=3, m=2, i=1, j=2, Base = 3000

=3000 + ((1 - 0) * 3 + (2 - 0))*2


=3000 + (3+2)*2
=3000 + 10
=3010

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

The address of element A[i, j] can be obtained by


evaluating expression:
Loc (A [i, j]) = Base + ((j – L2) * m + (i – L1))*
s
i is the row index.
j is the column index.
m is the no. of rows.
n is the no of column.
S is size. 28
L1 is the lower
bound for row.
CONT…
A [1, 2] = = Base + ((j – L2) * m + (i – L1))*
s
Here, n=3, m=2, i=1, j=2, Base = 3000
=3000 + ((2 - 0) * 2 + (1 - 0))*2
=3000 + (4+1)*2
=3000 + 10
=3010

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]

I1
K [N+1] X
Step 2:[Search the Vector]
Repeat while K [I] ≠ X
II+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.

 Let low represents lower limit of the list and high


represents the higher limit of the list.
 First we calculate the value of middle as:

 Mid = (Low + High)/2

 Now we compare the value of the middle element


with the element to be searched.
 If the value of the middle element is greater then
the element to be searched then the element will
exist in lower half of the list. So take high = mid
– 1 and find value of the middle in this new 33

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

 Now mid = (low + high)/2

= (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

 Now mid = (low + high)/2

= (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]
LOW1
HIGHN
Step 2. [Perform
Search]
Repeat
t h r u step 4
while LOW
≤ HIGH 37

Step 3. [Obtain Index


of midpoint interval]
CONT
… 4. [Compare Key Value to Search Element]
Step
If X < K [MIDDLE] then
HIGH  MIDDLE – 1
Else if X > K [MIDDLE] then
LOWMIDDLE+1
Else
Write “Successful Search”
Return (MIDDLE)
Step 5. [Unsuccessful Search]
Write
“Unsuccessful Search”
R 38
eturn 0
39

You might also like