forked from aabyaneh/benchmarks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbsearch_2_1500.c
64 lines (52 loc) · 1.01 KB
/
bsearch_2_1500.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
63
64
uint64_t compar(uint64_t* op1, uint64_t* op2) {
if (*op1 < *op2) {
return 1;
} else if (*op1 > *op2) {
return 2;
} else {
return 0;
}
// if (*op1 == *op2)
// return 0;
// else if (*op1 < *op2)
// return 1;
// else
// return 2;
}
uint64_t* bsearch(uint64_t* key, uint64_t* base, uint64_t num, uint64_t size) {
uint64_t* pivot;
uint64_t result;
while (num != 0) {
pivot = base + (num / 2) * size;
result = compar(key, pivot);
if (result == 0)
return pivot;
if (result == 2) {
base = pivot + size;
num = num - 1;
}
num = num / 2;
}
return (uint64_t*) 0;
}
uint64_t main() {
uint64_t i;
uint64_t* arr;
uint64_t* element;
uint64_t* res;
uint64_t cnt;
uint64_t step;
cnt = 1500;
step = 1;
arr = malloc(cnt * 8);
i = 0;
while (i < cnt) {
*(arr + i) = i * step;
i = i + 1;
}
interval(arr+cnt/2, 0, cnt*2, 1);
element = malloc(1 * 8);
interval(element, 0, cnt*2, 1);
res = bsearch(element, arr, cnt, 1);
return 0;
}