0% found this document useful (0 votes)
61 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 PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 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 PDF, TXT or read online on Scribd
You are on page 1/ 39

UNIT:1

1
Introduction to Data
Structure
TOPICS TO BE COVERED….
1.1 Data Structure Basic Concepts
1.2 Types of data structures
1.3 Primitive and non-primitive data structures
1.4 Introduction to Algorithms
1.5 Key features of an algorithm
1.6 Analysis Terms
1.7 Array
1.8 Overview of various array operations.
1.9 Searching an element into an 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 that
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 that are directly processed by
machine using its instructions are known as
primitive data 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 data 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
 Two most commonly known character set supported
by computer are ASCII and EBCDIC.
CONT…
4. Logical:
 A logical data item is a primitive data structure that
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 that it provides faster
7
insertion and deletion of elements.
CONT…
NON PRIMITIVE DATA STRUCTURE
 The data structures that are not directly processed
by machine using its instructions are known as non
primitive data structure.
 Following are the non primitive data structure:

1. Array:
 Array is an ordered set which consist of a fixed
number of object.
 Insertion and deletion operation can be performed
on array.
2. List:
 List is an 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

1. Linear Data structure(Linear List)


 In the linear data structure processing of data items
is possible in linear fashion.
 Data are processed one by one sequentially.
 Examples of the linear data structure are:
(A) Array (B) Stack (C) Queue (D) Linked List

9
(A) Array: Array is an ordered set which consist of a
fixed number of object.
CONT…
(B) Stack: A stack is a linier list in which insertion
and deletion operations are performed at only one
end of the list.

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

10
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
data items is not possible in linier fashion.
 Examples of the non linier data structure are:

 (A)Tree (B) Graph

 (A) Tree: In treeVPMP


data contains hierarchical
VPMP
relationship. VPMP
EE
CEV
ME VP
PM
VP MP
PVP
MP VP
MP
VP MP
MP
12
CONT…

(B) Graph: this data structure contains relationship


between pairs of elements.

3. File:
A file is a large list that 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
data 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 that is
followed to accomplish a particular task.
 In mathematics and computing, an 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 must be sufficient”.

16
1.5 KEY FEATURES OF ALGORITHM
 Name of Algorithm: Every Algorithm is given
an 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 thru 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.
18
 Exit Statement: It is used to terminate an
algorithm.
 Example:
 Write Algorithm to compute the average of n
elements.
 Steps:
ALGORITHM: FIND AVERAGE OF N NUMBER
Assume that 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 an 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 an algorithm takes in terms of the
amount of input to the algorithm

 Space Complexity: It is a function describing the


amount of memory an algorithm takes in terms of
the amount of input to the algorithm.

20
CONT…
 Asymptotic Notation: Is a way of expressing the
cost of an 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 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
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.
L1 is the lower bound for row. 25
L2 is the lower bound for column.
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.
L1 is the lower bound for row. 28
L2 is the lower bound for column.
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 ARRAY
 Searching: Search is used to find whether the
desired data 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.
 LINEAR SEARCH / SEQUENTIAL SEARCH
 BINARY SEARCH

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
Write “Successful Search”
Return 1
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 thru step 4 while LOW ≤ HIGH
Step 3. [Obtain Index of midpoint interval]
MIDDLE [(LOW + HIGH)/2]

37
CONT…
Step 4. [Compare Key Value to Search Element]
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”
Return 0
38
39

You might also like