DS Notes-II Sem-Vins
DS Notes-II Sem-Vins
Data Structures
(AS PER SEP SYLLABUS)
Prepared By
Ms. Vinanthi S
Assistant Professor,
Department of Computer Science,
Hindustan College, Mysuru
Data Structures
Course Outcomes :
Syllabus :
UNIT – I
Introduction: Data Structure Definition, Basic Terminology and Concepts,
Importance of Data Structures in Programming. Classification of Data Structures.
Primitive Data Structures, Non-Primitive Data Structures.
Stack: Definition, Memory Representation, Algorithms for Stack Operations
(Push,
Pop), Applications of Stack.
UNIT – II
Queue: Definition, Memory Representation, Linear Queue, Circular Queue,
Enqueue, Dequeue. Applications of Queue.
Linked Lists: Definition, Types.
Singly Linked List: Implementation, Insertion [At the Beginning], Deletion [At the
End]. Doubly Linked List: Memory Representation of Singly Linked List and Doubly
Linked Lists. Applications of Linked List.
UNIT – III
Tree: Definition, Memory Representation Using Array and Linked List.
Binary Tree: Definition, Traversal Algorithms [Pre-Order, In-Order, Post-
Order], Construction of Tree from In-Order and Pre-Order, In-Order and Post-
Order.
Binary Search Trees: Insertion of a Node, Deletion of A Node.
Advanced Tree Structures AVL And B-Trees: Definition and Applications.
Data Structures
UNIT – IV
Graph: Definition, Memory Representation of Graph. Adjacency Matrix, Adjacency
List. Graph Traversal Algorithms: Breadth-First Search (BFS), Depth-First Search
Hours (DFS). Sorting Techniques: Bubble Sort, Selection Sort [Algorithm, Time &
Space Complexity]. Searching Techniques: Linear And Binary Search Sort
[Algorithm, Time & Space Complexity].
Heap: Heap Operations and Applications.
Text Books :
1. Data Structures Through C++ (4th Edition) Yashvant Kanetkar.
2. Data Structures and Algorithm Analysis in C++" by Mark Allen Weiss.
3. Data structure and Algorithms using C++ by Sachi Nandan Mohanty, Pabitra
Kumar Tripathy.
4. Data Structures and Algorithms in C++, Second Edition by Adam Drozdek.
Data Structures
DATA STRUCTURES
UNIT – 1
Data Structure Definition : A data structure is a specialized format for organizing,
processing, storing and retrieving (getting back) the data so that it can be accessed and used
efficiently. Data structures define the relationships between data and the operations that
can be performed on it.
Example: Array, Linked List, Stack queue.
A data structure is a Specific way to store and organize data in a computer's memory so
that these data can be used efficiently later.
Or
The data structure is basically a technique of organizing and storing of different types of
data items in computer memory. It is considered as not only the storing of data elements
but also the maintaining of the logical relationship existing between individual data
elements. The Data structure can also be defined as a mathematical or logical model,
which relates to a particular organization of different data elements.
4. Pointer: A pointer is a variable that holds the memory address of another variable or
element. Pointers are particularly useful in linked lists and trees for navigating
through elements.
Data Structures
6. Link: A link is a reference that connects nodes in linked data structures like linked lists
or trees.
7. Head: The head is the first node in a linked list. It’s the starting point of traversal in a
list.
8. Tail: The tail is the last node in a linked list, or the last element in a data structure like
a queue.
9. Stack: A stack is a data structure that follows the Last In, First Out (LIFO) principle.
Elements are added (pushed) and removed (popped) from the top of the stack.
10. Queue: A queue is a data structure that follows the First In, First Out (FIFO)
principle. Elements are added (enqueued) at the rear and removed (dequeued) from
the front.
11. Push: In a stack, push refers to adding an element to the top of the stack.
12. Pop: In a stack, pop refers to removing the element from the top of the stack.
13. Enqueue: In a queue, enqueue refers to adding an element to the rear of the queue.
14. Dequeue: In a queue, dequeue refers to removing an element from the front of the
queue.
15. Linked List: A linked list is a linear data structure where each element (node) contains
data and a reference (link) to the next node. It allows dynamic memory allocation and
efficient insertion and deletion.
16. Singly Linked List: A Singly Linked List (SLL) is a linear data structure where
each element, called a node, contains two parts:
Data: The value stored in the node.
Next: A pointer to the next node in the sequence.
Nodes are connected sequentially, forming a chain, with the last node pointing to Null.
17. Doubly Linked List: A doubly linked list is a linked list in which each node
contains two links: one pointing to the next node and one pointing to the previous
node.
Data Structures
18. Traversal: Traversal refers to the process of visiting each element in a data
structure. Common types of traversal include:
19. Tree: A tree is a hierarchical data structure consisting of nodes connected by edges. The
topmost node is called the root, and it branches out into sub-nodes.
20. Binary Tree: A binary tree is a tree where each node has at most two children,
commonly referred to as the left child and right child.
21. Leaf : A leaf node is a node in a tree that does not have any children (i.e., it has no
further branches).
22. Root: The root is the topmost node in a tree from which all other nodes descend.
23. Depth of a Node: The depth of a node is the number of edges from the root to that
node.
24. Height of a Tree: The height of a tree is the length of the longest path from the root
to a leaf node.
25. Binary Search Tree: A Binary Search Tree (BST) is a binary tree data structure in
which each node has the following properties:
Left Subtree: Contains only nodes with values less than the parent node.
Right Subtree: Contains only nodes with values greater than the parent node.
Left Subtree: Contains only nodes with values less than the parent node.
Right Subtree: Contains only nodes with values greater than the parent node.
26. B-tree: A B-tree is a self-balancing tree data structure used for indexing large
amounts of data, particularly in databases and file systems. It maintains sorted data
Data Structures
27. AVL Tree: An AVL tree is a self-balancing binary search tree in which the
difference in height between the left and right subtrees of any node is at most one.
If the balance factor becomes greater than one, rotations are used to restore balance.
28. Graph: A graph is a collection of nodes (vertices) and edges (connections between
nodes). Graphs can be directed (edges have a direction) or undirected (edges have
no direction).
29. Degree of a Node: The degree of a node in a graph is the number of edges
connected to the node. In a directed graph, the in-degree is the number of incoming
edges, and the out-degree is the number of outgoing edges.
30. Adjacency: In a graph, adjacency refers to the relationship between two vertices that
are connected by an edge.
29. Adjacency List: An adjacency list is a collection of lists or arrays used to represent
a graph, where each list corresponds to a vertex and contains a list of adjacent
vertices.
30. Sorting : Sorting is the process of arranging the elements in a specific order, typically
either in ascending or descending order. Sorting techniques include :
Bubble Sort
Selection Sort
Insertion Sort
Merge Sort
Quick Sort
Heap Sort
32. Heap: A heap is a specialized tree-based data structure that satisfies the heap
property:
In a max heap, the value of the parent node is greater than or equal to the values of its
children; in a min heap, the value of the parent node is less than or equal to the
values of its children.
Eg: bool
isTrue
= true;
bool
isFals
e=
false;
In a linear data structure, the data elements connect to each other sequentially (in a
Data Structures
Stack :A stack is a linear data structure where elements are stored in the LIFO
(Last In First Out) principle where the last element inserted would be the first
element to be deleted.
Eg: struct Stack
{
int
items[
MAX_
SIZE];
int top;
};
Queue :A queue is a linear data structure where elements are stored in the FIFO (First
In First Out) principle where the first element inserted would be the first element to be
accessed.
Eg: struct Queue
{
int items[MAX_SIZE]; int front, rear;
};
Linked List :A linked list consists of nodes where each node contains a data field and
a reference(link) to the next node in the list.
Eg: struct Node
{
int data;
struct Node* next;
Data Structures
};
2)Non-Linear data structure:In a non-linear data structure, the data elements connect to each
other hierarchically. Thus, they are presented at various levels.
Graphs : A graph is a collection of two sets V and E where V is a finite set of vertices
and E is a finite set of edges.
Between Data (each holds a single value). (e.g., linked lists, trees).
2. Examples Array, Linked List, Stack, Queue. Tree, Graph, Hash Table.
necessarily in
sequence.
Requires special
Can be traversed linearly in one or
5.Traversal traversal techniques
both directions.
like DFS (Depth-First
Search) or BFS
(Breadth-First Search).
Suitable for tasks requiring simple data Suitable for complex data
9. Usage
management (e.g., queues in CP U relationships (e.g., social
scheduling) networks, file systems).
ARRAYS
Types of Arrays
One-dimensional Array
Two-dimensional Array
Multi-dimensional Array
Basic operations on one-dimensional arrays
The following operations are performed on arrays:
Traversing: Accessing each element of the array exactly once to do some
operation.
Searching: Finding the location of an element in the array.
Sorting: Arranging the elements of the array in some order. Insertion: Inserting
an element into the array.
Deletion: Removing an element from the array.
Merging: Combining one or more arrays to form a single array.
Data Structures
STACK :
Stack Definition : A stack is a linear data structure that follows the Last In, First Out
(LIFO) principle, meaning the last element added to the stack is the first one to be
removed. Insertion and Deletion of elements takes place at the same end, called the top of
the stack.
Example: Tower of Hanoi game, Stack of plates, A deck of cards.
The image illustrates the concept of a Stack in computer science, a data structure
that operates on the principle of Last In, First Out (LIFO).
1. Stack Structure: A stack is represented as a vertical collection of elements where
operations can only be performed at one end, called the top.
2. LIFO Principle: The phrase "Last In, First Out" (LIFO) means that the last element
added (pushed) to the stack will be the first to be removed (popped).
3. Push Operation: Push is the operation of adding an element to the top of the stack.
For example, in the image, 6 is the most recently pushed element and is now at the
top of the stack.
4. Pop Operation: Pop is the operation of removing the top element from the stack. In
the current state, if a pop operation is performed, the number 6 will be removed.
5. Top Pointer: The top indicates the element that is currently at the highest position in
the stack. It helps track where the next push or pop operation will occur.
Elements in the Stack: The stack contains elements 1, 2, 3, 4, 5, 6 from bottom to top.
The element 1 was the first to be pushed, making it the last to be popped.
Data Structures
PUSH OPERATION
Through this operation, we add new elements to our stack. The push( ) operation
goes through the following steps:
1. Before inserting a new element into the stack, check whether the stack is full or not.
2. If the stack is already full and we try to insert more elements into it, it will return an
overflow error.
3. If the stack is not full, increment the ‘top’ to point to the next empty space and insert the
new element there.
4. When the stack is empty, ‘top’ points to -1. Once we add a new element, the ‘top’
points to that element and has a value of 0.
5. The stack can accommodate elements as long as it does not reach its maximum
capacity .
pointer ]
TOP TOP + 1
Step 4: return
[ end if ]
OR
if (top == max - 1) // When stack is full // Here, max is the total stack capacity
{
print("Overflow");
}
else
{
top = top + 1; //
Incrementing top by 1
stack[top]
=item;//Inserting the
element
Data Structures
POP OPERATION
It helps in removing elements from the top of the stack. The steps involved in the
pop operation are:
2. If the stack is already empty, we cannot remove any element from it. So, we will return
an underflow error.
3. If the stack is not empty, remove the top most element of the stack.
Algorithm :
if (top = = -1) // If stack is empty
{
print("Underflow");
}
else
{
item = stack [top]; //
Deleting the element top =
top - 1; //
Decrementing top by 1
}
Data Structures
Applications of Stack:
4. Stacks are used to allocate and manage memory in some operating systems and
programming languages.
5. Maintains browser history for back and forward navigation in web browsers.
7. Solves recursive problems like the Tower of Hanoi using stack operations.
8. Implements undo and redo functionality in text editors and design software.
Data Structures
DATA STRUCTURES
UNIT – 2
QUEUE
A queue is a linear data structure that follows the FIFO (First In, First Out) principle. It has
two primary operations:
Types of Queue
1. Linear Queue: A simple queue where insertion happens at the rear and deletion at the
front. The queue becomes full when the rear reaches the last index, even if there are
vacant spaces at the front.
2. Dequeue (Deletion)
o Check if the queue is empty (front == -1 or front > rear).
void enqueue(int queue[], int Crear, int size, int value) { if (rear
== size - 1) {
queue[++rear] = value;
return;
}
Data Structures
cout << "Deleted: " << queue[front++] << endl;
1. Enqueue (Insertion)
o Check if the queue is full using the condition:
(rear + 1) % SIZE == front
o If not full, insert the new element at (rear + 1) % SIZE.
2. Dequeue (Deletion)
o Check if the queue is empty (front == -1).
o Remove the front element and move front to (front + 1) % SIZE.
o If the last element is removed, reset front and rear to -1.
Data Structures
void enqueue(int queue[], int Cfront, int Crear, int size, int value) { if ((rear +
1) % size == front) {
(rear + 1) % size;
queue[rear] = value;
void dequeue(int queue[], int Cfront, int Crear, int size) { if (front
== -1) {
return;
(front == rear) {
} else {
Applications of Queue
A queue is a data structure that follows the First-In-First-Out (FIFO) principle. It has
numerous applications in computing, networking, and real-world scenarios. Here are some
key applications:
Disk Scheduling: Disk I/O requests are handled using queues to optimize data
retrieval.
3. Network Applications
Packet Scheduling: Routers and switches use queues to manage data packet
transfers.
Call Handling in Call Centres: Incoming customer calls are queued and answered in
order.
LINKED LIST
A linked list is a linear data structure where each node contains:
1. Data
2. Pointer to the next node (Singly Linked List) or both next and previous nodes (Doubly
Linked List).
2. Doubly Linked List: Each node has two pointers, one to the next and one to the
previous node.
3. Circular Linked List: The last node connects to the first node.
2. Next Pointer: Holds the reference to the next node in the list.
Data Structures
data;
Node* next;
};
newNode->data = value;
newNode->next = head;
head = newNode;
(!head) return;
if (!head->next) {
NULL; return;
temp->next;
temp->next = NULL;
}
Data Structures
A Doubly Linked List (DLL) is a type of linked list in which each node contains three
components:
This bidirectional linkage allows traversal in both forward and backward directions,
enhancing flexibility over singly linked lists.
struct DNode {
int data;
DNode* prev;
DNode* next;
};
newNode->data = value;
Data Structures
ewNode->prev = NULL;
newNode->next = head;
head = newNode;
(!head) return;
if (!head->next) {
NULL; return;
temp->prev->next = NULL;
delete temp;
A Circular Linked List is a variation of the linked list where the last node points back to the
first node, forming a circle. This structure allows for continuous traversal without a defined
beginning or end.
Graphical Representation:
1. Circular Singly Linked List: In this type, each node contains data and a reference to
the next node. The last node's reference points back to the first node.
2. Circular Doubly Linked List: Here, each node has three components:
o Data: The value stored in the node.
Stacks and Queues: Implemented using linked lists for efficient dynamic storage.
Graphs: Adjacency lists use linked lists to represent graph edges.
3. Undo/Redo Functionality
Text Editors & Software Applications: Maintain a history of actions using a doubly
linked list for undo and redo operations.
7. File Systems
Linked allocation in file systems stores file data across non-contiguous blocks to
minimize fragmentation.
8. Polynomial Arithmetic
Linked Lists store and process polynomials efficiently for mathematical
computations.
Linked Lists store incoming and outgoing packets efficiently in network routers.
Data Structures
DATA STRUCTURES
UNIT – 3
TREE
A tree is a non-linear data structure that consists of nodes connected by edges. It follows
a hierarchical structure with a designated root node, and each node can have child
nodes.
1. Nodes:
2. Edges:
o The connection between two nodes is called an edge.
Edges:
A→B
A→C
B→D
B→E
C→F
o Trees (especially binary trees) can be stored in arrays using level order
traversal.
Data
BINARY TREE
A Binary Tree is a tree where each node has at most two children: left and right.
Traversal Algorithms
1. Pre-Order (Root → Left → Right): [Preorder: 1 → 2 → 3 → 4 → 5 → 6 → 7]
o Visit root.
o Visit root.
o Visit root.
o Locate this root in the in-order sequence (left side becomes left subtree,
right side becomes right subtree).
Full Binary Tree: Each node can have 0 or 2 child nodes in this binary tree. Only
one child node is not allowed in this type of binary tree. So, except for the leaf
node, all nodes will have 2 children.
Full Binary Tree: Each node can have 0 or 2 nodes. It seems like the Full Binary
Tree, but all the leaf elements are lean to the left subtree, whereas in the full binary
tree node can be in the right or left subtree.
Perfect Binary Tree: All the nodes must have 0 or 2 nodes, and all the leaf nodes
should be at the same level or height. The above example of a full binary tree
structure is not a Perfect Binary Tree because node 6 and node 1,2,3 are not in the
same height. But the example of the Complete Binary Tree is a perfect binary tree.
Degenerate Binary Tree: Every node can have only a single child. All the
operations like searching, inserting, and deleting take O(N) time.
Data Structures
Balanced Binary Tree: Here this binary tree, the height difference of left and
right subtree is at most 1. So, while adding or deleting a node, we need to
balance the tree’s height again. This type of Self-Balanced Binary Tree is called
the AVL tree.
Left subtree contains nodes with values less than the root.
Right subtree contains nodes with values greater than the root.
Operations
Binary trees support several fundamental operations, including insertion, deletion,
searching, and traversal:
1. Insertion
Insertion involves adding a new node to the binary tree. In a binary tree, a new node is
usually inserted at the first available position in level order to maintain the completeness
of the tree.
Example:
2. Deletion
Deletion involves removing a node from the binary tree. In a binary tree, the node to be
deleted is replaced by the deepest and rightmost node to maintain the tree's structure.
Example:
Let's delete the value 3 from the following binary tree:
3. Search
Searching involves finding a node with a given value in the binary tree. The search
operation can be implemented using any traversal method (in-order, pre-order, post-
order, or level-order).
Example:
Visit node 1
Visit node 2
Visit node 3
Visit node 4
4. Traversal
Traversal involves visiting all the nodes in the binary tree in a specific order. The main
traversal methods are in-order, pre-order, post-order, and level-order.
Example:
Consider the following binary tree:
Applications:
1. Database Indexing
2. Memory Management
3. File Systems
4. Network Routing
5. Priority Queues
6. Compiler Design
7. Geospatial Databases
B-TREES
A self-balancing search tree used for large data storage (e.g., databases, file
systems).
Applications:
o B-Trees are used in CAD systems to organize and search geometric data.
o B-Trees are also used in other areas such as natural language processing,
computer networks, and cryptography.
Data Structures
DATA STRUCTURES
UNIT – 4
GRAPH
A graph is a data structure consisting of a set of nodes (vertices) and edges that connect
pairs of nodes. Graphs can be directed (edges have a direction) or undirected (edges
have no direction). They can also be weighted (edges have weights) or unweighted.
1. Adjacency Matrix
Example
In the above examples, 1 represents an edge from row vertex to column vertex, and 0
represents no edge from row vertex to column vertex.
2. Adjacency List
Example
Let's see the following directed graph representation implemented using linked list:
Example:
Figure 3: Moving
Step 3: B is moved into the Visited, and then the
neighbors of B, E, and F, are placed in
the Queue. While A is a neighbor of C, it cannot
be added to the Queue as it is already in
the Visited.
Data Structures
Figure 4: Moving
Figure 5: Moving
Step 5: D is moved into the Visited, and
subsequently, the neighbors of D that are not
already in the Visited, namely H and I, are
placed in the Queue.
Figure 6: Moving
Step 6: We are at E now. It is important to note
that although E has a neighbor B, once it is
marked as Visited, B can no longer be added to
the Queue. Consequently, E can be moved
into the Visited category.
Figure 7: Moving
Step 7: Moving F into the Visited category in a
similar manner. The neighbor of F, which is B,
cannot be added to the Queue as it has already
been visited.
Figure 8: Moving
Step 8: Moving G into the Visited.
Figure G: Moving
In this instance, all nodes are depicted in red, indicating that the entire graph has been
traversed and the process has been finalized. The Visited path encompasses the sequence
A, B, C, D, E, F, G, H, and I, progressing from the upper level to the middle level, and
ultimately to the lower level.
The following code can be used to implement this process. It is important to note that in
an undirected graph, the key-value pairs are symmetric to each other. To be more precise, if
the value of key A contains B, then the value of key B must also contain A. I also
recommend experimenting with various graphs and initial points.
Algorithm:
The steps involved in the BFS algorithm to explore a graph are given as follows -
Step 2: Enqueue the starting node A and set its STATUS = 2 (waiting state)
Step 4: Dequeue a node N. Process it and set its STATUS = 3 (processed state).
Step 5: Enqueue all the neighbours of N that are in the ready state (whose STATUS = 1)
and set
their STATUS =
2 (waiting state)
[END OF
LOOP] Step 6:
EXIT
Data Structures
Step 1: Reviewing A.
Step 7:
Once again, the code is provided below. You can observe that BFS and DFS would
employ nearly identical code, with the only difference being the use of pop(0) for the
queue in BFS and pop(-1) for the stack in DFS.
Data Structures
Algorithm
Step 2: Push the starting node A on the stack and set its STATUS = 2 (waiting state)
Step 4: Pop the top node N. Process it and set its STATUS = 3 (processed state)
Step 5: Push on the stack all the neighbors of N that are in the ready state (whose STATUS
= 1) and set their STATUS = 2 (waiting state)
[END OF LOOP]
Step 6: EXIT
Definition BFS stands for Breadth First DFS stands for Depth First Search.
Search.
Data structure BFS uses a Queue to find the DFS uses a Stack to find the shortest path.
shortest path.
Source BFS is better when target is DFS is better when target is far from source.
closer to Source.
Suitability As BFS considers all DFS is more suitable for decision tree. As
fo neighbor so it is not suitable for with one decision, we need to traverse
r decision tree decision tree used in puzzle further to augment the decision. If we reach
games. the conclusion, we won.
Memory BFS requires more memory DFS requires less memory space.
space.
Tapping In BFS, there is no problem of In DFS, we may be trapped into infinite loops.
i trapping into finite loops.
n loops
Principle BFS is implemented using DFS is implemented using LIFO (Last In First
FIFO (First In First Out) Out) principle.
principle.
Data Structures
Sorting Techniques
1. Bubble Sort
Bubble sort is a simple sorting algorithm. The algorithm starts at the beginning of the data
set. It compares the first two elements, and if the first is greater than the second, it swaps
them. It continues doing this for each pair of adjacent elements to the end of the data set.
It then starts again with the first two elements, repeating until no swaps have occurred on
the last pass
[End of if]
[End for]
return list
2. Selection Sort
Selection sort is a simple sorting algorithm which finds the smallest element in
the array and exchanges it with the element in the first position. Then finds the
second smallest element and exchanges it with the element in the second position
and continues until the entire array is sorted.
Data Structures
Algorithm
Step 1 − Set MIN to location 0
Step 2 − Search the minimum element in the list
Step 3 − Swap with value at location MIN
Step 4 − Increment MIN to point to next element
Step 5 − Repeat until list is sorted
Now, let's see the time complexity of selection sort in best case, average case, and in
worst case. We will also see the space complexity of the selection sort.
1. Time Complexity
2. Space Complexity
Space O(1)
Complexity
Stable YES
o The space complexity of selection sort is O(1). It is because, in selection sort, an
extra variable is required for swapping.
Searching
Searching Techniques
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
Algorithm:
if arr[i] == target:
return i
return -1
2. Binary Search
Binary search looks for a particular item by comparing the middle most item of the
collection. If a match occurs, then the index of item is returned. If the middle item is
greater than the item, then the item is searched in the sub-array to the left of the middle
item. Otherwise, the item is searched for in the sub-array to the right of the middle item.
This process continues on the sub-array as well until the size of the subarray reduces to
zero.
Now we compare the value stored at location 4, with the value being searched, i.e. 31.
We find that the value at location 4 is 27, which is not a match. As the value is greater
than 27 and we have a sorted array, so we also know that the target value must be in the
upper portion of the array.
Data Structures
We change our low to mid + 1 and find the new mid value again.
low = mid + 1
mid = low + (high - low) / 2
Our new mid is 7 now. We compare the value stored at location 7 with our target value 31.
The value stored at location 7 is not a match, rather it is more than what we are looking for.
So, the value must be in the lower part from this location.
We compare the value stored at location 5 with our target value. We find that it is a match.
Now, let's see the time complexity of Binary search in the best case, average case, and
worst case. We will also see the space complexity of Binary search.
Data Structures
1. Time Complexity
o Best Case Complexity - In Binary search, best case occurs when the element to
search is found in first comparison, i.e., when the first middle element itself is the
element to be searched. The best-case time complexity of Binary search is O(1).
o Average Case Complexity - The average case time complexity of Binary
search is O(logn).
o Worst Case Complexity - In Binary search, the worst case occurs, when we have
to keep reducing the search space till it has only one element. The worst -case time
complexity of Binary search is O(logn).
2. Space Complexity
Heap Sort
Heapsort is a popular terminology that belongs to the heap data structure family. It
is a comparison-based sorting method based on Binary Heap data structure. In
heapsort, we generally prefer the heapify method to sort the elements.
To learn Heapsort in-depth, we need to learn about arrays and trees first.
The above picture shows the strong relationship between arrays and trees and how
we can sort an array in a form of heap.
Heap is a special data structure. It has a tree structure. A heap data should
follow some properties, then only we can consider it a genuine heap data
structure. The properties are:
When it comes to creating a heap tree( Min Heap or Max Heap), the heapify
method is most suitable, because it takes less time as compared to the other
methods like Insert key one by one in the given order.
This is the case of Max heap because in this above picture the parent node is
greater than the child node. And to perform the heapify method, we have to
compare the nodes and swap them if required.
How to heapify root element when its subtrees are already max heaps?
To maintain the max-heap property in a tree where both sub-trees are max-heaps,
we need to run heapify on the root element repeatedly until it is larger than its
children or it becomes a leaf node.
2. Swap: Extract the root element and we need to place the last element of the tree
(heap) at the vacant place.
4. Heapify: Heapify the root element again so that we have the highest element at
root.
5. The process is repeated until all the items of the list are sorted.
In the above picture, we are representing all the operations. And we are trying to
balance a tree by heapify method.
Data Structures
Algorithm
Applications of Heap
1. HeapSort(arr)
Priority Queues
2. BuildMaxHeap(arr)
Dijkstra’s Algorithm (Shortest
3. for i = length(arr) to 2 Path)
6. MaxHeapify(arr,1)
7. End
Now, let's see the time complexity of Heap sort in the best case, average case, and worst
case. We will also see the space complexity of Heapsort.
1. Time Complexity
Case Time Complexity
o Best Case Complexity - It occurs when there is no sorting required, i.e. the array
is already sorted. The best-case time complexity of heap sort is O(n logn).
o Average Case Complexity - It occurs when the array elements are in jumbled
order that is not properly ascending and not properly descending. The average
case time complexity of heap sort is O(n log n).
o Worst Case Complexity - It occurs when the array elements are required to be
sorted in reverse order. That means suppose you have to sort the array elements in
ascending order, but its elements are in descending order. The worst-case time
complexity of heap sort is O(n log n).
The time complexity of heap sort is O(n logn) in all three cases (best case, average case,
and worst case). The height of a complete binary tree having n elements is logn.
Stable N0
Data Structures
Applications of Heap
Priority Queues
Heap Sort
Scheduling Algorithms
2. It is also a type of in-place sorting algorithm. So, its space complexity is O(1).
3. In comparison to quick sort, heap sort has a better worst-case complexity. So, its
time complexity is O(log n). The best-case complexity is the same for both quick
sort and heap sort — O(log n).
5. The input data being completely or almost sorted doesn’t make the complexities
suffer.
6. The average-case complexity is the same as that of merge sort and quicksort.
There are some disadvantages to using heap sort. These are as follows.
1. It is not stable because the operations on the heap can change the relative order of
equal key items. It’s typically an unstable sorting algorithm.