[go: up one dir, main page]

0% found this document useful (0 votes)
15 views19 pages

Algo Inisa

The document discusses Java sorting and searching algorithms. It explains selection sort, bubble sort, and common Java searching terminology. Sorting and searching algorithms are important for tasks like data management, databases, machine learning, and more.
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)
15 views19 pages

Algo Inisa

The document discusses Java sorting and searching algorithms. It explains selection sort, bubble sort, and common Java searching terminology. Sorting and searching algorithms are important for tasks like data management, databases, machine learning, and more.
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/ 19

Java Sorting – refers to arrangement of a given management, and file system

array or list of elements according to a organization.


comparison operator on the elements. The HOW DOES SELECTION SORT ALGORITHM
comparison operator is used to decide the new WORK?
order of elements in the respective data ● Lets consider the following array as an
structure. Sorting means reordering of all the example: arr[] =
elements either in ascending or in descending
{64, 25, 12, 22, 11}
order.
● First pass:
Sorting Algorithm – used to rearrange a given ● For the first position in the sorted
array or list of elements according to a array, the whole array is traversed from
comparison operator on the elements. The index 0 to 4 sequentially. The first
comparison operator is used to decide the new position where 64 is stored presently,
order of elements in the respective data
after traversing the whole array it is
structure.
clear that 11 is the lowest value.
Java Selection Sort – algorithm repeatedly ● Thus, replace 64 with 11. After one
selects the smallest (or largest) element from iteration 11, which happens to be the
the unsorted portion of the list and swaps it least value in the array, tends to appear
with the first element of the unsorted part. This in the first position of the sorted list.
process is repeated for the remaining unsorted
portion until the entire list is sorted.

JAVA APPLICATION OF SORTING ALGORITHMS

● Searching Algorithms: Sorting is often a


crucial step in search algorithms like
binary search, Ternary Search, where
the data needs to be sorted before
● Second Pass:
searching for a specific element.
● Data management: Sorting data makes
● For the second position, where 25 is
it easier to search, retrieve, and analyze. present, again traverse the rest of the
● Database optimization: Sorting data in array in a sequential manner.
databases improves query performance. ● After traversing, we found that 12 is
● Machine learning: Sorting is used to the second lowest value in the array
prepare data for training machine and it should appear at the second
learning models. place in the array, thus swap these
● Data Analysis: Sorting helps in values.
identifying patterns, trends, and outliers
in datasets. It plays a vital role in
statistical analysis, financial modeling,
and other data driven fields.
● Operating Systems: Sorting algorithms
are used in operating systems for tasks
like task scheduling, memory
● Third Pass:
● Now, for third place, where 25 is
present again traverse the rest of the
array and find the third least value
present in the array.
● While traversing, 22 came out to be
the third least value and it should
appear at the third place in the array,
thus swap 22 with element present at
third position.

● Fourth pass:
● Similarly, for fourth position traverse the
rest of the array and find the fourth least
element in the array
● As 25 is the 4th lowest value hence, it will
place at the fourth position.
Bubble Sort – simplest sorting algorithm that
works by repeatedly swapping the adjacent
elements if they are in the wrong order. This
algorithm is not suitable for large data sets as its
average and worst-case time complexity is quite
high.

In Bubble Sort algorithm,


● Fifth Pass:
● At last the largest value present in the ● traverse from left and compare adjacent
array automatically get placed at the last elements and the higher one is placed
position in the array at right side.
● The resulted array is the sorted array. ● In this way, the largest element is
moved to the rightmost end at first.
● This process is then continued to find
the second largest and place it and so
on until the data is sorted.
HOW DOES BUBBLE SORT ALGORITHM WORK?
● Input: arr[] = {6, 3, 0, 5}
● First Pass:
● The largest element is placed in its correct
position, i.e., the end of the array.

● Second Pass:
● Place the second largest element at correct
position

● Third Pass:
● Place the remaining two elements at their
correct positions. Java Searching Algorithm – used to locate
specific items within a collection of data.
● Total no. of passes: n-1 Designed to efficiently navigate structures to
● Total no. of comparisons: n*(n-1)/2 find through the data desired information,
making them fundamental in various
applications such as databases, web search
engines, and more.

Searching – fundamental process of locating a


specific element or item within a collection of
data. This collection of data can take various
forms, such as arrays, lists, trees, or other
structured representations. The primary
objective of searching is to determine whether
the desired element exists within the data, and
if so, to identify its precise location or retrieve it.
JAVA SEARCHING TERMINOLOGIES: ● Database Systems: Searching is
fundamental in database systems for
● Target Element: In searching, there is
retrieving specific data records based on
always a specific target element or item
user queries, improving efficiency in
that you want to find within the data
data retrieval.
collection. This target could be a value,
● E-commerce: Searching is crucial in e
a record, a key, or any other data entity
commerce platforms for users to find
of interest.
products quickly based on their
● Search Space: The search space refers
preferences, specifications, or
to the entire collection of data within
keywords.
which you are looking for the target
● Networking: In networking, searching
element. Depending on the data
algorithms are used for routing packets
structure used, the search space may
efficiently through networks, finding
vary in size and organization.
optimal paths, and managing network
● Complexity: Searching can have
resources.
different levels of complexity depending
● Artificial Intelligence: Searching
on the data structure and the algorithm
algorithms play a vital role in AI
used. The complexity is often measured
applications, such as problem-solving,
in terms of time and space
game playing (e.g., chess), and
requirements.
decision-making processes
● Deterministic vs. Non-deterministic:
● Pattern Recognition: Searching
Some searching algorithms, like binary
algorithms are used in pattern matching
search, are deterministic, meaning they
tasks, such as image recognition, speech
follow a clear, systematic approach.
recognition, and handwriting
Others, such as linear search, are
recognition.
non-deterministic, as they may need to
examine the entire search space in the Linear Search Algorithm – a sequential search
worst case. algorithm that starts at one end and goes
through each element of a list until the desired
Importance of Searching in DSA:
element is found, otherwise the search
● Efficiency: Efficient algorithms improve continues till the end of the data set.
searching program performance.
● Data Retrieval: Quickly find and retrieve
specific data from large datasets.
● Database Systems: Enables fast
querying of databases.
● Problem Solving: Used in a wide range
of problem-solving tasks.
In Linear Search Algorithm,
Applications of Searching:
● Every element is considered as a
● Information Retrieval: Search engines potential match for the key and checked
like Google, Bing, and Yahoo use for the same.
sophisticated searching algorithms to
retrieve relevant information from vast
amounts of data on the web.
● If any element is found equal to the key,
the search is successful and the index of
that element is returned.
● If no element is found equal to the key,
the search yields “No match found”.

For example: Consider the array arr[] =


{10, 50, 30,
70, 80, 20, 90, 40} and key = 30
Step 1: Start from the first element
(index 0) and compare key with each
element (arr[i]).
● Comparing key with first element arr[0].
Since not equal, the iterator moves to the
next element as a potential match.

Binary Search Algorithm – a searching


algorithm used in a sorted array by repeatedly
dividing the search interval in half. The idea of
binary search is to use the information that the
array is sorted and reduce the time complexity
● Comparing key with next element arr[1]. to O(log N).
Since not equal, the iterator moves to the
next element as a potential match.

Step 2: Now when comparing arr[2]


with key, the
Conditions for when to apply Binary Search in a
value matches. So the Linear Search
Data Structure:
Algorithm
will yield a successful message and To apply Binary Search algorithm:
return the
● The data structure must be
index of the element when key is found
sorted.
(here 2).
● Access to any element of the
data structure takes constant
time.

In this algorithm,

● Divide the search space into two halves


by finding the middle index “mid”.
● Key is less than the current mid 56. The
search space moves to the left.

● Compare the middle element of the


search space with the key.
● If the key is found at middle element, Second Step: If the key matches the
the process is terminated. value of the
● If the key is not found at middle mid element, the element is found and
element, choose which half will be used stop search.
as the next search space.
o If the key is smaller than the
middle element, then the left
side is used for next search.
o If the key is larger than the
middle element, then the right
side is used for next search.
● This process is continued until the key is The Binary Search Algorithm can be
found or the total search space is implemented in the following two ways:
exhausted. ● Iterative Binary Search Algorithm
How does Binary Search work? ● Recursive Binary Search Algorithm
● To understand the working of binary
search,
consider the following illustration:

Consider an array arr[] = {2, 5, 8, 12, 16,


23, 38, 56, 72, 91}, and the target = 23.
First Step: Calculate the mid and
compare the
mid element with the key. If the key is
less than
mid element, move to left and if it is
greater than the mid then move search
space to the right.
● Key (i.e., 23) is greater than current mid
element (i.e., 16). The search space moves
to the right.
Recursive Binary Search Algorithm:

Iterative Binary Search Algorithm:

Binary Tree - defined as a tree data structure


where each node has at most 2 children.
• Since each element in a binary tree can have
only 2 children, we typically name them the left
and right child.

A Binary tree is represented by a pointer to the


topmost node (commonly known as the “root”)
of the tree. If the tree is empty, then the value
of the root is NULL.

Each node of a Binary Tree contains the


following parts:

● Data
● Pointer to left child
● Pointer to right child
minimum in O(1) time complexity.
• Represent hierarchical data.
• Used in editing software like Microsoft Excel
and spreadsheets.
• Useful for indexing segmented at the database
is useful in storing cache in the system,
• Syntax trees are used for most famous
compilers for programming like GCC, and AOCL
to perform arithmetic operations.
• For implementing priority queues.
• Used to find elements in less time (binary
search tree)
• Used to enable fast memory allocation in
computers.
• Used to perform encoding and decoding
Below is an example of a tree node with integer
operations.
data: • Binary trees can be used to organize and
retrieve information from large datasets, such
as in inverted index and k-d trees.
• Binary trees can be used to represent the
decision-making process of computer controlled
characters in games, such as in decision trees.
• Binary trees can be used to implement
searching algorithms, such as in binary search
trees which can be used to quickly find an
element in a sorted list.
• Binary trees can be used to implement sorting
algorithms, such as in heap sort which uses a
Basic Operation On Binary Tree:
binary heap to sort elements efficiently.
• Inserting an element.
• Removing an element. Binary Tree Traversals
• Searching for an element.
Tree Traversal algorithms can be classified
• Traversing the tree.
broadly into two categories:
Auxiliary Operation On Binary Tree:
• Depth-First Search (DFS) Algorithms
• Finding the height of the tree
• Breadth-First Search (BFS) Algorithms
• Find the level of a node of the tree.
• Finding the size of the entire tree. Depth-First Search (DFS)
Applications of Binary Tree Tree Traversal using Depth-First Search (DFS)
• In compilers, Expression Trees are used which algorithm can be further classified into three
is an application of binary trees. categories:
• Huffman coding trees are used in data • Preorder Traversal (current-left-right)
compression algorithms. • Inorder Traversal (left-current-right)
• Priority Queue is another application of binary • Postorder Traversal (left-right-current)
tree that is used for searching maximum or
Preorder Traversal (current-left-right): • Level-order Traversal of the above tree:
• Visit the current node before visiting any 1-2-3-4-5-6-7
nodes inside the left or right subtrees.
• Here, the traversal is root – left child – right
child. It means that the root node is traversed
first then its left child and finally the right child.

Inorder Traversal (left-current-right):


• Visit the current node after visiting all nodes
inside the left subtree but before visiting any
node within the right subtree.
• Here, the traversal is left child – root – right
child.
• It means that the left child is traversed first
then its root node and finally the right child.

Postorder Traversal (left-right-current): Pre-order Traversal (DFS)


• Visit the current node after visiting all the • Pre-order Traversal of the above tree:
nodes of the left and right subtrees. 1-2-4-5-3-6-7
• Here, the traversal is left child – right child – • Preorder Traversal (current-leftright):
root. • Visit the current node before visiting any
• It means that the left child has traversed first nodes inside the left or right subtrees.
then the right child and finally its root node. • Here, the traversal is root – left child – right
child. It means that the root node is traversed
Breadth-First Search (BFS)
first then its left child and finally the right child.
Tree Traversal using Breadth-First Search (BFS)
algorithm can be further classified into one
category:
• Level Order Traversal

Level Order Traversal:


• Visit nodes level-by-level and left-to-right
fashion at the same level. Here, the traversal is
level-wise.
• It means that the most left child has traversed
first and then the other children of the same
level from left to right have traversed.

Binary Tree Traversals


• Pre-order Traversal of the above tree: In-order Traversal (DFS)
1-2-4-5-3-6-7 • In-order Traversal of the above tree:
• In-order Traversal of the above tree: 4-2-5-1-6-3-7
4-2-5-1-6-3-7 • Inorder Traversal (left-currentright):
• Post-order Traversal of the above tree: • Visit the current node after visiting all nodes
4-5-2-6-7-3-1 inside the left subtree but before visiting any
node within the right subtree.
• Here, the traversal is left child – root – right fashion at the same level. Here, the traversal is
child. level-wise.
• It means that the left child is traversed first • It means that the most left child has traversed
then its root node and finally the right child. first and then the other children of the same
level from left to right have traversed.

Post-order Traversal (DFS)


• Post-order Traversal of the above tree: Implementation of Binary Tree
4-5-2-6-7-3-1
Let us create a simple tree with 4 nodes. The
• Postorder Traversal (left-rightcurrent):
created tree would be as follows.
• Visit the current node after visiting all the
nodes of the left and right subtrees.
• Here, the traversal is left child – right child –
root.
• It means that the left child has traversed first
then the right child and finally its root node.

Level-order Traversal (BFS)


• Level-order Traversal of the above tree:
1-2-3-4-5-6-7
• Visit nodes level-by-level and left-to-right
Types of Binary Tree based on the number of
children:
• Full Binary Tree
• Degenerate Binary Tree
• Skewed Binary Trees

Full Binary Tree


• A Binary Tree is a full binary tree if every node
has 0 or 2 children. Skewed Binary Tree
• We can also say a full binary tree is a binary • A skewed binary tree is a
tree in which all nodes except leaf nodes have pathological/degenerate tree in which the tree
two children. is either dominated by the left nodes or the
• A full Binary tree is a special type of binary right nodes.
tree in which every parent node/internal node • Thus, there are two types of skewed binary
has either two or no children. tree: left-skewed binary tree and right-skewed
• It is also known as a proper binary tree. binary tree.

Degenerate (or pathological) tree


Types of Binary Tree On the basis of the
• A Tree where every internal node has one
completion of levels:
child.
• Complete Binary Tree
• Such trees are performance wise same as
• Perfect Binary Tree
linked list.
• Balanced Binary Tree
• A degenerate or pathological tree is a tree
having a single child either left or right. Complete Binary Tree
• A Binary Tree is a Complete Binary Tree if all
the levels are completely filled except possibly
the last level and the last level has all keys as
left as possible.

A complete binary tree is just like a full binary


tree, but with two major differences:
• Every level except the last level must be
completely filled.
• All the leaf elements must lean towards the
left.
• The last leaf element might not have a right
sibling i.e. a complete binary tree doesn’t have
to be a full binary tree.
Balanced Binary Tree
• A binary tree is balanced if the height of the
tree is O(Log n) where n is the number of nodes.
• For Example, the AVL tree maintains O(Log n)
height by making sure that the difference
between the heights of the left and right
subtrees is at most 1.
• Red-Black trees maintain O(Log n) height by
making sure that the number of Black nodes on
every root to leaf paths is the same and that
there are no adjacent red nodes.
Perfect Binary Tree • Balanced Binary Search trees are
• A Binary tree is a Perfect Binary Tree in which performance-wise good as they provide O(log n)
all the internal nodes have two children and all time for search, insert and delete.
leaf nodes are at the same level. • It is a type of binary tree in which the
• A perfect binary tree is a type of binary tree in difference between the height of the left and
which every internal node has exactly two child the right subtree for each node is either 0 or 1.
nodes and all the leaf nodes are at the same • In the figure below, the root node having a
level. value 0 is unbalanced with a depth of 2 units.
• In a Perfect Binary Tree, the number of leaf
nodes is the number of internal nodes plus 1
• L = I + 1 Where L = Number of leaf nodes, I =
Number of internal nodes.
• A Perfect Binary Tree of height h (where the
height of the binary tree is the number of edges
in the longest path from the root node to any
leaf node in the tree, height of root node is 0)
has 2h+1 – 1 node.
• An example of a Perfect binary tree is
ancestors in the family. Special Types of Trees On the basis of node
• Keep a person at root, parents as children, values:
parents of parents as their children. • Binary Search Tree
• AVL Tree and retrieve large amounts of data.
• Red Black Tree • A B- tree is characterized by a fixed maximum
• B Tree degree (or order), which determines the
• B+ Tree maximum number of child nodes that a parent
• Segment Tree node can have.
• Each node in a B-tree can have multiple child
nodes and multiple keys, and the keys are used
to index and locate data items.

Binary Search Tree - a node-based binary tree


data structure that has the following properties: B+ Tree
• The left subtree of a node contains only nodes
• is a balanced binary search tree that follows a
with keys lesser than the node’s key.
multi-level index format.
• The right subtree of a node contains only
• The leaf nodes of a B+ tree denote actual data
nodes with keys greater than the node’s key.
pointers.
• The left and right subtree each must also be a
• B+ tree ensures that all leaf nodes remain at
binary search tree.
the same height, thus balanced.
AVL tree - is a self-balancing Binary Search Tree • Additionally, the leaf nodes are linked using a
(BST) where the difference between heights of link list; therefore, a B+ tree can support
left and right subtrees cannot be more than one random access as well as sequential access.
for all nodes. • Structure of B+ Tree Every leaf node is at equal
• The tree is AVL because the differences distance from the root node.
between the heights of left and right subtrees • A B+ tree is of the order n where n is fixed for
for every node are less than or equal to 1. every B+ tree

Red-black tree - a kind of self-balancing binary


search tree where each node has an extra bit,
and that bit is often interpreted as the color
(red or black).
• These colors are used to ensure that the tree
remains balanced during insertions and
deletions
• This tree was invented in 1972 by Rudolf
Bayer.

B- tree - a type of self balancing tree structure


that data allows efficient access, insertion, and
deletion of data items.
• B-trees are commonly used in databases and Segment Tree
file systems, where they can efficiently store • Segment Tree, also known as a statistic tree, is
a tree data structure used for storing ● Indexes are created as a combination of
information about intervals, or segments. the two columns.
• It allows querying which of the stored
Indexing (First column)
segments contain a given point.
• It is, in principle, a static structure; that is, it’s ● First column is the Search key.
a structure that cannot be modified once it’s ● It contains a copy of the primary key or
built. A similar data structure is the interval candidate key of the table.
tree. ● The values of this column may be sorted
• A segment tree for a set I of n intervals uses or not.
O(n log n) storage and can be built in O(n log n) ● But if the values are sorted, the
time. corresponding data can be accessed
• Segment trees support searching for all the easily.
intervals that contain a query point in time ● Second column is the Data reference or
O(log n + k), k being the number of retrieved Pointer.
intervals or segments. ● It contains the address of the disk block
where we can find the corresponding
key value.

Indexing (Second column)

● is the Data reference or Pointer.


● It contains the address of the disk block
where we can find the corresponding
File Organization key value.

● is a logical relationship among various


records.
● This method defines how file records
are mapped onto disk blocks.
● it is used to describe the way in which
the records are stored in terms of
blocks, and the blocks are placed on the
storage medium.

Indexing

● Indexing minimizes the number of disk


accesses required when a query is
processed.
Attributes of Indexing Primary Indexing

● Access Types - refers to the type of ● Primary index is defined on an ordered


access such as value-based search, data file.
range access, etc. ● The data file is ordered on a key field.
● Access Time - refers to the time needed ● The key field is generally the primary
to find a particular data element or set key of the relation.
of elements. ● Primary indexing only has two columns.
● Insertion Time - refers to the time taken ● First column has the primary key values
to find the appropriate space and insert which are the search keys.
new data. ● The second column has the pointers
● Deletion Time - Time taken to find an which contain the address to the
item and delete it as well as update the corresponding data block of the search
index structure. key value.
● Space Overhead - refers to the ● The table should be ordered and there
additional space required by the index. is a one-to-one relationship between
the records in the index file and the
Indexing
data blocks.
● a data structure technique that helps to ● This is a more traditional yet a fast
speed up data retrieval. mechanism.
● As we can quickly locate and access the
Non-clustered or Secondary Indexing
data in the database, it is a must-know
data structure that will be needed for ● Secondary index may be generated
database optimizing. from a field which is a candidate key
● Indexing is defined based on its indexing and has a unique value in every record,
attributes or a non-key with duplicate values.
● A non-clustered index just tells us
Types of Indexing
where the data lies, it gives us a list of
● Primary Indexing virtual pointers or references to the
● Secondary Indexing (Non clustered location where the data is actually
Indexing) stored.
● Clustered Indexing ● Data is not physically stored in the order
● Multilevel Indexing of the index.
● Instead, data is present in leaf nodes.
● For ex. the contents page of a book.
● Each entry gives us the page number or
location of the information stored.
● The actual data here(information on
each page of the book) is not organized
but we have an ordered
reference(contents page) to where the
data points actually lie.
● In the case of a clustered index, data is
directly present in front of the index.
● The clustering index is defined on an
ordered data file. The data file is
ordered on a non-key field.
● In some cases, the index is created on
non primary key columns which may
not be unique for each record.
● Essentially, records with similar
properties are grouped together, and
indexes for these groupings are formed.
Clustering Index ● Students studying each semester, for
example, are grouped together.
● Clustering index is defined on an ● First-semester students,
ordered data file. second-semester students,
● The data file is ordered on a non-key third-semester students, and so on are
field. categorized.
● The table is ordered in clustered
indexing.
● The table is ordered in clustered
indexing.
● At the times when the indexes are
created using the non primary key, we
combine two or more columns together
to get the unique values to identify data
uniquely and use it to create the index. Dense Index

● In dense index, there is an index record


for every search key value in the
database.
● This makes searching faster but requires
more space to store index records itself.
● Index records contain search key value
and a pointer to the actual record on
the disk.

● When more than two records are stored


in the same file this type of storing is
known as cluster indexing.
● By using cluster indexing we can reduce ● There is an index record that contains a
the cost of searching reason being search key and pointer for every search
multiple records related to the same key value in the data file.
thing are stored in one place and it also ● Though the Dense index is a fast
gives the frequent joining of more than method it requires more memory to
two tables (records). store index records for each key value.
sequential search until the desired data
is found.
● There are only a few index records that
point to the search key value.
● First, the index record starts searching
sequentially by pointing to a location of
a value in the data file until it finds the
actual location of the search key value.
● For every search key value in the data ● it requires less memory to store index
file, there is an index record. records as it has less of them.
● This record contains the search key and ● The index record appears only for a few
also a reference to the first data record items in the data file. Each item points
with that search key value. to a block as shown.
● To locate a record, we find the index
record with the largest search key value
less than or equal to the search key
value we are looking for.
● We start at that record pointed to by
the index record, and proceed along
with the pointers in the file (that is,
sequentially) until we find the desired
record.
● Number of Accesses required=log₂(n)+1,
Sparse Index (here n=number of blocks acquired by
index file)
● records are not created for every search
key
● index record contains a search key and
an actual pointer to the data on the disk

● To search a record, we first proceed by


index record and reach at the actual
location of the data.
● If the data we are looking for is not
where we directly reach by following
the index, then the system starts
Multilevel Index Advantages of Indexing

Improved Query Performance:

● Indexing enables faster data retrieval


from the database.
● The database may rapidly discover rows
that match a specific value or collection
of values by generating an index on a
column, minimizing the amount of time
it takes to perform a query.

Efficient Data Access:

● Indexing can enhance data access


efficiency by lowering the amount of
disk I/O required to retrieve data.
● The database can maintain the data
● Index records comprise search-key pages for frequently visited columns in
values and data pointers. memory by generating an index on
● Multilevel index is stored on the disk those columns, decreasing the
along with the actual database files. requirement to read from disk.
● As the size of the database grows, so
Optimized Data Sorting:
does the size of the indices.
● There is an immense need to keep the ● Indexing can also improve the
index records in the main memory so as performance of sorting operations.
to speed up the search operations. ● By creating an index on the columns
● If single-level index is used, then a large used for sorting, the database can avoid
size index cannot be kept in memory sorting the entire table and instead sort
which leads to multiple disk accesses. only the relevant rows.
● Multi-level Index helps in breaking
down the index into several smaller Consistent Data Performance:
indices in order to make the outermost ● Indexing can assist ensure that the
level so small that it can be saved in a database performs consistently even as
single disk block, which can easily be the amount of data in the database
accommodated anywhere in the main rises.
memory. ● Without indexing, queries may take
● If the primary index does not fit in the longer to run as the number of rows in
memory, multilevel indexing used. the table grows, while indexing
● When the is database increases its size maintains a roughly consistent speed.
the indices increased. ● By ensuring that only unique values are
● A single-level index can be too big to inserted into columns that have been
store in the main memory. indexed as unique, indexing can also be
● In multilevel indexing, the main data utilized to ensure the integrity of data.
block breaks down into smaller blocks
that can be stored in the main memory.
● This avoids storing duplicate data in the
database, which might lead to issues
when performing queries or reports.

Disadvantages of Indexing

● Indexing necessitates more storage


space to hold the index data structure,
which might increase the total size of
the database.
● Increased database maintenance
overhead: Indexes must be maintained
as data is added, destroyed, or modified
in the table, which might raise database
maintenance overhead.
● Indexing can reduce insert and update
performance since the index data
structure must be updated each time
data is modified.
● Choosing an index can be difficult as it
can be challenging to choose the right
indexes for a specific query or
application and may call for a detailed
examination of the data and access
patterns.

You might also like