DSA Hashing
DSA Hashing
A) Linear Probing
#include<stdio.h>
struct hash
{
int arr[10];
};
struct hash h1;
void main()
{
int choice;
int n;
int m;
int i;
for(i=0;i<=10;i++)
{
h1.arr[i]=-1;
}
do
{
printf("1.Insert\n2.Display\n3.SearchElement\nEnter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1://Insert Elements
printf("Enter the number to be inserted: ");
scanf("%d",&n);
insert(n);
break;
case 2://Display list
display();
break;
case 3://search elements
printf("Enter the elements to be searched: ");
scanf("%d",&m);
search(m);
break;
default:
printf("Invalid choice");
break;
}
}while(choice<4);
}
void display()
{
for(int i=0;i<10;i++)
{
printf("%d ",h1.arr[i]);
}
printf("\n");
}
void search(int x)
{
int key;
key=x%10;
for(int key=0;key<=10;key++)
{
if(h1.arr[key]==x)
{
printf("%d is the position of element in the table.",key);
break;
}
}
}
void insert(int x)
{
int key;
key=x%10;
if(h1.arr[key]==-1)
{
h1.arr[key]=x;
}
else
{
for(int k=key;k<10;k++)
{
if(h1.arr[k]==-1)
{
h1.arr[k]=x;
break;
}
}
printf("An element already exists at that position.\nElement shifted to next
position\n");
}
}
B) Quadratic Probing
#include<stdio.h>
struct hash {
int arr[10];
};
struct hash h1;
void display();
void search(int x);
void insert(int x);
void main() {
int choice;
int n;
int m;
int i;
do {
printf("1. Insert\n2. Display\n3. Search Element\nEnter your choice: ");
scanf("%d", &choice);
switch(choice) {
case 1: // Insert Elements
printf("Enter the number to be inserted: ");
scanf("%d", &n);
insert(n);
break;
case 2: // Display list
display();
break;
case 3: // Search elements
printf("Enter the element to be searched: ");
scanf("%d", &m);
search(m);
break;
default:
printf("Invalid choice\n");
break;
}
} while(choice < 4);
}
void display() {
for(int i=0; i<10; i++) {
printf("%d ", h1.arr[i]);
}
printf("\n");
}
void search(int x) {
int key = x % 10;
int i = 0;
void insert(int x) {
int key = x % 10;
int i = 0;
struct hash {
int arr[10];
};
struct hash h1;
void display();
void search(int x);
void insert(int x);
int hash1(int x);
int hash2(int x);
void main() {
int choice;
int n;
int m;
int i;
do {
printf("1. Insert\n2. Display\n3. Search Element\nEnter your choice: ");
scanf("%d", &choice);
switch(choice) {
case 1: // Insert Elements
printf("Enter the number to be inserted: ");
scanf("%d", &n);
insert(n);
break;
case 2: // Display list
display();
break;
case 3: // Search elements
printf("Enter the element to be searched: ");
scanf("%d", &m);
search(m);
break;
default:
printf("Invalid choice\n");
break;
}
} while(choice < 4);
}
void display() {
for(int i=0; i<10; i++) {
printf("%d ", h1.arr[i]);
}
printf("\n");
}
void search(int x) {
int key = hash1(x);
int i = 0;
void insert(int x) {
int key = hash1(x);
int i = 0;
int hash1(int x) {
return x % 10;
}
int hash2(int x) {
return 7 - (x % 7);
}
Output:
1. Linear Probing:
1.Insert
2.Display
3.SearchElement
Enter your choice: 1
Enter the number to be inserted: 12
1.Insert
2.Display
3.SearchElement
Enter your choice: 1
Enter the number to be inserted: 22
An element already exists at that position.
Element shifted to next position
1.Insert
2.Display
3.SearchElement
Enter your choice: 2
-1 -1 12 22 -1 -1 -1 -1 -1 -1
1.Insert
2.Display
3.SearchElement
Enter your choice: 1
Enter the number to be inserted: 32
An element already exists at that position.
Element shifted to next position
1.Insert
2.Display
3.SearchElement
Enter your choice: 2
-1 -1 12 22 32 -1 -1 -1 -1 -1
1.Insert
2.Display
3.SearchElement
Enter your choice: 1
Enter the number to be inserted: 16
1.Insert
2.Display
3.SearchElement
Enter your choice: 1
Enter the number to be inserted: 26
An element already exists at that position.
Element shifted to next position
1.Insert
2.Display
3.SearchElement
Enter your choice: 2
-1 -1 12 22 32 -1 16 26 -1 -1
1.Insert
2.Display
3.SearchElement
Enter your choice: 3
Enter the elements to be searched: 26
7 is the position of element in the table.
2. Quadratic Probing
1. Insert
2. Display
3. Search Element
Enter your choice: 1
Enter the number to be inserted: 12
1. Insert
2. Display
3. Search Element
Enter your choice: 1
Enter the number to be inserted: 14
1. Insert
2. Display
3. Search Element
Enter your choice: 2
-1 -1 12 -1 14 -1 -1 -1 -1 -1
1. Insert
2. Display
3. Search Element
Enter your choice: 1
Enter the number to be inserted: 24
1. Insert
2. Display
3. Search Element
Enter your choice: 1
Enter the number to be inserted: 34
1. Insert
2. Display
3. Search Element
Enter your choice: 2
-1 -1 12 -1 14 24 -1 -1 34 -1
1. Insert
2. Display
3. Search Element
Enter your choice: 3
Enter the element to be searched: 14
4 is the position of the element in the table.
1. Insert
2. Display
3. Search Element
Enter your choice: 3
Enter the element to be searched: 34
8 is the position of the element in the table.
1. Insert
2. Display
3. Search Element
Enter your choice:
3. Double Hashing:
1. Insert
2. Display
3. Search Element
Enter your choice: 1
Enter the number to be inserted: 12
1. Insert
2. Display
3. Search Element
Enter your choice: 2
-1 -1 12 -1 -1 -1 -1 -1 -1 -1
1. Insert
2. Display
3. Search Element
Enter your choice: 1
Enter the number to be inserted: 22
1. Insert
2. Display
3. Search Element
Enter your choice: 2
-1 -1 12 -1 -1 -1 -1 -1 22 -1
1. Insert
2. Display
3. Search Element
Enter your choice: 3
Enter the element to be searched: 12
2 is the position of the element in the table.
1. Insert
2. Display
3. Search Element
Enter your choice: 3
Enter the element to be searched: 22
8 is the position of the element in the table.
1. Insert
2. Display
3. Search Element
Enter your choice: 1
Enter the number to be inserted: 32
1. Insert
2. Display
3. Search Element
Enter your choice: 2
-1 -1 12 -1 -1 32 -1 -1 22 -1
1. Insert
2. Display
3. Search Element
Enter your choice: