0% found this document useful (0 votes)
22 views13 pages

DSA Hashing

Consists of hashing techniques-linear probing,quadratic probing and double hashing code and theory in c

Uploaded by

vedant.spamz65
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views13 pages

DSA Hashing

Consists of hashing techniques-linear probing,quadratic probing and double hashing code and theory in c

Uploaded by

vedant.spamz65
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Code:

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;

for(i=0; i<10; i++) {


h1.arr[i] = -1;
}

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;

while(h1.arr[(key + i*i) % 10] != -1) {


if(h1.arr[(key + i*i) % 10] == x) {
printf("%d is the position of the element in the table.\n", (key + i*i) % 10);
return;
}
i++;
if(i == 10) {
break;
}
}
printf("Element not found.\n");
}

void insert(int x) {
int key = x % 10;
int i = 0;

while(h1.arr[(key + i*i) % 10] != -1) {


i++;
if(i == 10) {
printf("Hash table is full, cannot insert %d.\n", x);
return;
}
}

h1.arr[(key + i*i) % 10] = x;


}
C) Double Hashing
#include<stdio.h>

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;

for(i=0; i<10; i++) {


h1.arr[i] = -1;
}

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;

while(h1.arr[(key + i * hash2(x)) % 10] != -1) {


if(h1.arr[(key + i * hash2(x)) % 10] == x) {
printf("%d is the position of the element in the table.\n", (key + i * hash2(x)) %
10);
return;
}
i++;
if(i == 10) {
break;
}
}
printf("Element not found.\n");
}

void insert(int x) {
int key = hash1(x);
int i = 0;

while(h1.arr[(key + i * hash2(x)) % 10] != -1) {


i++;
if(i == 10) {
printf("Hash table is full, cannot insert %d.\n", x);
return;
}
}

h1.arr[(key + i * hash2(x)) % 10] = x;


}

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:

You might also like