-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHashing.c
62 lines (59 loc) · 1.29 KB
/
Hashing.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include<stdio.h>
#include<math.h>
long arr[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
void insert(){
int i,c=1,collision=0;
long a = 100000 + (rand() % 900000);
printf("Data :: %ld\n",a);
while(c==1){
i=hash(a + collision);
if(arr[i]!=0){
collision++;
printf("collision @ %d -> ",i);
}else{
arr[i]=a;
break;
}
}
if(collision>0){
printf("\n");
}
printf("%d Inserted @ %d\n\n",a,i);
}
int hash(long val){
return (val % 18)+2;
}
void display(){
printf("\nHashed Array is :: \n");
printf("|");
for(int i=0;i<20*9-1;i++){
printf("=");
}printf("|\n");
for(int i=0;i<20;i++){
if(i<10){
printf("| 0%d ",i);
}else{
printf("| %d ",i);
}
}
printf("|\n|");
for(int i=0;i<20*9-1;i++){
printf("=");
}printf("|\n");
for(int i=0;i<20;i++){
if(arr[i]==0){
printf("| ------ ");
}else{
printf("| %ld ",arr[i]);
}
}
printf("|\n|");
for(int i=0;i<20*9-1;i++){
printf("=");
}printf("|\n");
}
int main(){
for(int i=0;i<15;i++)
insert();
display();
}