0% found this document useful (0 votes)
32 views27 pages

Unit1 - Data Structure Operations

Uploaded by

vgadgetstamil
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)
32 views27 pages

Unit1 - Data Structure Operations

Uploaded by

vgadgetstamil
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/ 27

21CSC201J

DATA STRUCTURES AND ALGORITHMS

UNIT-1

DATA STRUCTURE OPERATIONS


Data Structure Operations

• The data in the data structures are processed by certain


operations.
o Traversing
o Searching
o Inserting
o Updating
o Deleting
o Sorting
o Merging
Data Structure Operations (Cont..)
• Traversing - Visiting each record so that items in the records
can be accessed.
• Searching - Finding the location of the record with a given key
or value or finding all records which satisfy given conditions.
• Inserting - Adding a new record to the data structure.
• Updating – Change the content of some elements of the record.
• Deleting - Removing records from the data structure.
• Sorting - Arranging records in some logical or numerical order.
o (Eg: Alphabetic order)
• Merging - Combing records from two different sorted files into
a single file.
Example
• An organization contains a membership file in which each record
contains the following data for a given member:
Name, Address, Telephone Number, Age, Sex
a. Suppose the organization wants to announce a meeting
through a mailing. Then one would traverse the file to obtain
Name and Address of each member.
b. Suppose one wants to find the names of all members living in a
particular area. Again traverse and search the file to obtain
the data.
c. Suppose one wants to obtain Address of a specific Person. Then
one would search the file for the record containing Name.
d. Suppose a member dies. Then one would delete his or her
record from the file.
Example
e. Suppose a new person joins the organization. Then one
would insert a record into the file
f. Suppose a member has shifted to a new residence his/her
address and telephone number need to be changed. Given
the name of the member, one would first need to search for
the record in the file. Then perform the update operation,
i.e. change some items in the record with the new data.
g. Suppose one wants to find the number of members whose
age is 65 or above. Again one would traverse the file,
counting such members.
ABSTRACT DATA TYPE
Abstract Data Types
• An Abstract Data type (ADT) is a type for objects whose behavior is
defined by a set of value and a set of operations.
• ADT refers to a set of data values and associated operations that are
specified accurately, independent of any particular implementation.
• The ADT consists of a set of definitions that allow us to use the
functions while hiding the implementation.
• Abstract in the context of data structures means everything except
the detailed specifications or implementation.
• Data type of a variable is the set of values that the variable can take.

ADT = Type + Function Names + Behaviour of


each function
• Examples: Stacks, Queues, Linked List
ADT Operations
• While modeling the problems the necessary details are
separated out from the unnecessary details. This process of
modeling the problem is called abstraction.
• The model defines an abstract view to the problem. It focuses
only on problem related stuff and that you try to define
properties of the problem.
• These properties include
o The data which are affected.
o The operations which are identified.
ADT Operations ( Cont..)
• Abstract data type operations are
o Create - Create the data structure.
o Display - Displaying all the elements stored in the data
structure.
o Insert - Elements can be inserted at any desired position.
o Delete - Desired element can be deleted from the data
structure.
o Modify - Any desired element can be deleted/modified from
the data structure.
Abstract Data Types Model
• The ADT model has two different parts
functions (public and private) and
data structures.
• Both are contained within the ADT
model itself, and do not come within
the scope of the application program.
• Data structures are available to all of
the ADT’s functions as required, and a
function may call any other
function to accomplish its task.
• Data structures and functions are
within the scope of each other.
Abstract Data Types Model (Cont..)
• Data are entered, accessed, modified
and deleted through the external
application programming interface.
• For each ADT operation, there is an
algorithm that performs its specific
task.
• The operation name and parameters
are available to the application, and
they provide only an interface to the
application.
• When a list is controlled entirely by
the program, it is implemented using
simple structure.
Review Questions

• ______ are used to manipulate the data contained in various data


structures.
• In ______, the elements of a data structure are stored
sequentially.
• ______ of a variable specifies the set of values that the variable
can take.
• Abstract means ______.
• If the elements of a data structure are stored sequentially, then
it is a ______.
ALGORITHMS
SEARCHING
TECHIQUES
Algorithms
• An algorithm is a well defined list of steps for solving a
particular problem.
• The time and space are two major measures of the efficiency of
an algorithm.
• The complexity of an algorithm is the function which gives the
running time and / or space in terms of input size.
Searching Algorithms
• Searching Algorithms are designed to check for an element or
retrieve an element from any data structure where it is stored.
• Based on the type of search operation, these algorithms are
generally classified into two categories:
• Linear Search
• Binary Search
Linear Search
• Linear search is a very basic and simple search algorithm.
• In Linear search, we search an element or value in a given array
by traversing the array from the starting, till the desired
element or value is found or we can establish that the element is
not present.
• It compares the element to be searched with all the elements
present in the array and when the element is matched
successfully, it returns the index of the element in the array, else
it return -1.
• Linear search is applied on unsorted or unordered lists, when
there are fewer elements in a list.
Linear Search - Example
• Search each record of the file, one at a time, until finding the
given value.
Algorithm for Linear Search
• In Steps 1 and 2 of the algorithm, we
initialize the value of POS and I.
• In Step 3, a while loop is executed that
would be executed till I is less than N.
• In Step 4, a check is made to see if a
match is found between the current
array element and VAL.
• If a match is found, then the position of
the array element is printed, else the
value of I is incremented to match the
next element with VAL.
• If all the array elements have been
compared with VAL and no match is
found, then it means that VAL is not
present in the array.
Program for Linear Search
Linear Search - Advantages
• When a key element matches the first element in the array, then
linear search algorithm is best case because executing time of
linear search algorithm is O(n), where n is the number of
elements in an array.
• The list doesn’t have to sort. Contrary to a binary search, linear
searching does not demand a structured list.
• Not influenced by the order of insertions and deletions. Since
the linear search doesn’t call for the list to be sorted, added
elements can be inserted and deleted.
Linear Search - Disadvantages
• The drawback of a linear search is the fact that it is time
consuming if the size of data is huge.
• Every time a vital element matches the last element from the
array or an essential element does not match any element Linear
search algorithm is the worst case.
Binary Search
• Binary Search is used with sorted array or list. In binary search,
we follow the following steps:
1) We start by comparing the element to be searched with the
element in the middle of the list/array.
2) If we get a match, we return the index of the middle
element and terminate the process
3) If the element/number to be searched is greater than the
middle element, we pick the elements on the left/right side
of the middle element, and move to step 1.
4) If the element/number to be searched is lesser in value than
the middle number, then we pick the elements on the
right/left side of the middle element, and move to step 1.
Binary Search - Example
• Binary Search is useful when there are large number of
elements in an array and they are sorted.
Algorithm for Binary Search
• In Step 1, we initialize the value of
variables, BEG, END, and POS.
• In Step 2, a while loop is executed until
BEG is less than or equal to END.
• In Step 3, the value of MID is calculated.
• In Step 4, we check if the array value at
MID is equal to VAL. If a match is found,
then the value of POS is printed and the
algorithm exits. If a match is not found, and
if the value of A[MID] is greater than VAL,
the value of END is modified, otherwise if
A[MID] is greater than VAL, then the value
of BEG is altered.
• In Step 5, if the value of POS = –1, then
VAL is not present in the array and an
appropriate message is printed on the
screen before the algorithm exits.
Program for Binary Search
Binary Search (Cont..)
Advantages Disadvantages
• It eliminates half of the list • It employs recursive approach
from further searching by which requires more stack
using the result of each space.
comparison. • Programming binary search
• It indicates whether the algorithm is error prone and
element being searched is difficult.
before or after the current • The interaction of binary
position in the list. search with memory hierarchy
• This information is used to i.e. caching is poor.
narrow the search.
• For large lists of data, it
works significantly better
than linear search.
Difference between Linear & Binary Search
Linear Search Binary Search
• Linear Search necessitates • Binary Search necessitates
the input information to be the input information to be
not sorted sorted.
• Linear Search needs equality • Binary Search necessitates an
comparisons. ordering contrast.
• Linear search has complexity • Binary Search has complexity
C(n)= n/2. C(n)= log 2 n.
• Linear Search needs • Binary Search requires
sequential access. random access to this
information.

You might also like