Module 5 UQ
Module 5 UQ
In data structures,
Hashing is a well-known technique to search any particular element among several
elements.
It minimizes the number of comparisons while performing the search
Hashing Mechanism-
In hashing,
• Hashing is the process of mapping large amount of data item to smaller table
with the help of hashing function.
• Hashing is also known as Hashing Algorithm or Message Digest Function.
• An array data structure called as Hash table is used to store the data items.
• Based on the hash key value, data items are inserted into the hash table.
Applications
9. What are the limitations of linear probing? How can double hashing be used to resolve
these limitations? Illustrate with examples.
Limitation of Linear Probing
The main problem with linear probing is clustering.
Many consecutive elements form groups.
Then, it takes time to search an element or to find an empty bucket.
Double hashing resolves the limitations of linear probing by addressing its primary
weaknesses—primary clustering and poor performance at high load factors—through the use of
a second, independent hash function. This creates a more dispersed probing sequence,
minimizing clustering and improving efficiency.
Linear Probing example
Quadratic Probing example
10. What is hash table? What are the properties of hash function?
Hash tables are a type of data structure in which the address/ index value of the data element is
generated from a hash function. This enables very fast data access as the index value behaves
as a key for the data value.
One-way: It's computationally infeasible to find an input that maps to a pre-specified output.
Collision resistant: It's computationally infeasible to find two distinct inputs that map to the
same output.
Deterministic: The same input message always produces the same hash value.
Efficient: The hash value is computed quickly, regardless of the input size.
Hidden: It's difficult to guess the input value for a hash function from its output.
Puzzle-friendly: It's difficult to select an input that provides a pre-defined output.
11. What is max heap?Write an algorithm to perform heap sort. Give example.
A max heap is a complete binary tree in which the value of a node is greater than or equal to the
values of its children. Max Heap data structure is useful for sorting data using heap sort.
12. Show all the passes using insertion sort for the following list
54,26,93,17,77,31,44,55,20.
13. Briefly explain any 4 hashing functions.
Division Method:
Definition: The key is divided by the size of the hash table, and the remainder is used
as the hash value.
Formula: h(k)=kmod mh(k) = k \mod mh(k)=kmodm where kkk is the key and mmm
is the size of the hash table.
Advantages:
o Simple and efficient to compute.
Limitations:
o Works best when mmm (hash table size) is a prime number to avoid clustering
and improve distribution.
Example:
For k=25k = 25k=25 and m=7m = 7m=7:
h(25)=25mod 7=4h(25) = 25 \mod 7 = 4h(25)=25mod7=4.
Mid-Square Method:
Definition: The key is squared, and some middle bits of the resulting number are
extracted as the hash value.
Steps:
1. Square the key.
2. Extract the middle rrr bits.
Advantages:
o Works well for keys with patterns, as squaring spreads the bits.
Limitations:
o Requires consistent bit extraction.
Example:
H(k): 72 93 99
4th and 5th digits have been selected. From the right side.
Folding Method
The key k is partitioned into no. of parts
Then add these parts together and ignoring the last carry.
One can also reverse the first part before adding (right or left justified. Mostly right)
H(k) = k1 + k2 + ………. + kn
Example:
H(3205)=32+05=37 or H(3250)=32+50=82
H(7148)=71+48=19 or H(7184)=71+84=55
H(2345)=23+45=68 or H(2354)=23+54=77
Multiplication Method:
Formula:
Advantages:
o Works well even if mmm is not a prime number.
Limitations:
o Slightly more computationally expensive.
Example:
14. Write the algorithm for Quicksort. Analyse its worst case and best case
performances. Let the size of the hash table be 12. Consider the keys 43, 24, 57,
12, 10, 64, 19, 82, 36, 39 in the order. Show how the keys are occupied using
chaining method.
Quicksort is a sorting algorithm based on the divide and conquer approach where
1. An array is divided into subarrays by selecting a pivot element (element selected from
the array).
While dividing the array, the pivot element should be positioned in such a way that
elements less than pivot are kept on the left side and elements greater than pivot are on
the right side of the pivot.
2. The left and right subarrays are also divided using the same approach. This process
continues until each subarray contains a single element.
At this point, elements are already sorted. Finally, elements are combined to form a
sorted array.
Algorithm
• ..Step 1 - Consider the first element of the list as pivot (i.e., Element at first position in
the list).
• Step 2 - Define two variables i and j. Set i and j to first and last elements of the list
respectively.
• Step 3 - Increment i until list[i] > pivot then stop.
• Step 4 - Decrement j until list[j] < pivot then stop.
• Step 5 - If i < j then exchange list[i] and list[j].
• Step 6 - Repeat steps 3,4 & 5 until i > j.
• Step 7 - Exchange the pivot element with list[j] element.
15. Explain Merge Sort algorithm/pseudocode with the help of an example? Mention the
best case and worst case time complexity of Merge sort algorithm?
Merge Sort is one of the most popular sorting algorithms that is based on the principle
of Divide and Conquer Algorithm.
Here, a problem is divided into multiple sub-problems. Each sub-problem is solved
individually. Finally, sub-problems are combined to form the final solution.
#include<stdio.h>
int l,a[10],n,b[10],i;
void merge(int a,int b,int c);
void mergesort(int a,int b);
int main()
{
printf("Enter the no.of elements: ");
scanf("%d",&n);
printf("\nEnter the elements: ");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
mergesort(0,n-1);
printf("\nSorted elements are: ");
for(i=0;i<n;i++)
printf("%d ",a[i]);
}
void mergesort(int l,int h)
{
int mid;
if(l<h)
{
mid=(l+h)/2;
mergesort(l,mid);
mergesort(mid+1,h);
merge(l,mid,h);
}
}
void merge(int l,int mid,int h)
{
int hi=l,i=l,j=mid+1,k;
while(hi<=mid&&j<=h)
{
if(a[hi]<=a[j])
{
b[i]=a[hi];
hi=hi+1;
}
else
{
b[i]=a[j];
j=j+1;
}
i=i+1;
}
if(hi<=mid)
{
for(k=hi;k<=mid;k++)
{
b[i]=a[k];
i=i+1;
}
}
else
{
for(k=j;k<=h;k++)
{
b[i]=a[k];
i++;
}
}
for(k=l;k<=h;k++)
{
a[k]=b[k];
}
Time Complexity
Best O(n*log n)
Worst O(n*log n)
Average O(n*log n)