SlideShare a Scribd company logo
1
G BHARATH KUMAR, Assistant Professor, CSE Department
MODULE 1:
INTRODUCTION
I. Basic Terminology: Elementary Data Organization:
1. Data: Data are simply values or sets of values.
 The word "data" was first used to mean "Transmissible and storable computer information" in
1946.
Data Documents: whenever data needs to be registered, data exists in the form of a data
documents. Different kinds of data documents include:
 data repository
 data study
 data set
 software
 data paper
 data base
 data handbook
 data journal etc...
Structure: Structure means to purposefully arrange something in a specific way.
 Structure is an arrangement and organization of interrelated elements in a system.
 The arrangement of and relations between the parts of something complex.
 a building or other object constructed from several parts.
2. Data items: Data items refers to a single unit of values.
Data items that are divided into sub-items are called Group items. Ex: An Employee Name
may be divided into three subitems- first name, middle name, and last name.
Data items that are not able to divide into sub-items are called Elementary items.
Ex: R.No, Gender, Age etc...
3. Entity: An entity can be a real-world object, either animate or inanimate, that can be easily
Identifiable. The values may be either numeric or non-numeric.
Attributes- Names, Age, Sex, SSN
Values- Rohland Gail, 34, F, 134-34-5533
For Example, in a college data base students, Staff, classes and courses offered can be considered as
entities.
All these entities have some attributes that give them their identity.
4. Field is a single elementary unit of information representing an attribute of an entity.
5. Record is the collection of field values of a given entity.
2
G BHARATH KUMAR, Assistant Professor, CSE Department
6. File is the collection of records of the entities in a given entity set.
Each record in a file may contain many field items but the value in a certain field may uniquely
determine the record in the file. Such a field K is called a primary key and the values k1, k2,
….. in such a field are called keys or key values.
Records may also be classified according to length.
A file can have fixed-length records or variable-length records.
 In fixed-length records, all the records contain the same data items with the same amount
of space assigned to each data item.
 In variable-length records file records may contain different lengths.
Example: Student records have variable lengths, since different students take different
numbers of courses. Variable-length records have a minimum and a maximum length.
The above organization of data into fields, records and files may not be complex enough to
maintain and efficiently process certain collections of data. For this reason, data are also
organized into more complex types of structures.
7. Data Structures
Data may be organized in many different ways. The logical or mathematical model of a
particular organization of data is called a data structure.
The choice of a particular data model depends on the two considerations
1. It must be rich enough in structure to mirror the actual relationships of the data in the
real world.
2. The structure should be simple enough that one can effectively process the data
whenever necessary.
In computer science, a data structure is a data organization, management, and storage format
that enables efficient access and modification. More precisely, a data structure is a collection of data
values, the relationships among them, and the functions or operations that can be applied to the data.
The study of complex data structures includes the following three steps:
1. Logical or mathematical description of the structure
2. Implementation of the structure on a computer
3. Quantitative analysis of the structure, which includes determining the amount of
memory needed to store the structure and the time required to process the structure.
Data Structures Usage
Data structures serve as the basis for abstract data types (ADT). The ADT defines the logical form of
the data type. The data structure implements the physical form of the data type.
Different types of data structures are suited to different kinds of applications, and some are highly
specialized to specific tasks. For example, relational databases commonly use B-tree indexes for data
retrieval, while compiler implementations usually use hash tables to look up identifiers.
3
G BHARATH KUMAR, Assistant Professor, CSE Department
Data structures provide a means to manage large amounts of data efficiently for uses such as
large databases and internet indexing services. Usually, efficient data structures are key to designing
efficient algorithms. Some formal design methods and programming languages emphasize data
structures, rather than algorithms, as the key organizing factor in software design. Data structures
can be used to organize the storage and retrieval of information stored in both main
memory and secondary memory.
Data Structures Implementation
Data structures are generally based on the ability of a computer to fetch and store data at any place in
its memory, specified by a pointer—a bit string, representing a memory address, that can be itself
stored in memory and manipulated by the program. Thus, the array and record data structures are
based on computing the addresses of data items with arithmetic operations, while the linked data
structures are based on storing addresses of data items within the structure itself.
The implementation of a data structure usually requires writing a set of procedures that create and
manipulate instances of that structure. The efficiency of a data structure cannot be analyzed
separately from those operations. This observation motivates the theoretical concept of an abstract
data type, a data structure that is defined indirectly by the operations that may be performed on it,
and the mathematical properties of those operations (including their space and time cost).
CLASSIFICATION OF DATA STRUCTURES
Data structures are generally classified into
 Primitive data Structures
 Non-primitive data Structures
1. Primitive data Structures: Primitive data structures are the fundamental data types which
are supported by a programming language. Basic data types such as integer, real, character
and Boolean are known as Primitive data Structures. These data types consists of characters
that cannot be divided and hence they also called simple data types.
2. Non- Primitive data Structures: Non-primitive data structures are those data structures
which are created using primitive data structures. Examples of non-primitive data structures
is the processing of complex numbers, linked lists, stacks, trees, andgraphs.
Based on the structure and arrangement of data, non-primitive data structures is further
classified into
1. Linear Data Structure
2. Non-linear Data Structure
1. Linear Data Structure:
A data structure is said to be linear if its elements form a sequence or a linear list. There are
basically two ways of representing such linear structure in memory.
1. One way is to have the linear relationships between the elements represented by means
of sequential memory location. These linear structures are called arrays.
2. The other way is to have the linear relationship between the elements represented by
4
G BHARATH KUMAR, Assistant Professor, CSE Department
means of pointers or links. These linear structures are called linked lists.
The common examples of linear data structure are Arrays, Queues, Stacks, Linked lists
2. Non-linear Data Structure:
A data structure is said to be non-linear if the data are not arranged in sequence or a linear. The
insertion and deletion of data is not possible in linear fashion. This structure is mainly used to
represent data containing a hierarchical relationship between elements. Trees and graphs are
the examples of non-linear data structure.
Abstract Data Types
Abstract Data type (ADT) is a type (or class) for objects whose behaviour is defined by a set of value and a set of operations.
The definition of ADT only mentions what operations are to be performed but not how these operations will be implemented. It
does not specify how data will be organized in memory and what algorithms will be used for implementing the operations. It is
called ―abstract‖ because it gives an implementation-independent view. The process of providing only the essentials and hiding
the details is known as abstraction.
The user of data type does not need to know how that data type is implemented, for example, we have been using
Primitive values like int, float, char data types only with the knowledge that these data type can operate and be
performed on without any idea of how they are implemented. So a user only needs to know what a data type can do,
but not how it will be implemented. Think of ADT as a black box which hides the inner structure and design of the
data type.
Trees
Data frequently contain a hierarchical relationship between various elements. The data structure
which reflects this relationship is called a rooted tree graph or a tree.
Some of the basic properties of tree are explained by means of examples
Example 1: Record Structure
Although a file may be maintained by means of one or more arrays a record, where one indicates
both the group items and the elementary items, can best be described by means of a tree
structure.
For example, an employee personnel record may contain the following data items:
Social Security Number, Name, Address, Age, Salary, Dependents
5
G BHARATH KUMAR, Assistant Professor, CSE Department
However, Name may be a group item with the sub-items Last, First and MI (middle initial).
Also Address may be a group item with the subitems Street address and Area address, where
Area itself may be a group item having subitems City, State and ZIP codenumber.
This hierarchical structure is pictured below
Another way of picturing such a tree structure is in terms of levels, as shown below
Some of the data structures are briefly described below.
1. Stack: A stack, also called a fast-in first-out (LIFO) system, is a linear list in which insertions
and deletions can take place only at one end, called the top. This structure is similar in its
operation to a stack of dishes on a spring system as shown in fig.
Note that new 4 dishes are inserted only at the top of the stack and dishes can be deleted only
from the top of the Stack.
6
G BHARATH KUMAR, Assistant Professor, CSE Department
7
G BHARATH KUMAR, Assistant Professor, CSE Department
2. Queue: A queue, also called a first-in first-out (FIFO) system, is a linear list in which
deletions can take place only at one end of the list, the "from'' of the list, and insertions can take
place only at the other end of the list, the ―rear‖ of the list.
This structure operates in much the same way as a line of people waiting at a bus stop, as
pictured in Fig. the first person in line is the first person to board the bus. Another analogy is
with automobiles waiting to pass through an intersection the first car in line is the first car
through.
3. Graph: Data sometimes contain a relationship between pairs of elements which is not
necessarily hierarchical in nature. For example, suppose an airline flies only between the cities
connected by lines in Fig. The data structure which reflects this type of relationship is called a
graph.
8
G BHARATH KUMAR, Assistant Professor, CSE Department
II. DATA STRUCTURES OPERATIONS
The data appearing in data structures are processed by means of certain operations.
The following four operations play a major role in thistext:
1. Traversing: accessing each record/node exactly once so that certain items in the record
may be processed. (This accessing and processing is sometimes called ―visiting‖ the
record.)
2. Searching: Finding the location of the desired node with a given key value, or finding
the locations of all such nodes which satisfy one or more conditions.
3. Inserting: Adding a new node/record to the structure.
4. Deleting: Removing a node/record from the structure.
The following two operations, which are used in special situations:
1. Sorting: Arranging the records in some logical order (e.g., alphabetically according to
some NAME key, or in numerical order according to some NUMBER key, such as social
security number or account number)
2. Merging: Combining the records in two different sorted files into a single sorted file.
V Space-Time Trade-off
Types of Space-Time Trade-off
 Compressed or Uncompressed data
 Re Rendering or Stored images
 Smaller code or loop unrolling
 Lookup tables or Recalculation
Compressed or Uncompressed data: A space-time trade-off can be applied to the problem of data
storage.
If data stored is uncompressed, it takes more space but less time.
But if the data is stored compressed, it takes less space but more time to run the decompression algorithm.
There are many instances where it is possible to directly work with compressed data. In that case of
compressed bitmap indices, where it is faster to work with compression than without compression.
Re-Rendering or Stored images: In this case, storing only the source.
rendering it as an image would take more space but less time i.e., storing an image in the cache is faster
than re-rendering but requires more space in memory.
Smaller code or Loop Unrolling(larger code- without loops): Smaller code occupies less space in
memory but it requires high computation time that is required for jumping back to the beginning of the
loop at the end of each iteration.
9
G BHARATH KUMAR, Assistant Professor, CSE Department
Loop unrolling can optimize execution speed at the cost of increased binary size. It occupies more space in
memory but requires less computation time.
Lookup tables or Recalculation: In a lookup table, an implementation can include the entire table which
reduces computing time but increases the amount of memory needed.
It can recalculate i.e., compute table entries as needed, increasing computing time but reducing memory
requirements.
Searching Algorithms
Searching-
 Searching is a process of finding a particular element among several given elements.
 The search is successful if the required element is found.
 Otherwise, the search is unsuccessful.
Searching Algorithms-
Searching Algorithms are a family of algorithms used for the purpose of searching.
10
G BHARATH KUMAR, Assistant Professor, CSE Department
The searching of an element in the given array may be carried out in the following two ways-
1. Linear Search
2. Binary Search
In this article, we will discuss about Linear Search Algorithm.
VI. Linear Search
 Linear Search is the simplest searching algorithm.
 It traverses the array sequentially to locate the required element.
 It searches for an element by comparing it with each element of the array one by one.
 So, it is also called as Sequential Search.
Linear Search Algorithm is applied when-
 No information is given about the array.
 The given array is unsorted or the elements are unordered.
 The list of data items is smaller.
Linear Search Algorithm-
Consider-
 There is a linear array ‗a‘ of size ‗n‘.
 Linear search algorithm is being used to search an element ‗item‘ in this linear array.
 If search ends in success, it sets loc to the index of the element otherwise it sets loc to -1.
In computer science, a linear search or sequential search is a method for finding an element within
a list. It sequentially checks each element of the list until a match is found or the whole list has been
searched.[1]
11
G BHARATH KUMAR, Assistant Professor, CSE Department
A linear search runs in at worst linear time and makes at most n comparisons, where n is the length of the
list. If each element is equally likely to be searched, then linear search has an average case
of n+1/2 comparisons, but the average case can be affected if the search probabilities for each element
vary. Linear search is rarely practical because other search algorithms and schemes, such as the binary
search algorithm and hash tables, allow significantly faster searching for all but short lists.[2]
Linear search is a very simple search algorithm. In this type of search, a sequential search is made over
all items one by one. Every item is checked and if a match is found then that particular item is returned,
otherwise the search continues till the end of the data collection.
Algorithm
Linear Search ( Array A, Value x)
Step 1: Set i to 1
Step 2: if i > n then go to step 7
Step 3: if A[i] = x then go to step 6
Step 4: Set i to i + 1
Step 5: Go to Step 2
Step 6: Print Element x Found at index i and go to step 8
Step 7: Print element not found
Step 8: Exit
Pseudocode
procedure linear_search (list, value)
for each item in the list
if match item == value
return the item's location
end if
end for
end procedure
A simple approach is to do a linear search, i.e
 Start from the leftmost element of arr[] and one by one compare x with each element of arr[]
 If x matches with an element, return the index.
 If x doesn‘t match with any of elements, return -1.
Note:
Linear search is rarely used practically because other search algorithms such as the binary search algorithm
and hash tables allow significantly faster-searching comparison to Linear search.
12
G BHARATH KUMAR, Assistant Professor, CSE Department
Linear Search vs Binary Search
A linear search scans one item at a time, without jumping to any item .
1. The worst case complexity is O(n), sometimes known an O(n) search
2. Time taken to search elements keep increasing as the number of elements are increased.
A binary search however, cut down your search to half as soon as you find middle of a sorted list.
1. The middle element is looked to check if it is greater than or less than the value to be searched.
2. Accordingly, search is done to either half of the given list
Important Differences
 Input data needs to be sorted in Binary Search and not in Linear Search
 Linear search does the sequential access whereas Binary search access data randomly.
 Time complexity of linear search -O(n) , Binary search has time complexity O(log n).
 Linear search performs equality comparisons and Binary search performs ordering comparisons
Class Search algorithm
Worst-case performance O(n)
Best-case performance O(1)
Average performance O(n/2)
Worst-case space complexity O(1) iterative
#include<stdio.h>
int main()
{
int a[20],i,x,n;
printf("How many elements?");
scanf("%d",&n);
printf("Enter array elements:n");
for(i=0;i<n;++i)
scanf("%d",&a[i]);
printf("nEnter element to search:");
scanf("%d",&x);
for(i=0;i<n;++i)
if(a[i]==x)
break;
if(i<n)
13
G BHARATH KUMAR, Assistant Professor, CSE Department
printf("Element found at index %d",i);
else
printf("Element not found");
return 0;
}
Complexity of the Linear Search Algorithm
VII Binary Search-
 Binary Search is one of the fastest searching algorithms.
 It is used for finding the location of an element in a linear array.
 It works on the principle of divide and conquer technique.
Binary Search Algorithm can be applied only on Sorted arrays.
So, the elements must be arranged in-
 Either ascending order if the elements are numbers.
 Or dictionary order if the elements are strings.
To apply binary search on an unsorted array,
 First, sort the array using some sorting technique.
 Then, use binary search algorithm.
Binary Search Algorithm-
Consider-
 There is a linear array ‗a‘ of size ‗n‘.
 Binary search algorithm is being used to search an element ‗item‘ in this linear array.
 If search ends in success, it sets loc to the index of the element otherwise it sets loc to -1.
 Variables beg and end keeps track of the index of the first and last element of the array or sub array in
which the element is being searched at that instant.
 Variable mid keeps track of the index of the middle element of that array or sub array in which the
element is being searched at that instant.
Then, Binary Search Algorithm is as follows-
Begin Set beg = 0 Set end = n-1 Set mid = (beg + end) / 2 while ( (beg <= end) and (a[mid] ≠ item) ) do
if (item < a[mid]) then Set end = mid - 1 else Set beg = mid + 1 endif Set mid = (beg + end) / 2
14
G BHARATH KUMAR, Assistant Professor, CSE Department
endwhile if (beg > end) then Set loc = -1 else Set loc = mid endif End
Explanation
Binary Search Algorithm searches an element by comparing it with the middle most
element of the array.
Then, following three cases are possible-
Case-01
If the element being searched is found to be the middle most element, its index is
returned.
Case-02
If the element being searched is found to be greater than the middle most element,
then its search is further continued in the right sub array of the middle most element.
Case-03
If the element being searched is found to be smaller than the middle most element,
then its search is further continued in the left sub array of the middle most element.
This iteration keeps on repeating on the sub arrays until the desired element is found
or size of the sub array reduces to zero.
Time Complexity Analysis-
Binary Search time complexity analysis is done below-
 In each iteration or in each recursive call, the search gets reduced to half of the array.
 So for n elements in the array, there are log2n iterations or recursive calls.
Thus, we have-
Time Complexity of Binary Search Algorithm is O(log2n).
15
G BHARATH KUMAR, Assistant Professor, CSE Department
Here, n is the number of elements in the sorted linear array.
This time complexity of binary search remains unchanged irrespective of the element position even if it
is not present in the array.
Binary Search Example-
Consider-
 We are given the following sorted linear array.
 Element 15 has to be searched in it using Binary Search Algorithm.
Binary Search Algorithm works in the following steps-
Step-01:
 To begin with, we take beg=0 and end=6.
 We compute location of the middle element as-
mid
= (beg + end) / 2
= (0 + 6) / 2
= 3
 Here, a[mid] = a[3] = 20 ≠ 15 and beg < end.
 So, we start next iteration.
Step-02:
 Since a[mid] = 20 > 15, so we take end = mid – 1 = 3 – 1 = 2 whereas beg remains unchanged.
 We compute location of the middle element as-
mid
= (beg + end) / 2
= (0 + 2) / 2
16
G BHARATH KUMAR, Assistant Professor, CSE Department
= 1
 Here, a[mid] = a[1] = 10 ≠ 15 and beg < end.
 So, we start next iteration.
Step-03:
 Since a[mid] = 10 < 15, so we take beg = mid + 1 = 1 + 1 = 2 whereas end remains unchanged.
 We compute location of the middle element as-
mid
= (beg + end) / 2
= (2 + 2) / 2
= 2
 Here, a[mid] = a[2] = 15 which matches to the element being searched.
 So, our search terminates in success and index 2 is returned.
Binary Search Algorithm Advantages-
The advantages of binary search algorithm are-
 It eliminates half of the list from further searching by using the result of each comparison.
 It indicates whether the element being searched is before or after the current position in the list.
 This information is used to narrow the search.
 For large lists of data, it works significantly better than linear search.
Binary Search Algorithm Disadvantages-
The disadvantages of binary search algorithm are-
 It employs recursive approach which requires more stack space.
 Programming binary search algorithm is error prone and difficult.
 The interaction of binary search with memory hierarchy i.e. caching is poor.
(because of its random access nature)
Important Note-
For in-memory searching, if the interval to be searched is small,
 Linear search may exhibit better performance than binary search.
 This is because it exhibits better locality of reference.

More Related Content

DOCX
Linked List
DOCX
ADT STACK and Queues
PPTX
Data structure &amp; algorithms introduction
DOCX
Chapter 1
PPTX
Data Structure & Algorithms | Computer Science
PPTX
Data structure,abstraction,abstract data type,static and dynamic,time and spa...
PDF
Data structure
PPTX
What is Link list? explained with animations
Linked List
ADT STACK and Queues
Data structure &amp; algorithms introduction
Chapter 1
Data Structure & Algorithms | Computer Science
Data structure,abstraction,abstract data type,static and dynamic,time and spa...
Data structure
What is Link list? explained with animations

What's hot (20)

PPTX
Introduction to data structure ppt
PPT
Chapter 1( intro &amp; overview)
PPTX
Introduction to data structure
PPTX
Linked Lists
PPT
Data structure
PPT
Lecture 1 data structures and algorithms
PPT
data structure
PDF
Data and File Structure Lecture Notes
PPTX
Data structure
PPTX
Data Structures
PPT
Introductiont To Aray,Tree,Stack, Queue
PPT
Chapter 8: tree data structure
PPTX
Mca ii dfs u-1 introduction to data structure
PPTX
Introduction to data structure
PPTX
Data structure & its types
PPTX
Data structure and its types.
PPTX
Presentation on Data Structure
PDF
lect 2-DS ALGO(online).pdf
PPTX
Data Structure
PPT
Introduction to data structure
Introduction to data structure ppt
Chapter 1( intro &amp; overview)
Introduction to data structure
Linked Lists
Data structure
Lecture 1 data structures and algorithms
data structure
Data and File Structure Lecture Notes
Data structure
Data Structures
Introductiont To Aray,Tree,Stack, Queue
Chapter 8: tree data structure
Mca ii dfs u-1 introduction to data structure
Introduction to data structure
Data structure & its types
Data structure and its types.
Presentation on Data Structure
lect 2-DS ALGO(online).pdf
Data Structure
Introduction to data structure
Ad

Similar to Introduction of Data Structures and Algorithms by GOWRU BHARATH KUMAR (20)

PDF
Introduction of data structures and algorithms
PPTX
INTRODUCTION TO DATA STRUCTURE & ABSTRACT DATA TYPE.pptx
PDF
Datastructures Notes
PPTX
Lecture 1. Data Structure & Algorithm.pptx
PPTX
Introduction to Data Structures
PDF
BCA DATA STRUCTURES INTRODUCTION AND OVERVIEW SOWMYA JYOTHI
PDF
Data Structure Ppt for our engineering college industrial training.
PPTX
DSA - Copy.pptx
PPTX
Different types of sorting used in programming.pptx
PPTX
UNIT 3.pptx-Data Structures definition with examples
PPTX
DataStructurePpt.pptx
DOC
data structures and algorithm Cha 1and 2.doc
PPT
Data structure and algorithms unit 1 pdf SRM
PDF
Data structures introduction
PPTX
DataStructurePpt-01.pptxEngineering data structure notes
PDF
3130703dsgtudarshan Enotesallunits Darshan Institute Of Engineering Technology
PPTX
Introduction to data structures (ss)
PPTX
Unit-1 DataStructure Intro.pptx
PDF
unit 1 ds.INTRODUCTION TO DATA STRUCTURES
PPTX
Data structure chapter 1.pptx
Introduction of data structures and algorithms
INTRODUCTION TO DATA STRUCTURE & ABSTRACT DATA TYPE.pptx
Datastructures Notes
Lecture 1. Data Structure & Algorithm.pptx
Introduction to Data Structures
BCA DATA STRUCTURES INTRODUCTION AND OVERVIEW SOWMYA JYOTHI
Data Structure Ppt for our engineering college industrial training.
DSA - Copy.pptx
Different types of sorting used in programming.pptx
UNIT 3.pptx-Data Structures definition with examples
DataStructurePpt.pptx
data structures and algorithm Cha 1and 2.doc
Data structure and algorithms unit 1 pdf SRM
Data structures introduction
DataStructurePpt-01.pptxEngineering data structure notes
3130703dsgtudarshan Enotesallunits Darshan Institute Of Engineering Technology
Introduction to data structures (ss)
Unit-1 DataStructure Intro.pptx
unit 1 ds.INTRODUCTION TO DATA STRUCTURES
Data structure chapter 1.pptx
Ad

More from BHARATH KUMAR (15)

PPTX
Object-Oriented concepts.pptx
PPTX
Java buzzwords.pptx
PPTX
history and evaluation of java.pptx
PPTX
Data Models
PPTX
Structure of a DBMS/Architecture of a DBMS
PPTX
DBMS languages/ Types of SQL Commands
PPTX
1.4 data independence
PPTX
data abstraction in DBMS
PPTX
File system vs DBMS
PPTX
DBMS introduction
DOCX
Trees and Graphs in data structures and Algorithms
DOCX
Sorting
PPTX
Why we study LMC? by GOWRU BHARATH KUMAR
PDF
A Survey on Big Data Analytics
PPTX
Relation between Languages, Machines and Computations
Object-Oriented concepts.pptx
Java buzzwords.pptx
history and evaluation of java.pptx
Data Models
Structure of a DBMS/Architecture of a DBMS
DBMS languages/ Types of SQL Commands
1.4 data independence
data abstraction in DBMS
File system vs DBMS
DBMS introduction
Trees and Graphs in data structures and Algorithms
Sorting
Why we study LMC? by GOWRU BHARATH KUMAR
A Survey on Big Data Analytics
Relation between Languages, Machines and Computations

Recently uploaded (20)

PPT
Mechanical Engineering MATERIALS Selection
PPTX
bas. eng. economics group 4 presentation 1.pptx
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PPTX
CH1 Production IntroductoryConcepts.pptx
DOCX
573137875-Attendance-Management-System-original
PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PDF
composite construction of structures.pdf
PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
PPTX
OOP with Java - Java Introduction (Basics)
PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PPT
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
PPTX
additive manufacturing of ss316l using mig welding
PPTX
Construction Project Organization Group 2.pptx
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PPT
Project quality management in manufacturing
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
Mechanical Engineering MATERIALS Selection
bas. eng. economics group 4 presentation 1.pptx
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
CH1 Production IntroductoryConcepts.pptx
573137875-Attendance-Management-System-original
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
composite construction of structures.pdf
Automation-in-Manufacturing-Chapter-Introduction.pdf
OOP with Java - Java Introduction (Basics)
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
additive manufacturing of ss316l using mig welding
Construction Project Organization Group 2.pptx
UNIT-1 - COAL BASED THERMAL POWER PLANTS
Project quality management in manufacturing
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
Embodied AI: Ushering in the Next Era of Intelligent Systems

Introduction of Data Structures and Algorithms by GOWRU BHARATH KUMAR

  • 1. 1 G BHARATH KUMAR, Assistant Professor, CSE Department MODULE 1: INTRODUCTION I. Basic Terminology: Elementary Data Organization: 1. Data: Data are simply values or sets of values.  The word "data" was first used to mean "Transmissible and storable computer information" in 1946. Data Documents: whenever data needs to be registered, data exists in the form of a data documents. Different kinds of data documents include:  data repository  data study  data set  software  data paper  data base  data handbook  data journal etc... Structure: Structure means to purposefully arrange something in a specific way.  Structure is an arrangement and organization of interrelated elements in a system.  The arrangement of and relations between the parts of something complex.  a building or other object constructed from several parts. 2. Data items: Data items refers to a single unit of values. Data items that are divided into sub-items are called Group items. Ex: An Employee Name may be divided into three subitems- first name, middle name, and last name. Data items that are not able to divide into sub-items are called Elementary items. Ex: R.No, Gender, Age etc... 3. Entity: An entity can be a real-world object, either animate or inanimate, that can be easily Identifiable. The values may be either numeric or non-numeric. Attributes- Names, Age, Sex, SSN Values- Rohland Gail, 34, F, 134-34-5533 For Example, in a college data base students, Staff, classes and courses offered can be considered as entities. All these entities have some attributes that give them their identity. 4. Field is a single elementary unit of information representing an attribute of an entity. 5. Record is the collection of field values of a given entity.
  • 2. 2 G BHARATH KUMAR, Assistant Professor, CSE Department 6. File is the collection of records of the entities in a given entity set. Each record in a file may contain many field items but the value in a certain field may uniquely determine the record in the file. Such a field K is called a primary key and the values k1, k2, ….. in such a field are called keys or key values. Records may also be classified according to length. A file can have fixed-length records or variable-length records.  In fixed-length records, all the records contain the same data items with the same amount of space assigned to each data item.  In variable-length records file records may contain different lengths. Example: Student records have variable lengths, since different students take different numbers of courses. Variable-length records have a minimum and a maximum length. The above organization of data into fields, records and files may not be complex enough to maintain and efficiently process certain collections of data. For this reason, data are also organized into more complex types of structures. 7. Data Structures Data may be organized in many different ways. The logical or mathematical model of a particular organization of data is called a data structure. The choice of a particular data model depends on the two considerations 1. It must be rich enough in structure to mirror the actual relationships of the data in the real world. 2. The structure should be simple enough that one can effectively process the data whenever necessary. In computer science, a data structure is a data organization, management, and storage format that enables efficient access and modification. More precisely, a data structure is a collection of data values, the relationships among them, and the functions or operations that can be applied to the data. The study of complex data structures includes the following three steps: 1. Logical or mathematical description of the structure 2. Implementation of the structure on a computer 3. Quantitative analysis of the structure, which includes determining the amount of memory needed to store the structure and the time required to process the structure. Data Structures Usage Data structures serve as the basis for abstract data types (ADT). The ADT defines the logical form of the data type. The data structure implements the physical form of the data type. Different types of data structures are suited to different kinds of applications, and some are highly specialized to specific tasks. For example, relational databases commonly use B-tree indexes for data retrieval, while compiler implementations usually use hash tables to look up identifiers.
  • 3. 3 G BHARATH KUMAR, Assistant Professor, CSE Department Data structures provide a means to manage large amounts of data efficiently for uses such as large databases and internet indexing services. Usually, efficient data structures are key to designing efficient algorithms. Some formal design methods and programming languages emphasize data structures, rather than algorithms, as the key organizing factor in software design. Data structures can be used to organize the storage and retrieval of information stored in both main memory and secondary memory. Data Structures Implementation Data structures are generally based on the ability of a computer to fetch and store data at any place in its memory, specified by a pointer—a bit string, representing a memory address, that can be itself stored in memory and manipulated by the program. Thus, the array and record data structures are based on computing the addresses of data items with arithmetic operations, while the linked data structures are based on storing addresses of data items within the structure itself. The implementation of a data structure usually requires writing a set of procedures that create and manipulate instances of that structure. The efficiency of a data structure cannot be analyzed separately from those operations. This observation motivates the theoretical concept of an abstract data type, a data structure that is defined indirectly by the operations that may be performed on it, and the mathematical properties of those operations (including their space and time cost). CLASSIFICATION OF DATA STRUCTURES Data structures are generally classified into  Primitive data Structures  Non-primitive data Structures 1. Primitive data Structures: Primitive data structures are the fundamental data types which are supported by a programming language. Basic data types such as integer, real, character and Boolean are known as Primitive data Structures. These data types consists of characters that cannot be divided and hence they also called simple data types. 2. Non- Primitive data Structures: Non-primitive data structures are those data structures which are created using primitive data structures. Examples of non-primitive data structures is the processing of complex numbers, linked lists, stacks, trees, andgraphs. Based on the structure and arrangement of data, non-primitive data structures is further classified into 1. Linear Data Structure 2. Non-linear Data Structure 1. Linear Data Structure: A data structure is said to be linear if its elements form a sequence or a linear list. There are basically two ways of representing such linear structure in memory. 1. One way is to have the linear relationships between the elements represented by means of sequential memory location. These linear structures are called arrays. 2. The other way is to have the linear relationship between the elements represented by
  • 4. 4 G BHARATH KUMAR, Assistant Professor, CSE Department means of pointers or links. These linear structures are called linked lists. The common examples of linear data structure are Arrays, Queues, Stacks, Linked lists 2. Non-linear Data Structure: A data structure is said to be non-linear if the data are not arranged in sequence or a linear. The insertion and deletion of data is not possible in linear fashion. This structure is mainly used to represent data containing a hierarchical relationship between elements. Trees and graphs are the examples of non-linear data structure. Abstract Data Types Abstract Data type (ADT) is a type (or class) for objects whose behaviour is defined by a set of value and a set of operations. The definition of ADT only mentions what operations are to be performed but not how these operations will be implemented. It does not specify how data will be organized in memory and what algorithms will be used for implementing the operations. It is called ―abstract‖ because it gives an implementation-independent view. The process of providing only the essentials and hiding the details is known as abstraction. The user of data type does not need to know how that data type is implemented, for example, we have been using Primitive values like int, float, char data types only with the knowledge that these data type can operate and be performed on without any idea of how they are implemented. So a user only needs to know what a data type can do, but not how it will be implemented. Think of ADT as a black box which hides the inner structure and design of the data type. Trees Data frequently contain a hierarchical relationship between various elements. The data structure which reflects this relationship is called a rooted tree graph or a tree. Some of the basic properties of tree are explained by means of examples Example 1: Record Structure Although a file may be maintained by means of one or more arrays a record, where one indicates both the group items and the elementary items, can best be described by means of a tree structure. For example, an employee personnel record may contain the following data items: Social Security Number, Name, Address, Age, Salary, Dependents
  • 5. 5 G BHARATH KUMAR, Assistant Professor, CSE Department However, Name may be a group item with the sub-items Last, First and MI (middle initial). Also Address may be a group item with the subitems Street address and Area address, where Area itself may be a group item having subitems City, State and ZIP codenumber. This hierarchical structure is pictured below Another way of picturing such a tree structure is in terms of levels, as shown below Some of the data structures are briefly described below. 1. Stack: A stack, also called a fast-in first-out (LIFO) system, is a linear list in which insertions and deletions can take place only at one end, called the top. This structure is similar in its operation to a stack of dishes on a spring system as shown in fig. Note that new 4 dishes are inserted only at the top of the stack and dishes can be deleted only from the top of the Stack.
  • 6. 6 G BHARATH KUMAR, Assistant Professor, CSE Department
  • 7. 7 G BHARATH KUMAR, Assistant Professor, CSE Department 2. Queue: A queue, also called a first-in first-out (FIFO) system, is a linear list in which deletions can take place only at one end of the list, the "from'' of the list, and insertions can take place only at the other end of the list, the ―rear‖ of the list. This structure operates in much the same way as a line of people waiting at a bus stop, as pictured in Fig. the first person in line is the first person to board the bus. Another analogy is with automobiles waiting to pass through an intersection the first car in line is the first car through. 3. Graph: Data sometimes contain a relationship between pairs of elements which is not necessarily hierarchical in nature. For example, suppose an airline flies only between the cities connected by lines in Fig. The data structure which reflects this type of relationship is called a graph.
  • 8. 8 G BHARATH KUMAR, Assistant Professor, CSE Department II. DATA STRUCTURES OPERATIONS The data appearing in data structures are processed by means of certain operations. The following four operations play a major role in thistext: 1. Traversing: accessing each record/node exactly once so that certain items in the record may be processed. (This accessing and processing is sometimes called ―visiting‖ the record.) 2. Searching: Finding the location of the desired node with a given key value, or finding the locations of all such nodes which satisfy one or more conditions. 3. Inserting: Adding a new node/record to the structure. 4. Deleting: Removing a node/record from the structure. The following two operations, which are used in special situations: 1. Sorting: Arranging the records in some logical order (e.g., alphabetically according to some NAME key, or in numerical order according to some NUMBER key, such as social security number or account number) 2. Merging: Combining the records in two different sorted files into a single sorted file. V Space-Time Trade-off Types of Space-Time Trade-off  Compressed or Uncompressed data  Re Rendering or Stored images  Smaller code or loop unrolling  Lookup tables or Recalculation Compressed or Uncompressed data: A space-time trade-off can be applied to the problem of data storage. If data stored is uncompressed, it takes more space but less time. But if the data is stored compressed, it takes less space but more time to run the decompression algorithm. There are many instances where it is possible to directly work with compressed data. In that case of compressed bitmap indices, where it is faster to work with compression than without compression. Re-Rendering or Stored images: In this case, storing only the source. rendering it as an image would take more space but less time i.e., storing an image in the cache is faster than re-rendering but requires more space in memory. Smaller code or Loop Unrolling(larger code- without loops): Smaller code occupies less space in memory but it requires high computation time that is required for jumping back to the beginning of the loop at the end of each iteration.
  • 9. 9 G BHARATH KUMAR, Assistant Professor, CSE Department Loop unrolling can optimize execution speed at the cost of increased binary size. It occupies more space in memory but requires less computation time. Lookup tables or Recalculation: In a lookup table, an implementation can include the entire table which reduces computing time but increases the amount of memory needed. It can recalculate i.e., compute table entries as needed, increasing computing time but reducing memory requirements. Searching Algorithms Searching-  Searching is a process of finding a particular element among several given elements.  The search is successful if the required element is found.  Otherwise, the search is unsuccessful. Searching Algorithms- Searching Algorithms are a family of algorithms used for the purpose of searching.
  • 10. 10 G BHARATH KUMAR, Assistant Professor, CSE Department The searching of an element in the given array may be carried out in the following two ways- 1. Linear Search 2. Binary Search In this article, we will discuss about Linear Search Algorithm. VI. Linear Search  Linear Search is the simplest searching algorithm.  It traverses the array sequentially to locate the required element.  It searches for an element by comparing it with each element of the array one by one.  So, it is also called as Sequential Search. Linear Search Algorithm is applied when-  No information is given about the array.  The given array is unsorted or the elements are unordered.  The list of data items is smaller. Linear Search Algorithm- Consider-  There is a linear array ‗a‘ of size ‗n‘.  Linear search algorithm is being used to search an element ‗item‘ in this linear array.  If search ends in success, it sets loc to the index of the element otherwise it sets loc to -1. In computer science, a linear search or sequential search is a method for finding an element within a list. It sequentially checks each element of the list until a match is found or the whole list has been searched.[1]
  • 11. 11 G BHARATH KUMAR, Assistant Professor, CSE Department A linear search runs in at worst linear time and makes at most n comparisons, where n is the length of the list. If each element is equally likely to be searched, then linear search has an average case of n+1/2 comparisons, but the average case can be affected if the search probabilities for each element vary. Linear search is rarely practical because other search algorithms and schemes, such as the binary search algorithm and hash tables, allow significantly faster searching for all but short lists.[2] Linear search is a very simple search algorithm. In this type of search, a sequential search is made over all items one by one. Every item is checked and if a match is found then that particular item is returned, otherwise the search continues till the end of the data collection. Algorithm Linear Search ( Array A, Value x) Step 1: Set i to 1 Step 2: if i > n then go to step 7 Step 3: if A[i] = x then go to step 6 Step 4: Set i to i + 1 Step 5: Go to Step 2 Step 6: Print Element x Found at index i and go to step 8 Step 7: Print element not found Step 8: Exit Pseudocode procedure linear_search (list, value) for each item in the list if match item == value return the item's location end if end for end procedure A simple approach is to do a linear search, i.e  Start from the leftmost element of arr[] and one by one compare x with each element of arr[]  If x matches with an element, return the index.  If x doesn‘t match with any of elements, return -1. Note: Linear search is rarely used practically because other search algorithms such as the binary search algorithm and hash tables allow significantly faster-searching comparison to Linear search.
  • 12. 12 G BHARATH KUMAR, Assistant Professor, CSE Department Linear Search vs Binary Search A linear search scans one item at a time, without jumping to any item . 1. The worst case complexity is O(n), sometimes known an O(n) search 2. Time taken to search elements keep increasing as the number of elements are increased. A binary search however, cut down your search to half as soon as you find middle of a sorted list. 1. The middle element is looked to check if it is greater than or less than the value to be searched. 2. Accordingly, search is done to either half of the given list Important Differences  Input data needs to be sorted in Binary Search and not in Linear Search  Linear search does the sequential access whereas Binary search access data randomly.  Time complexity of linear search -O(n) , Binary search has time complexity O(log n).  Linear search performs equality comparisons and Binary search performs ordering comparisons Class Search algorithm Worst-case performance O(n) Best-case performance O(1) Average performance O(n/2) Worst-case space complexity O(1) iterative #include<stdio.h> int main() { int a[20],i,x,n; printf("How many elements?"); scanf("%d",&n); printf("Enter array elements:n"); for(i=0;i<n;++i) scanf("%d",&a[i]); printf("nEnter element to search:"); scanf("%d",&x); for(i=0;i<n;++i) if(a[i]==x) break; if(i<n)
  • 13. 13 G BHARATH KUMAR, Assistant Professor, CSE Department printf("Element found at index %d",i); else printf("Element not found"); return 0; } Complexity of the Linear Search Algorithm VII Binary Search-  Binary Search is one of the fastest searching algorithms.  It is used for finding the location of an element in a linear array.  It works on the principle of divide and conquer technique. Binary Search Algorithm can be applied only on Sorted arrays. So, the elements must be arranged in-  Either ascending order if the elements are numbers.  Or dictionary order if the elements are strings. To apply binary search on an unsorted array,  First, sort the array using some sorting technique.  Then, use binary search algorithm. Binary Search Algorithm- Consider-  There is a linear array ‗a‘ of size ‗n‘.  Binary search algorithm is being used to search an element ‗item‘ in this linear array.  If search ends in success, it sets loc to the index of the element otherwise it sets loc to -1.  Variables beg and end keeps track of the index of the first and last element of the array or sub array in which the element is being searched at that instant.  Variable mid keeps track of the index of the middle element of that array or sub array in which the element is being searched at that instant. Then, Binary Search Algorithm is as follows- Begin Set beg = 0 Set end = n-1 Set mid = (beg + end) / 2 while ( (beg <= end) and (a[mid] ≠ item) ) do if (item < a[mid]) then Set end = mid - 1 else Set beg = mid + 1 endif Set mid = (beg + end) / 2
  • 14. 14 G BHARATH KUMAR, Assistant Professor, CSE Department endwhile if (beg > end) then Set loc = -1 else Set loc = mid endif End Explanation Binary Search Algorithm searches an element by comparing it with the middle most element of the array. Then, following three cases are possible- Case-01 If the element being searched is found to be the middle most element, its index is returned. Case-02 If the element being searched is found to be greater than the middle most element, then its search is further continued in the right sub array of the middle most element. Case-03 If the element being searched is found to be smaller than the middle most element, then its search is further continued in the left sub array of the middle most element. This iteration keeps on repeating on the sub arrays until the desired element is found or size of the sub array reduces to zero. Time Complexity Analysis- Binary Search time complexity analysis is done below-  In each iteration or in each recursive call, the search gets reduced to half of the array.  So for n elements in the array, there are log2n iterations or recursive calls. Thus, we have- Time Complexity of Binary Search Algorithm is O(log2n).
  • 15. 15 G BHARATH KUMAR, Assistant Professor, CSE Department Here, n is the number of elements in the sorted linear array. This time complexity of binary search remains unchanged irrespective of the element position even if it is not present in the array. Binary Search Example- Consider-  We are given the following sorted linear array.  Element 15 has to be searched in it using Binary Search Algorithm. Binary Search Algorithm works in the following steps- Step-01:  To begin with, we take beg=0 and end=6.  We compute location of the middle element as- mid = (beg + end) / 2 = (0 + 6) / 2 = 3  Here, a[mid] = a[3] = 20 ≠ 15 and beg < end.  So, we start next iteration. Step-02:  Since a[mid] = 20 > 15, so we take end = mid – 1 = 3 – 1 = 2 whereas beg remains unchanged.  We compute location of the middle element as- mid = (beg + end) / 2 = (0 + 2) / 2
  • 16. 16 G BHARATH KUMAR, Assistant Professor, CSE Department = 1  Here, a[mid] = a[1] = 10 ≠ 15 and beg < end.  So, we start next iteration. Step-03:  Since a[mid] = 10 < 15, so we take beg = mid + 1 = 1 + 1 = 2 whereas end remains unchanged.  We compute location of the middle element as- mid = (beg + end) / 2 = (2 + 2) / 2 = 2  Here, a[mid] = a[2] = 15 which matches to the element being searched.  So, our search terminates in success and index 2 is returned. Binary Search Algorithm Advantages- The advantages of binary search algorithm are-  It eliminates half of the list from further searching by using the result of each comparison.  It indicates whether the element being searched is before or after the current position in the list.  This information is used to narrow the search.  For large lists of data, it works significantly better than linear search. Binary Search Algorithm Disadvantages- The disadvantages of binary search algorithm are-  It employs recursive approach which requires more stack space.  Programming binary search algorithm is error prone and difficult.  The interaction of binary search with memory hierarchy i.e. caching is poor. (because of its random access nature) Important Note- For in-memory searching, if the interval to be searched is small,  Linear search may exhibit better performance than binary search.  This is because it exhibits better locality of reference.