-
Notifications
You must be signed in to change notification settings - Fork 1
/
Linear_probing.c
62 lines (62 loc) · 1.14 KB
/
Linear_probing.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>
#define size 10
int position(int key)
{
return key % 10;
}
int probe(int H[], int key)
{
int i = 0;
int index = position(key);
while (H[(index + i) % size] != 0)
{
i++;
}
return (index + i) % size;
}
void insert(int H[], int key)
{
int index = position(key);
int i = 0;
if (H[index] == 0)
{
H[index] = key;
}
else
{
index = probe(H, key);
H[index] = key;
}
}
int search(int H[], int key)
{
int index = position(key);
int i = 0;
if (H[index] == key)
{
return index;
}
else
{
while (H[(index + i) % size] != key)
{
if (H[(index + i) % size] == 0)
{
return (-1);
break;
}
i++;
}
return (index + i) % size;
}
}
int main()
{
int HT[10] = {0};
insert(HT, 12);
insert(HT, 25);
insert(HT, 26);
insert(HT, 35);
printf("Key is found at index:%d.\n", search(HT, 26));
}
/*In linear probing,loading factor is maintained near 0.5 .*/