0% found this document useful (0 votes)
22 views

Algorithm Design Unit 2

Algorithm Design Unit 2

Uploaded by

komalkhati457
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Algorithm Design Unit 2

Algorithm Design Unit 2

Uploaded by

komalkhati457
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 35

ALGORITHM DESIGN

UNIT 2

Non-linear Data Structure :


Data structures where data elements are not arranged sequentially or linearly are
called non-linear data structures. In a non-linear data structure, single level is not
involved. Therefore, we can’t traverse all the elements in single run only. Non-linear
data structures are not easy to implement in comparison to linear data structure. It
utilizes computer memory efficiently in comparison to a linear data structure. Its
examples are trees and graphs.

Binary Tree Data Structure


Learn more about Binary Tree in DSA Self Paced Course
Practice Problems on Binary Tree !
Top Quizzes on Binary Tree
What is Binary Tree Data Structure?
Binary Tree is 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.

Binary Tree Representation

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:
1. Data
2. Pointer to left child
3. Pointer to right child
Basic Operation On Binary Tree:
 Inserting an element.
 Removing an element.
 Searching for an element.
 Traversing the tree.
Auxiliary Operation On Binary Tree:
 Finding the height of the tree
 Find the level of a node of the tree
 Finding the size of the entire tree.

Topic :
 Introduction
 Basic Operation
 Traversals
 Standard Problems on Binary Trees
Introduction :
1. Introduction to Binary Tree – Data Structure and Algorithm Tutorials
2. Properties of Binary Tree
3. Types of Binary Tree
4. Applications, Advantages and Disadvantages of Binary Tree
5. Binary Tree (Array implementation)
6. Complete Binary Tree
7. Perfect Binary Tree
Basic Operations on Binary Tree:
1. Tree Traversals (Inorder, Preorder and Postorder)
2. Level Order Tree Traversal
3. Find the Maximum Depth or Height of given Binary Tree
4. Insertion in a Binary Tree
5. Deletion in a Binary Tree
6. Enumeration of Binary Trees
Some other important Binary Tree Traversals :
1. Level order traversal in spiral form
2. Reverse Level Order Traversal
3. BFS vs DFS for Binary Tree
4. Inorder Tree Traversal without Recursion
5. Morris traversal for Preorder
6. Iterative Preorder Traversal
7. Iterative Postorder Traversal Using Two Stacks
8. Diagonal Traversal of Binary Tree
9. Boundary Traversal of binary tree

Expression tree in data structure


The expression tree is a tree used to represent the various expressions. The tree data structure
is used to represent the expressional statements. In this tree, the internal node always denotes
the operators.
o The leaf nodes always denote the operands.
o The operations are always performed on these operands.
o The operator present in the depth of the tree is always at the highest priority.
o The operator, which is not much at the depth in the tree, is always at the lowest
priority compared to the operators lying at the depth.
o The operand will always present at a depth of the tree; hence it is considered
the highest priority among all the operators.
o In short, we can summarize it as the value present at a depth of the tree is at the
highest priority compared with the other operators present at the top of the tree.
o The main use of these expression trees is that it is used to evaluate,
analyze and modify the various expressions.
o It is also used to find out the associativity of each operator in the expression.
o For example, the + operator is the left-associative and / is the right-associative.
o The dilemma of this associativity has been cleared by using the expression trees.
o These expression trees are formed by using a context-free grammar.
o We have associated a rule in context-free grammars in front of each grammar
production.
o These rules are also known as semantic rules, and by using these semantic rules, we
can be easily able to construct the expression trees.
o It is one of the major parts of compiler design and belongs to the semantic analysis
phase.
o In this semantic analysis, we will use the syntax-directed translations, and in the form
of output, we will produce the annotated parse tree.
o An annotated parse tree is nothing but the simple parse associated with the type
attribute and each production rule.
o The main objective of using the expression trees is to make complex expressions and
can be easily be evaluated using these expression trees.
o It is immutable, and once we have created an expression tree, we can not change it or
modify it further.
o To make more modifications, it is required to construct the new expression tree
wholly.
o It is also used to solve the postfix, prefix, and infix expression evaluation.
Expression trees play a very important role in representing the language-level code in the
form of the data, which is mainly stored in the tree-like structure. It is also used in the
memory representation of the lambda expression. Using the tree data structure, we can
express the lambda expression more transparently and explicitly. It is first created to convert
the code segment onto the data segment so that the expression can easily be evaluated.

The expression tree is a binary tree in which each external or leaf node corresponds to the
operand and each internal or parent node corresponds to the operators so for example
expression tree for 7 + ((1+8)*3) would be:

Algorithm Tutorials
Unlike linear data structures (Array, Linked List, Queues, Stacks, etc) which have
only one logical way to traverse them, trees can be traversed in different ways.
A Tree Data Structure can be traversed in following ways:
1. Depth First Search or DFS
1. Inorder Traversal
2. Preorder Traversal
3. Postorder Traversal

Applications of Tree Data Structure:


1. Store hierarchical data, like folder structure, organization structure,
XML/HTML data.
2. Binary Search Tree is a tree that allows fast search, insert, delete on a
sorted data. It also allows finding closest item
3. Heap is a tree data structure which is implemented using arrays and used
to implement priority queues.
4. B-Tree and B+ Tree : They are used to implement indexing in databases.
5. Syntax Tree: Scanning, parsing , generation of code and evaluation of
arithmetic expressions in Compiler design.
6. K-D Tree: A space partitioning tree used to organize points in K
dimensional space.
7. Trie : Used to implement dictionaries with prefix lookup.
8. Suffix Tree : For quick pattern searching in a fixed text.
9. Spanning Trees and shortest path trees are used in routers and bridges
respectively in computer networks
10. As a workflow for compositing digital images for visual effects.
11. Decision trees.
12. Organization chart of a large organization.
13. In XML parser.
14. Machine learning algorithm.
15. For indexing in database.
16. IN server like DNS (Domain Name Server)
17. In Computer Graphics.
18. To evaluate an expression.
19. In chess game to store defense moves of player.
20. In java virtual machine.
21. Tree data structures are used to organize and manage files and directories
in a file system. Each file and directory is represented as a node in the tree,
with parent-child relationships indicating the hierarchical structure of the
file system.
22. Tree data structures are often used in parsing, such as in compilers and
interpreters, to represent the structure of a program or a document.
23. Tree data structures, such as binary search trees, are commonly used to
implement efficient searching and sorting algorithms.
24. Graphics and UI design
25. Tree data structures are commonly used in decision-making algorithms in
artificial intelligence, such as game-playing algorithms, expert systems,
and decision trees.
26. Tree data structures can be used to represent the topology of a network
and to calculate routing tables for efficient data transmission.
Huffman Coding | Greedy Algo-3
Huffman coding is a lossless data compression algorithm. The idea is to assign
variable-length codes to input characters, lengths of the assigned codes are based on
the frequencies of corresponding characters.
The variable-length codes assigned to input characters are Prefix Codes, means the
codes (bit sequences) are assigned in such a way that the code assigned to one
character is not the prefix of code assigned to any other character. This is how
Huffman Coding makes sure that there is no ambiguity when decoding the generated
bitstream.
Let us understand prefix codes with a counter example. Let there be four characters a,
b, c and d, and their corresponding variable length codes be 00, 01, 0 and 1. This
coding leads to ambiguity because code assigned to c is the prefix of codes assigned to
a and b. If the compressed bit stream is 0001, the de-compressed output may be
“cccd” or “ccb” or “acd” or “ab”.
See this for applications of Huffman Coding.
There are mainly two major parts in Huffman Coding
1. Build a Huffman Tree from input characters.
2. Traverse the Huffman Tree and assign codes to characters.
Data Structure - Binary Search Tree
Previous Page
Next Page
A Binary Search Tree (BST) is a tree in which all the nodes follow the below-mentioned
properties −
 The left sub-tree of a node has a key less than or equal to its parent node's key.
 The right sub-tree of a node has a key greater than or equal to its parent node's
key.
Thus, BST divides all its sub-trees into two segments; the left sub-tree and the right sub-tree
and can be defined as −
left_subtree (keys) ≤ node (key) ≤ right_subtree (keys)
Representation
BST is a collection of nodes arranged in a way where they maintain BST properties. Each
node has a key and an associated value. While searching, the desired key is compared to the
keys in BST and if found, the associated value is retrieved.
Following is a pictorial representation of BST −

We observe that the root node key (27) has all less-valued keys on the left sub-tree and the
higher valued keys on the right sub-tree.

Basic Operations
Following are the basic operations of a tree −
 Search − Searches an element in a tree.
 Insert − Inserts an element in a tree.
 Pre-order Traversal − Traverses a tree in a pre-order manner.
 In-order Traversal − Traverses a tree in an in-order manner.
 Post-order Traversal − Traverses a tree in a post-order manner.
Defining a Node
Define a node that stores some data, and references to its left and right child nodes.
struct node {
int data;
struct node *leftChild;
struct node *rightChild;
};
Search Operation
Whenever an element is to be searched, start searching from the root node. Then if the data is
less than the key value, search for the element in the left subtree. Otherwise, search for the
element in the right subtree. Follow the same algorithm for each node.
Algorithm
1. START
2. Check whether the tree is empty or not
3. If the tree is empty, search is not possible
4. Otherwise, first search the root of the tree.
5. If the key does not match with the value in the root, search its subtrees.
6. If the value of the key is less than the root value, search the left subtree
7. If the value of the key is greater than the root value, search the right subtree.
8. If the key is not found in the tree, return unsuccessful search.
9. END

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 path is the same and that there are no adjacent
red nodes. Balanced Binary Search trees are performance-wise good
as they provide O(log n) time for search, insert and delete.

A balanced binary tree is a binary tree that follows the 3


conditions:

The height of the left and right tree for any node does not
differ by more than 1.
 The left subtree of that node is also balanced.
 The right subtree of that node is also balanced.
A single node is always balanced. It is also referred to as a height-
balanced binary tree.
Example:
Balanced and Unbalanced Binary Tree

It is a type of binary tree in which the difference between the height


of the left and the right subtree for each node is either 0 or 1. In the
figure above, the root node having a value 0 is unbalanced with a
depth of 2 units.
Application of Balanced Binary Tree:
 AVL Trees
 Red Black Tree
 Balanced Binary Search Tree
Advantages of Balanced Binary Tree:
 Non Destructive updates are supported by a Balanced
Binary Tree with the same asymptotic effectiveness.
 Range queries and iteration in the right sequence are made
feasible by the balanced binary tree.

AVL Tree Data Structure


An AVL tree defined as a self-balancing Binary Search
Tree (BST) where the difference between heights of left and right
subtrees for any node cannot be more than one.
The difference between the heights of the left subtree and the right
subtree for any node is known as the balance factor of the node.
The AVL tree is named after its inventors, Georgy Adelson-Velsky
and Evgenii Landis, who published it in their 1962 paper “An
algorithm for the organization of information”.
Example of AVL Trees:

AVL tree

The above tree is AVL because the differences between the heights
of left and right subtrees for every node are less than or equal to 1.

Operations on an AVL Tree:

 Insertion
 Deletion
 Searching [

Rotating the subtrees in an AVL Tree:

An AVL tree may rotate in one of the following four ways to keep
itself balanced:
Left Rotation:
When a node is added into the right subtree of the right subtree, if
the tree gets out of balance, we do a single left rotation.
Left-Rotation in AVL tree

Right Rotation:
If a node is added to the left subtree of the left subtree, the AVL tree
may get out of balance, we do a single right rotation.

Right Rotation in AVL tree

Left-Right Rotation:
A left-right rotation is a combination in which first left rotation takes
place after that right rotation executes.
Left-Right Rotation in AVL tree

Right-Left Rotation:
A right-left rotation is a combination in which first right rotation
takes place after that left rotation executes.

Right-Left Rotation in AVL tree

Applications of AVL Tree:

1. It is used to index huge records in a database and also to


efficiently search in that.
2. For all types of in-memory collections, including sets and
dictionaries, AVL Trees are used.
3. Database applications, where insertions and deletions are
less common but frequent data lookups are necessary
4. Software that needs optimized search.
5. It is applied in corporate areas and storyline games.
Advantages of AVL Tree:

1. AVL trees can self-balance themselves.


2. It is surely not skewed.
3. It provides faster lookups than Red-Black Trees
4. Better searching time complexity compared to other trees
like binary tree.
5. Height cannot exceed log(N), where, N is the total number
of nodes in the tree.

Disadvantages of AVL Tree:

1. It is difficult to implement.
2. It has high constant factors for some of the operations.
3. Less used compared to Red-Black trees.
4. Due to its rather strict balance, AVL trees provide
complicated insertion and removal operations as more
rotations are performed.
5. Take more processing for balancing.

Introduction of B-Tree
 Read
 Discuss
 Courses
 Practice
 Video
The limitations of traditional binary search trees can be frustrating.
Meet the B-Tree, the multi-talented data structure that can handle
massive amounts of data with ease. When it comes to storing and
searching large amounts of data, traditional binary search trees can
become impractical due to their poor performance and high memory
usage. B-Trees, also known as B-Tree or Balanced Tree, are a type of
self-balancing tree that was specifically designed to overcome these
limitations.
Unlike traditional binary search trees, B-Trees are characterized by
the large number of keys that they can store in a single node, which
is why they are also known as “large key” trees. Each node in a B-
Tree can contain multiple keys, which allows the tree to have a
larger branching factor and thus a shallower height. This shallow
height leads to less disk I/O, which results in faster search and
insertion operations. B-Trees are particularly well suited for storage
systems that have slow, bulky data access such as hard drives, flash
memory, and CD-ROMs.
B-Trees maintain balance by ensuring that each node has a
minimum number of keys, so the tree is always balanced. This
balance guarantees that the time complexity for operations such as
insertion, deletion, and searching is always O(log n), regardless of
the initial shape of the tree.

Time Complexity of B-Tree:

Algorith
Sr. No. m Time Complexity

1. Search O(log n)

2. Insert O(log n)

3. Delete O(log n)

Note: “n” is the total number of elements in the B-tree


Properties of B-Tree:
 All leaves are at the same level.
 B-Tree is defined by the term minimum degree ‘t‘. The
value of ‘t‘ depends upon disk block size.
 Every node except the root must contain at least t-1 keys.
The root may contain a minimum of 1 key.
 All nodes (including root) may contain at most (2*t – 1)
keys.
 Number of children of a node is equal to the number of keys
in it plus 1.
 All keys of a node are sorted in increasing order. The child
between two keys k1 and k2 contains all keys in the range
from k1 and k2.
 B-Tree grows and shrinks from the root which is unlike
Binary Search Tree. Binary Search Trees grow downward
and also shrink from downward.
 Like other balanced Binary Search Trees, the time
complexity to search, insert and delete is O(log n).
 Insertion of a Node in B-Tree happens only at Leaf Node.
Following is an example of a B-Tree of minimum order 5
Note: that in practical B-Trees, the value of the minimum order is
much more than 5.
We can see in the above diagram that all the leaf nodes are at the
same level and all non-leafs have no empty sub-tree and have keys
one less than the number of their children.

Interesting Facts about B-Trees:


 The minimum height of the B-Tree that can exist with n
number of nodes and m is the maximum number of children
of a node can have is:
 The maximum height of the B-Tree that can exist with n
number of nodes and t is the minimum number of children

that a non-root node can have is:


and

Traversal in B-Tree:
Traversal is also similar to Inorder traversal of Binary Tree. We start
from the leftmost child, recursively print the leftmost child, then
repeat the same process for the remaining children and keys. In the
end, recursively print the rightmost child.

Search Operation in B-Tree:


Search is similar to the search in Binary Search Tree. Let the key to
be searched is k.
 Start from the root and recursively traverse down.
 For every visited non-leaf node,
 If the node has the key, we simply return the node.
 Otherwise, we recur down to the appropriate child
(The child which is just before the first greater key)
of the node.
 If we reach a leaf node and don’t find k in the leaf node,
then return NULL.
Searching a B-Tree is similar to searching a binary tree. The
algorithm is similar and goes with recursion. At each level, the
search is optimized as if the key value is not present in the range of
the parent then the key is present in another branch. As these
values limit the search they are also known as limiting values or
separation values. If we reach a leaf node and don’t find the desired
key then it will display NULL.

Introduction to Splay tree data structure


Splay tree is a self-adjusting binary search tree data structure, which
means that the tree structure is adjusted dynamically based on the
accessed or inserted elements. In other words, the tree
automatically reorganizes itself so that frequently accessed or
inserted elements become closer to the root node.
1. The splay tree was first introduced by Daniel Dominic
Sleator and Robert Endre Tarjan in 1985. It has a simple and
efficient implementation that allows it to perform search,
insertion, and deletion operations in O(log n) amortized time
complexity, where n is the number of elements in the tree.
2. The basic idea behind splay trees is to bring the most
recently accessed or inserted element to the root of the tree
by performing a sequence of tree rotations, called splaying.
Splaying is a process of restructuring the tree by making the
most recently accessed or inserted element the new root
and gradually moving the remaining nodes closer to the
root.
3. Splay trees are highly efficient in practice due to their self-
adjusting nature, which reduces the overall access time for
frequently accessed elements. This makes them a good
choice for applications that require fast and dynamic data
structures, such as caching systems, data compression, and
network routing algorithms.
4. However, the main disadvantage of splay trees is that they
do not guarantee a balanced tree structure, which may lead
to performance degradation in worst-case scenarios. Also,
splay trees are not suitable for applications that require
guaranteed worst-case performance, such as real-time
systems or safety-critical systems.

Operations on Heaps
The common operation involved using heaps are:

 Heapify → Process to rearrange the heap in order to


maintain heap-property.
 Find-max (or Find-min) → find a maximum item of
a max-heap, or a minimum item of a min-heap,
respectively.
 Insertion → Add a new item in the heap.
 Deletion → Delete an item from the heap.
 Extract Min-Max → Returning and deleting the
maximum or minimum element in max-heap and min-
heap respectively.

Binomial Heap

The main application of Binary Heap is as implement a priority


queue. Binomial Heap is an extension of Binary Heap that provides
faster union or merge operation with other operations provided by
Binary Heap.
A Binomial Heap is a collection of Binomial Trees
What is a Binomial Tree?
A Binomial Tree of order 0 has 1 node. A Binomial Tree of order k
can be constructed by taking two binomial trees of order k-1 and
making one the leftmost child of the other.
A Binomial Tree of order k the has following properties.
Operations of Binomial Heap:
The main operation in Binomial Heap is a union(), all other
operations mainly use this operation. The union() operation is to
combine two Binomial Heaps into one. Let us first discuss other
operations, we will discuss union later.
1. insert(H, k): Inserts a key ‘k’ to Binomial Heap ‘H’. This
operation first creates a Binomial Heap with a single key
‘k’, then calls union on H and the new Binomial heap.
2. getting(H): A simple way to get in() is to traverse the list of
the roots of Binomial Trees and return the minimum key.
This implementation requires O(Logn) time. It can be
optimized to O(1) by maintaining a pointer to the minimum
key root.
3. extracting(H): This operation also uses a union(). We first
call getMin() to find the minimum key Binomial Tree, then
we remove the node and create a new Binomial Heap by
connecting all subtrees of the removed minimum node.
Finally, we call union() on H and the newly created Binomial
Heap. This operation requires O(Logn) time.
4. delete(H): Like Binary Heap, the delete operation first
reduces the key to minus infinite, then calls extracting().

5. decrease key(H): decrease key() is also similar to Binary


Heap. We compare the decreased key with its parent and if
the parent’s key is more, we swap keys and recur for the
parent. We stop when we either reach a node whose parent
has a smaller key or we hit the root node. The time
complexity of the decrease key() is O(Logn).
Union operation in Binomial Heap:
Given two Binomial Heaps H1 and H2, union(H1, H2)
creates a single Binomial Heap.
6. The first step is to simply merge the two Heaps in non-
decreasing order of degrees. In the following diagram,
figure(b) shows the result after merging.
7. After the simple merge, we need to make sure that there is
at most one Binomial Tree of any order. To do this, we
need to combine Binomial Trees of the same order. We
traverse the list of merged roots, we keep track of three-
pointers, prev, x, and next-x. There can be the following 4
cases when we traverse the list of roots.

Fibonacci Heap | Set 1 (Introduction)


INTRODUCTION:

1. A Fibonacci heap is a data structure used for implementing


priority queues. It is a type of heap data structure, but with
several improvements over the traditional binary heap and
binomial heap data structures.
2. The key advantage of a Fibonacci heap over other heap data
structures is its fast amortized running time for operations
such as insert, merge and extract-min, making it one of the
most efficient data structures for these operations. The
running time of these operations in a Fibonacci heap is O(1)
for insert, O(log n) for extract-min and O(1) amortized for
merge.
3. A Fibonacci heap is a collection of trees, where each tree is
a heap-ordered multi-tree, meaning that each tree has a
single root node with its children arranged in a heap-ordered
manner. The trees in a Fibonacci heap are organized in such
a way that the root node with the smallest key is always at
the front of the list of trees.
4. In a Fibonacci heap, when a new element is inserted, it is
added as a singleton tree. When two heaps are merged, the
root list of one heap is simply appended to the root list of
the other heap. When the extract-min operation is
performed, the tree with the minimum root node is removed
from the root list and its children are added to the root list.
5. One unique feature of a Fibonacci heap is the use of lazy
consolidation, which is a technique for improving the
efficiency of the merge operation. In lazy consolidation, the
merging of trees is postponed until it is necessary, rather
than performed immediately. This allows for the merging of
trees to be performed more efficiently in batches, rather
than one at a time.

HashSet in Java
Java HashSet class implements the Set interface, backed by a hash
table which is actually a HashMap instance. No guarantee is made
as to the iteration order of the hash sets which means that the class
does not guarantee the constant order of elements over time. This
class permits the null element. The class also offers constant time
performance for the basic operations like add, remove, contains,
and size assuming the hash function disperses the elements
properly among the buckets, which we shall see further in the
article.
Java HashSet Features
A few important features of HashSet are mentioned below:
 Implements Set Interface.
 The underlying data structure for HashSet is Hashtable.
 As it implements the Set Interface, duplicate values are not
allowed.
 Objects that you insert in HashSet are not guaranteed to be
inserted in the same order. Objects are inserted based on
their hash code.
 NULL elements are allowed in HashSet.
 HashSet also
implements Serializable and Cloneable interfaces.

What is Searching Algorithm?

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:
1. Sequential Search: In this, the list or array is traversed sequentially
and every element is checked. For example: Linear Search.
Linear Search to find the element “20” in a given list of
numbers

Linear-Search

2. Interval Search: These algorithms are specifically designed for


searching in sorted data-structures. These type of searching
algorithms are much more efficient than Linear Search as they
repeatedly target the center of the search structure and divide the
search space in half. For Example: Binary Search.
Binary Search to find the element “23” in a given list of
numbers

What is Sorting?

A Sorting Algorithm is used to rearrange a given array or list of elements


according to a comparison operator on the elements. The comparison
operator is used to decide the new order of elements in the respective data
structure.
For Example: The below list of characters is sorted in increasing order of
their ASCII values. That is, the character with a lesser ASCII value will be
placed first than the character with a higher ASCII value.
Hashing Data Structure

Learn more about Hashing in DSA Self Paced Course


Practice Problems on Hashing
Top Quizzes on Hashing
What is Hashing?
Hashing is a technique or process of mapping keys, and values into the
hash table by using a hash function. It is done for faster access to
elements. The efficiency of mapping depends on the efficiency of the hash
function used.
Let a hash function H(x) maps the value x at the index x%10 in an Array.
For example if the list of values is [11,12,13,14,15] it will be stored at
positions {1,2,3,4,5} in the array or Hash table respectively.
Indexed Sequential Search
In this searching method, first of all, an index file is created, that
contains some specific group or division of required record when the
index is obtained, then the partial indexing takes less time cause it
is located in a specified group.
Note: When the user makes a request for specific records it will find
that index group first where that specific record is recorded.

Characteristics of Indexed Sequential Search:

 In Indexed Sequential Search a sorted index is set aside in


addition to the array.
 Each element in the index points to a block of elements in
the array or another expanded index.
 The index is searched 1st then the array and guides the
search in the array.
Note: Indexed Sequential Search actually does the indexing
multiple time, like creating the index of an index.

Explanation by diagram “Indexed Sequential Search”:

Interpolation Search
Given a sorted array of n uniformly distributed values arr[], write a
function to search for a particular element x in the array.
Linear Search finds the element in O(n) time, Jump Search takes O(√
n) time and Binary Search takes O(log n) time.
The Interpolation Search is an improvement over Binary Search for
instances, where the values in a sorted array are uniformly
distributed. Interpolation constructs new data points within the
range of a discrete set of known data points. Binary Search always
goes to the middle element to check. On the other hand,
interpolation search may go to different locations according to the
value of the key being searched. For example, if the value of the key
is closer to the last element, interpolation search is likely to start
search toward the end side.
To find the position to be searched, it uses the following formula.

Hashing - Open Addressing for Collision


Handling
We have talked about

o A well-known search method is hashing.


o When the new key's hash value matches an already-occupied
bucket in the hash table, there is a collision.

Open Addressing for Collision Handling


Similar to separate chaining, open addressing is a technique for dealing
with collisions. In Open Addressing, the hash table alone houses all of the
elements. The size of the table must therefore always be more than or
equal to the total number of keys at all times (Note that we can increase
table size by copying old data if needed). This strategy is often referred to
as closed hashing. The foundation of this entire process is probing. We will
comprehend several forms of probing later.

o Insert (k): Continue probing until a slot is left open. Put k in the
first empty spot you find.
o Search (k): Continue probing until either an empty slot is found or
the slot's key no longer equals k.
o Delete (k): An intriguing delete procedure. The search can fail if we
just remove a key. Therefore, deleted key slots are specifically
noted as "deleted."

Separate Chaining:
The idea behind separate chaining is to implement the array as a
linked list called a chain. Separate chaining is one of the most
popular and commonly used techniques in order to handle
collisions.
The linked list data structure is used to implement this technique.
So what happens is, when multiple elements are hashed into the
same slot index, then these elements are inserted into a singly-
linked list which is known as a chain.
Here, all those elements that hash into the same slot index are
inserted into a linked list. Now, we can use a key K to search in the
linked list by just linearly traversing. If the intrinsic key for any
entry is equal to K then it means that we have found our entry. If
we have reached the end of the linked list and yet we haven’t
found our entry then it means that the entry does not exist. Hence,
the conclusion is that in separate chaining, if two different
elements have the same hash value then we store both the
elements in the same linked list one after the other.
Example: Let us consider a simple hash function as “key mod 7”
and a sequence of keys as 50, 700, 76, 85, 92, 73, 101
You can refer to the following link in order to understand how to
implement separate chaining with C++.
C++ program for hashing with chaining
Advantages:
 Simple to implement.
 Hash table never fills up, we can always add more
elements to the chain.
 Less sensitive to the hash function or load factors.
 It is mostly used when it is unknown how many and how fre

collision resolution

The purpose of collision resolution during insertion is to locate


an open location in the hash table when the record’s home position
is already taken. Any collision resolution technique may be thought
of as creating a series of hash table slots that may or may not
contain the record. The key will be in its home position in the first
position in the sequence. The collision resolution policy shifts to the
following location in the sequence if the home position is already
occupied. Another slot needs to be sought if this is also taken, and
so on. The probe sequence is a collection of slots that is produced
by a probe function that we will refer to as p. This is how insertion
operates.
Bubble Sort – Data Structure and
Algorithm Tutorials
Bubble Sort is the 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.
Bubble Sort Algorithm
In this algorithm,
 traverse from left and compare adjacent elements and the
higher one is placed at right side.
 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.
Recommended Problem

Bubble Sort

How does Bubble Sort Work?


Let us understand the working of bubble sort with the help of the
following illustration:
Input: arr[] = {6, 3, 0, 5}
First Pass:
The largest element is placed in its correct position, i.e., the end of
the array

Data Structure and Algorithms Selection


Sort
Selection sort is a simple sorting algorithm. This sorting algorithm is an in-
place comparison-based algorithm in which the list is divided into two
parts, the sorted part at the left end and the unsorted part at the right
end. Initially, the sorted part is empty and the unsorted part is the entire
list.
The smallest element is selected from the unsorted array and swapped
with the leftmost element, and that element becomes a part of the sorted
array. This process continues moving unsorted array boundary by one
element to the right.
This algorithm is not suitable for large data sets as its average and worst
case complexities are of Ο(n2), where n is the number of items.

How Selection Sort Works?


Consider the following depicted array as an example.

For the first position in the sorted list, the whole list is scanned
sequentially. The first position where 14 is stored presently, we search the
whole list and find that 10 is the lowest value.

So we replace 14 with 10. After one iteration 10, which happens to be the
minimum value in the list, appears in the first position of the sorted list.

For the second position, where 33 is residing, we start scanning the rest of
the list in a linear manner.

We find that 14 is the second lowest value in the list and it should appear
at the second place. We swap these values.

After two iterations, two least values are positioned at the beginning in a
sorted manner.

The same process is applied to the rest of the items in the array.
Following is a pictorial depiction of the entire sorting process −
Data Structure and Algorithms Insertion
Sort
This is an in-place comparison-based sorting algorithm. Here, a sub-list is
maintained which is always sorted. For example, the lower part of an
array is maintained to be sorted. An element which is to be 'insert'ed in
this sorted sub-list, has to find its appropriate place and then it has to be
inserted there. Hence the name, insertion sort.
The array is searched sequentially and unsorted items are moved and
inserted into the sorted sub-list (in the same array). This algorithm is not
suitable for large data sets as its average and worst case complexity are
of Ο(n2), where n is the number of items.

How Insertion Sort Works?


We take an unsorted array for our example.
Insertion sort compares the first two elements.

It finds that both 14 and 33 are already in ascending order. For now, 14 is
in sorted sub-list.

Insertion sort moves ahead and compares 33 with 27.

And finds that 33 is not in the correct position.

It swaps 33 with 27. It also checks with all the elements of sorted sub-list.
Here we see that the sorted sub-list has only one element 14, and 27 is
greater than 14. Hence, the sorted sub-list remains sorted after swapping.

By now we have 14 and 27 in the sorted sub-list. Next, it compares 33


with 10.

These values are not in a sorted order.

So we swap them.

However, swapping makes 27 and 10 unsorted.


Hence, we swap them too.

Again we find 14 and 10 in an unsorted order.

We swap them again. By the end of third iteration, we have a sorted sub-
list of 4 items.

This process goes on until all the unsorted values are covered in a sorted
sub-list. Now we shall see some programming aspects of insertion sort.

Data Structure and Algorithms - Quick Sort


Previous Page
Next Page
Quick sort is a highly efficient sorting algorithm and is based on
partitioning of array of data into smaller arrays. A large array is
partitioned into two arrays one of which holds values smaller than the
specified value, say pivot, based on which the partition is made and
another array holds values greater than the pivot value.
Quicksort partitions an array and then calls itself recursively twice to sort
the two resulting subarrays. This algorithm is quite efficient for large-sized
data sets as its average and worst-case complexity are O(n 2), respectively.

Partition in Quick Sort


Following animated representation explains how to find the pivot value in
an array.
The pivot value divides the list into two parts. And recursively, we find the
pivot for each sub-lists until all lists contains only one element.

Quick Sort Pivot Algorithm


Based on our understanding of partitioning in quick sort, we will now try to
write an algorithm for it, which is as follows.
Step 1 − Choose the highest index value has pivot
Step 2 − Take two variables to point left and right of the list excluding pivot
Step 3 − left points to the low index
Step 4 − right points to the high
Step 5 − while value at left is less than pivot move right
Step 6 − while value at right is greater than pivot move left
Step 7 − if both step 5 and step 6 does not match swap left and right
Step 8 − if left ≥ right, the point where they met is new pivot
Data Structures - Merge Sort Algorithm
Previous Page
Next Page
Merge sort is a sorting technique based on divide and conquer technique.
With worst-case time complexity being Ο(n log n), it is one of the most
respected algorithms.
Merge sort first divides the array into equal halves and then combines
them in a sorted manner.

How Merge Sort Works?


To understand merge sort, we take an unsorted array as the following −

We know that merge sort first divides the whole array iteratively into
equal halves unless the atomic values are achieved. We see here that an
array of 8 items is divided into two arrays of size 4.
This does not change the sequence of appearance of items in the original.
Now we divide these two arrays into halves.

We further divide these arrays and we achieve atomic value which can no
more be divided.

Now, we combine them in exactly the same manner as they were broken
down. Please note the color codes given to these lists.
We first compare the element for each list and then combine them into
another list in a sorted manner. We see that 14 and 33 are in sorted
positions. We compare 27 and 10 and in the target list of 2 values we put
10 first, followed by 27. We change the order of 19 and 35 whereas 42
and 44 are placed sequentially.

In the next iteration of the combining phase, we compare lists of two data
values, and merge them into a list of found data values placing all in a
sorted order.

After the final merging, the list should look like this −

Now we should learn some programming aspects of merge sorting.

Data Structure and Algorithms - Shell Sort


Previous Page
Next Page
Shell sort is a highly efficient sorting algorithm and is based on insertion
sort algorithm. This algorithm avoids large shifts as in case of insertion
sort, if the smaller value is to the far right and has to be moved to the far
left.
This algorithm uses insertion sort on a widely spread elements, first to
sort them and then sorts the less widely spaced elements. This spacing is
termed as interval. This interval is calculated based on Knuth's formula
as −

Knuth's Formula
h=h*3+1
where −
h is interval with initial value 1
This algorithm is quite efficient for medium-sized data sets as its average
and worst-case complexity of this algorithm depends on the gap sequence
the best known is Ο(n), where n is the number of items. And the worst
case space complexity is O(n).

How Shell Sort Works?


Let us consider the following example to have an idea of how shell sort
works. We take the same array we have used in our previous examples.
For our example and ease of understanding, we take the interval of 4.
Make a virtual sub-list of all values located at the interval of 4 positions.
Here these values are {35, 14}, {33, 19}, {42, 27} and {10, 44}

We compare values in each sub-list and swap them (if necessary) in the
original array. After this step, the new array should look like this −
Then, we take interval of 1 and this gap generates two sub-lists - {14, 27,
35, 42}, {19, 10, 33, 44}

We compare and swap the values, if required, in the original array. After
this step, the array should look like this −

Finally, we sort the rest of the array using interval of value 1. Shell sort
uses insertion sort to sort the array.
Following is the step-by-step depiction −
Tree Sort
 Video

Tree sort is a sorting algorithm that is based on Binary Search


Tree data structure. It first creates a binary search tree from the
elements of the input list or array and then performs an in-order
traversal on the created binary search tree to get the elements in
sorted order.
Algorithm:
 Step 1: Take the elements input in an array.
 Step 2: Create a Binary search tree by inserting data items
from the array into the binary search tree.
 Step 3: Perform in-order traversal on the tree to get the
elements in sorted o

You might also like