Unit 1 - Basic Concepts of Ds
Unit 1 - Basic Concepts of Ds
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.
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.
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:
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)
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.
10
CONT…
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:
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:X10
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.
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.
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 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]
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
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.
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 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
LOWMIDDLE+1
Else
Write “Successful Search”
Return (MIDDLE)
Step 5. [Unsuccessful Search]
Write “Unsuccessful Search”
Return 0
38
39