Data Structure Notes 3to 5 Modules
Data Structure Notes 3to 5 Modules
1
MOUDLE -III
SORTING AND SEARCHING
SORTING-INTRODUCTION
1. Whai is sorting?
Types of sorting
1. Bubble Sort
2. Selection Sort
3. insertion sort
4. Merge sort
5. Quick sort
SELECTION SORT
In selection sort, the smallest value among the unsorted elements of the array is
selected in every pass and inserted to its appropriate position into the array.
First, find the smallest element of the array and place it on the first position. Then,
find the second smallest element of the array and place it on the second position.
The process continues until we get the sorted array. The array with n elements is
sorted by using n-1 pass of selection sort algorithm.
• In 1st pass, smallest element of the array is to be found along with its index
pos. then, swap A[0] and A[pos]. Thus A[0] is sorted, we now have n -1
elements which are tobe sorted.
• In 2nd pas, position pos of the smallest element present in the sub-array A[n-
1] is found. Then, swap, A[1] and A[pos]. Thus A[0] and A[1] are sorted, we
now left with n-2 unsorted elements.
• In n-1th pass, position pos of the smaller element between A[n-1] and A[n-2]
is to be found. Then, swap, A[pos] and A[n-1].
Example: Consider the following array with 6 elements. Sort the elements of the array by
using selection sort.
A = {10, 2, 3, 90, 43, 56}.
3
Sorted A = {2, 3, 10, 43, 56, 90}
Complexity
Complexity Best Average Case Worst Case
Case
Time Ω(n) θ(n2) o(n2)
Space o(1)
4
Algorithm
BUBBLE SORT
Bubble Sort: This sorting technique is also known as exchange sort, which arranges
values by iterating over the list several times and in each iteration the larger value gets
bubble up to the end of the list. This algorithm uses multiple passes and in each pass the
first and second data items are compared. if the first data item is bigger than the second,
then the two items are swapped. Next the items in second and third position are compared
and if the first one is larger than the second, then they are swapped, otherwise no change in
their order. This process continues for each successive pair of data items until all items are
sorted.
Bubble Sort Algorithm:
Step 1: Repeat Steps 2 and 3 for i=1 to 10
Step 2: Set j=1
Step 3: Repeat while j<=n
(A)
if a[i] < a[j] Then
interchange a[i] and a[j]
[End of if]
(B) Set j = j+1
[End of Inner Loop]
[End of Step 1 Outer Loop]
Step 4: Exit
5
5. Explain Insertion sort
INSERTION SORT
Procedure
The simple steps of achieving the insertion sort are listed as follows -
Step 1 - If the element is the first element, assume that it is already sorted.
6
Step3 - Now, compare the key with all elements in the sorted array.
Step 4 - If the element in the sorted array is smaller than the current element,
then move to the next element. Else, shift greater elements in the array
towards the right.
Example:
The steps to sort the values stored in the array in ascending order using Insertion sort are
given below:
7 33 20 11 6
7 33 20 11 6
7 20 33 11 6
Step 4: Then the fourth element 11 is compared with its previous elements. Since 11 is less
than 33 and 20 ; and greater than 7. So it is placed in between 7 and 20. For this the
elements 20 and 33 are shifted one position towards the right.
7 20 33 11 6
7 11 20 33 6
Step5: Finally the last element 6 is compared with all the elements preceding it. Since it is
smaller than all other elements, so they are shifted one position towards right and 6 is
inserted at the first position in the array. After this pass, the Array is sorted.
7 11 20 33 6
6 7 11 20 33
Step 6: Finally the sorted Array is as follows:
6 7 11 20 33
ALGORITHM:
7
void insertion_sort ( int A[ ] , int n)
{
for( int i = 0 ;i < n ; i++ )
{
/*storing current element whose left side is checked for its correct position .*/
int temp = A[ i ];
int j = i;
/* check whether the adjacent element in left side is greater or less than the current element. */
while( j > 0 && temp < A[ j -1])
{
// moving the left side element to one position forward.
A[ j ] = A[ j-1];
j= j - 1;
}
// moving current element to its correct position.
A[ j ] = temp;
}
}
Advantages of Insertion Sort:
• It is simple sorting algorithm, in which the elements are sorted by considering one
item at a time. The implementation is simple.
• It is efficient for smaller data set and for data set that has been substantially sorted
before.
• It does not change the relative order of elements with equal keys
• It reduces unnecessary travels through the array
• It requires constant amount of extra memory space.
Disadvantages:-
• It is less efficient on list containing more number of elements.
• As the number of elements increases the performance of program would be slow
QUICK SORT
The Quick Sort algorithm follows the principal of divide and Conquer. It first picks
up the partition element called ‘Pivot’, which divides the list into two sub lists such that all
the elements in the left sub list are smaller than pivot and all the elements in the right sub
list are greater than the pivot. The same process is applied on the left and right sub lists
separately. This process is repeated recursively until each sub list containing more than one
element.
The steps to sort the values stored in the array in the ascending order using Quick Sort are
given below.
8 33 6 21 4
Step 1: Initially the index ‘0’ in the list is chosen as Pivot and the index variable Beg and End
are initiated with index ‘0’ and (n-1) respectively.
8
Step 2: The scanning of the element starts from the end of the list.
A[Pivot]>A[End]
i.e; 8>4
so they are swapped.
Step 3: Now the scanning of the elements starts from the beginning of the list. Since
A[Pivot]>A[Beg]. So Beg is incremented by one and the list remains unchanged.
Step 4: The element A[Pivot] is smaller than A[Beg].So they are swapped.
Step 5: Again the list is scanned form right to left. Since A[Pivot] is smaller than A[End], so
the value of End is decreased by one and the list remains unchanged.
Step 6: Next the element A[Pivot] is smaller than A[End], the value of End is increased by
one. and the list remains unchanged.
9
Step 7: A[Pivot>>A[End] so they are swapped.
Step 8: Now the list is scanned from left to right. Since A[Pivot]>A[Beg],value of Beg is
increased by one and the list remains unchanged.
At this point the variable Pivot, Beg, End all refers to same element, the first pass is
terminated and the value 8 is placed at its appropriate position. The elements to its left are
smaller than 8 and the elements to its right are greater than 8.The same process is applied
on left and right sub lists.
ALGORITHM
Step 1: Select first element of array as Pivot
Step 2: Initialize i and j to Beg and End elements respectively
Step 3: Increment i until A[i]>Pivot.
Stop
Step 4: Decrement j until A[ j]>Pivot
Stop
Step 5: if i<j interchange A[i] with A[j].
Step 6: Repeat steps 3,4,5 until i>j i.e: i crossed j.
Step 7: Exchange the Pivot element with element placed at j, which is correct place for
Pivot.
Advantages of Quick Sort:
• This is fastest sorting technique among all.
• It efficiency is also relatively good.
• It requires small amount of memory
Disadvantages:
• It is somewhat complex method for sorting.
• It is little hard to implement than other sorting methods
• It does not perform well in the case of small group of elements.
• Merge sort is the sorting technique that follows the divide and conquer approach.
• It also called stable sort
• It divides the given list into two equal halves, calls itself for the two halves and then merges the
two sorted halves.
• The sub-lists are divided again and again into halves until the list cannot be divided further. Then
we combine the pair of one element lists into two-element lists, sorting them in the process.
Let us apply the Merge Sort to sort the following list:
13 42 36 20 63 23 12
Step 1: First divide the combined list into two sub lists as follows.
Step 2: Now Divide the left sub list into smaller sub list
Step 3: Similarly divide the sub lists till one element is left in the sub list.
Step 4: Next sort the elements in their appropriate positions and then combined the sub
lists.
Step 5: Now these two sub lists are again merged to give the following sorted sub list of size
4.
Step 6: After sorting the left half of the array, containing the same process for the right sub
list also. Then the sorted array of right half of the list is as follows.
Step 7: Finally the left and right halves of the array are merged to give the sorted array as
follows.
11
Advantages:
• Merge sort is stable sort
• It is easy to understand
• It gives better performance.
Disadvantages:
• It requires extra memory space
• Copy of elements to temporary array
• It requires additional array
• It is slow process.
Complexity of Merge Sort: The merge sort algorithm passes over the entire list and requires
at most log n passes and merges n elements in each pass. The total number of comparisons
required by the merge sort is given by O(n log n).
External searching: When the records are stored in disk, tape, any secondary storage then
that searching is known as ‘External Searching’.
Internal Searching: When the records are to be searched or stored entirely within the
computer memory then it is known as ‘Internal Searching’.
12
LINEAR SEARCH
The Linear search or Sequential Search is most simple searching method. It does
not expect the list to be sorted. The Key which to be searched is compared with each
element of the list one by one. If a match exists, the search is terminated. If the end of the
list is reached, it means that the search has failed and the Key has no matching element in
the list.
Ex: consider the following Array A
23 15 18 17 42 96 103
Now let us search for 17 by Linear search. The searching starts from the first position.
Since A[0] ≠17.
The search proceeds to the next position i.e; second position A[1] ≠17.
The above process continuous until the search element is found such as A[3]=17.
Here the searching element is found in the position 4.
Advantages:
• It is simplest known technique.
• The elements in the list can be in any order.
Disadvantages:
This method is in efficient when large numbers of elements are present in list because time
taken for searching is more.
Complexity of Linear Search: The worst and average case complexity of Linear search is
O(n), where ‘n’ is the total number of elements present in the list.
BINARY SEARCH
Pseudocode
Step 2: if x = aq i.e key element is equal to mid element, the problem is immediately solved.
Step 3: if x < aq in this case x has to be searched for only in the sub-list ai, ai+1, ……, aq-1.
Therefore problem reduces to (q- i, ai…aq-1, x).
Step 4: if x > aq , x has to be searched for only in the sub-list aq+1, ...,., al . Therefore problem
reduces to (l-i, aq+1…al, x).
13
Define sorting? What is the difference between internal and external sorting methods?
Ans:- Sorting is a technique of organizing data. It is a process of arranging the elements
either may be ascending or descending order, ie; bringing some order lines with data.
14
UNIT-IV
TREES AND BINARY TREES
TREES
INTRODUCTION
In linear data structure data is organized in sequential order and in non-linear data structure
data is organized in random order. A tree is a very popular non-linear data structure used in
a wide range of applications. Tree is a non-linear data structure which organizes data in
hierarchical structure and this is a recursive definition.
TREE TERMINOLOGIES:
1. Root Node: In a Tree data structure, the first node is called as Root Node. Every tree
must have a root node. We can say that the root node is the origin of the tree data
structure. In any tree, there must be only one root node. We never have multiple root
nodes in a tree.
2. Edge: In a Tree, the connecting link between any two nodes is called as EDGE. In a tree
with 'N' number of nodes there will be a maximum of 'N-1' number of edges.
15
3. Parent Node: In a Tree, the node which is a predecessor of any node is called as PARENT
NODE. In simple words, the node which has a branch from it to any other node is called a
parent node. Parent node can also be defined as "The node which has child / children".
5. Siblings: In a Tree data structure, nodes which belong to same Parent are called
as SIBLINGS. In simple words, the nodes with the same parent are called Sibling nodes.
16
6. Leaf Node: In a Tree data structure, the node which does not have a child is called
as LEAF Node. In simple words, a leaf is a node with no child. In a tree data structure, the
leaf nodes are also called as External Nodes. External node is also a node with no child. In a
tree, leaf node is also called as 'Terminal' node.
7. Internal Nodes: In a Tree data structure, the node which has atleast one child is called
as INTERNAL Node. In simple words, an internal node is a node with atleast one child.
In a Tree data structure, nodes other than leaf nodes are called as Internal Nodes. The root
node is also said to be Internal Node if the tree has more than one node. Internal nodes are
also called as 'Non-Terminal' nodes.
8. Degree: In a Tree data structure, the total number of children of a node is called
as DEGREE of that Node. In simple words, the Degree of a node is total number of children it
has. The highest degree of a node among all the nodes in a tree is called as 'Degree of Tree'
9. Level: In a Tree data structure, the root node is said to be at Level 0 and the children of
root node are at Level 1 and the children of the nodes which are at Level 1 will be at Level 2
17
and so on... In simple words, in a tree each step from top to bottom is called as a Level and
the Level count starts with '0' and incremented by one at each level (Step).
10. Height: In a Tree data structure, the total number of edges from leaf node to a particular
node in the longest path is called as HEIGHT of that Node. In a tree, height of the root node
is said to be height of the tree. In a tree, height of all leaf nodes is '0'.
11. Depth: In a Tree data structure, the total number of egdes from root node to a
particular node is called as DEPTH of that Node. In a tree, the total number of edges from
root node to a leaf node in the longest path is said to be Depth of the tree. In simple words,
the highest depth of any leaf node in a tree is said to be depth of that tree. In a tree, depth
of the root node is '0'.
12. Path: In a Tree data structure, the sequence of Nodes and Edges from one node to
another node is called as PATH between that two Nodes. Length of a Path is total number
of nodes in that path. In below example the path A - B - E - J has length 4.
18
13. Sub Tree: In a Tree data structure, each child from a node forms a subtree recursively.
Every child node will form a subtree on its parent node.
TREE REPRESENTATIONS:
A tree data structure can be represented in two methods. Those methods are as follows...
1. List Representation
2. Left Child - Right Sibling Representation
19
through a 'reference node' which is further linked to any other node directly. This process
repeats for all the nodes in the tree.
The above example tree can be represented using List representation as follows...
In this representation, every node's data field stores the actual value of that node. If that node has
left a child, then left reference field stores the address of that left child node otherwise stores NULL.
If that node has the right sibling, then right reference field stores the address of right sibling node
otherwise stores NULL.
The above example tree can be represented using Left Child - Right Sibling representation as
follows...
In a normal tree, every node can have any number of children. A binary tree is a special type
of tree data structure in which every node can have a maximum of 2 children. One is known
as a left child and the other is known as right child.
A tree in which every node can have a maximum of two children is called Binary Tree.
20
In a binary tree, every node can have either 0 children or 1 child or 2 children but not more
than 2 children.
In general, tree nodes can have any number of children. In a binary tree, each node can
have at most two children. A binary tree is either empty or consists of a node called the root
together with two binary trees called the left subtree and the right subtree. A tree with no
nodes is called as a null tree
Example:
21
Example
22
In above figure, a normal binary tree is converted into full binary tree by adding dummy
nodes.
4. Skewed Binary Tree:
If a tree which is dominated by left child node or right child node, is said to be a Skewed
Binary Tree.
In a skewed binary tree, all nodes except one have only one child node. The remaining
node has no child.
In a left skewed tree, most of the nodes have the left child without corresponding right
child.
In a right skewed tree, most of the nodes have the right child without corresponding left
child.
Properties of binary trees:
Some of the important properties of a binary tree are as follows:
1. If h = height of a binary tree, then
a. Maximum number of leaves = 2h
b. Maximum number of nodes = 2h + 1 - 1
2. If a binary tree contains m nodes at level l, it contains at most 2m nodes at level l + 1.
3. Since a binary tree can contain at most one node at level 0 (the root), it can contain at
most 2l node at level l.
4. The total number of edges in a full binary tree with n node is n –
BINARY TREE REPRESENTATIONS:
A binary tree data structure is represented using two methods. Those methods are as
follows...
1. Array Representation
2. Linked List Representation
Consider the following binary tree...
23
5. give the Array Representation of Binary Tree
In array representation of a binary tree, we use one-dimensional array (1-D Array) to
represent a binary tree.
Consider the above example of a binary tree and it is represented as follows...
To represent a binary tree of depth 'n' using array representation, we need one dimensional
array with a maximum size of 2n + 1.
1. Linked List Representation of Binary Tree
We use a double linked list to represent a binary tree. In a double linked list, every node
consists of three fields. First field for storing left child address, second for storing actual
data and third for the right child address.
In this linked list representation, a node has the following structure...
The above example of the binary tree represented using Linked list representation is shown
as follows...
24
5. Explain BINARY TREE TRAVERSALS with example
In any binary tree, displaying order of nodes depends on the traversal method.
Displaying (or) visiting order of nodes in a binary tree is called as Binary Tree Traversal.
There are three types of binary tree traversals.
1. In - Order Traversal
2. Pre - Order Traversal
3. Post - Order Traversal
Tree traversal in C
#include <stdio.h>
#include <stdlib.h>
struct node {
int item;
struct node* left;
struct node* right;
};
// Inorder traversal
void inorderTraversal(struct node* root) {
if (root == NULL) return;
inorderTraversal(root->left);
printf("%d ->", root->item);
inorderTraversal(root->right);
}
// preorderTraversal traversal
void preorderTraversal(struct node* root) {
if (root == NULL) return;
printf("%d ->", root->item);
preorderTraversal(root->left);
preorderTraversal(root->right);
}
// postorderTraversal traversal
void postorderTraversal(struct node* root) {
if (root == NULL) return;
postorderTraversal(root->left);
25
postorderTraversal(root->right);
printf("%d ->", root->item);
}
Example:
26
That means here we have visited in the order of A-B-D-I-J-F-C-G-K-H using Pre-Order
Traversal.
27
6. write short notes on Binary Search Tree
• Binary Search Tree is a special kind of binary tree in which nodes are arranged in a specific order.
In a binary search tree (BST), each node contains-
1.Only smaller values in its left sub tree
2.Only larger values in its right sub tree.
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)
Basic Operations
7. Construct a Binary Search Tree (BST) for the following sequence of numbers-
10,12,5,4,20,8,7,15 and 13
28
7. Construct a Binary Search Tree (BST) for the following sequence of numbers-
50, 70, 60, 20, 90, 10, 40, 100
29
As 0 < 0, so insert 0 to the le of 0.
As 10 < 0, so insert 10 to the le of 0.
As 0 0, so insert 0 to the right of 0.
As 10 < 0, so insert 10 to the le of 0.
Balance Factor-
In AVL tree,
• Balance factor is defined for every node.
• Balance factor of a node = Height of the left subtree – Height of the right subtree
In AVL tree,
Balance factor of every node is either 0 or 1 or -1.
30
9. Explain the types of Rotation in AVL
31
32
7. What is the maximum height of any AVL-tree with 5 node?
N(h)= 1+N(h-1)+N(H-2)
9. What is B-tree
B tree is specialized Multiway tree used to store records in a disk. B-Tree is Binary search Tree ,but not a binary
tree
Properties of B-Tree
33
34