본문 바로가기
알고리즘

hashing

by 안선생 2022. 11. 27.
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
#include<iostream>
using namespace std;
#define size 10
int hash1(int key)
{
    return key % 10;
}
 
int probe(int h[], int key)
{
    int index = hash1(key);
    int i = 0;
    while (h[(index + i) % size!= 0)
        i++;
    return (index + i) % size;
}
void insert(int h[], int key)
{
    int index = hash1(key);
 
    if (h[index] != 0)
        index = probe(h, key);
    h[index] = key;
 
}
int search(int h[], int key)
{
    int index = hash1(key);
        int i = 0;
        while (h[(index + i) % size!= key)
            if (h[(index + i) % size== 0)
                return 0;
        else
                i++;
    return h[(index + i) % size];
}
int main()
{
    int ht[10]={0};
 
    insert(ht, 12);
    insert(ht, 25);
    insert(ht, 35);
    insert(ht, 26);
 
    cout << search(ht, 15);
    return 0;
}
cs

'알고리즘' 카테고리의 다른 글

graphs  (0) 2022.11.27
chaining  (0) 2022.11.27
sort 정렬  (0) 2022.11.27
heap  (0) 2022.11.27
BST  (0) 2022.11.27