-
Notifications
You must be signed in to change notification settings - Fork 24
/
tinybsearch.c
59 lines (53 loc) · 2.29 KB
/
tinybsearch.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
// Tiny binary search (dichotomic): array must be sorted && supporting sequential access.
// - rlyeh, public domain | wtrmrkrlyeh
#include <string.h>
unsigned bsearchint( const int *array, int numelems, int key ) {
int min = 0, max = numelems;
while( min <= max ) {
int mid = min + ( max - min ) / 2;
/**/ if( key == array[mid] ) return mid;
else if( key < array[mid] ) max = mid - 1;
else min = mid + 1;
}
return ~0u;
}
unsigned bsearchsz( const size_t *array, int numelems, size_t key ) {
int min = 0, max = numelems;
while( min <= max ) {
int mid = min + ( max - min ) / 2;
/**/ if( key == array[mid] ) return mid;
else if( key < array[mid] ) max = mid - 1;
else min = mid + 1;
}
return ~0u;
}
unsigned bsearchstr( const char **array, int numelems, const char *key ) {
int min = 0, max = numelems;
while( min <= max ) {
int mid = min + ( max - min ) / 2;
int search = strcmp(key, array[mid]);
/**/ if( 0 ==search ) return mid;
else if( search < 0 ) max = mid - 1;
else min = mid + 1;
}
return ~0u;
}
/*
#include <assert.h>
int main() {
// @ [0] [1] [2] [3]
const char *dict[] = { "abc", "abracadabra", "ale hop", "all your base"};
assert( bsearchstr(dict, sizeof(dict) / sizeof(dict[0]), "abc") == 0 ); // @ [0]
assert( bsearchstr(dict, sizeof(dict) / sizeof(dict[0]), "abracadabra") == 1 ); // @ [1]
assert( bsearchstr(dict, sizeof(dict) / sizeof(dict[0]), "ale hop") == 2 ); // @ [2]
assert( bsearchstr(dict, sizeof(dict) / sizeof(dict[0]), "all your base") == 3 ); // @ [3]
assert( bsearchstr(dict, sizeof(dict) / sizeof(dict[0]), "are belong to us") == ~0u ); // not found
assert( bsearchstr(dict, sizeof(dict) / sizeof(dict[0]), "move") == ~0u ); // not found
assert( bsearchstr(dict, sizeof(dict) / sizeof(dict[0]), "every") == ~0u ); // not found
assert( bsearchstr(dict, sizeof(dict) / sizeof(dict[0]), "zig") == ~0u ); // not found
assert( bsearchstr(dict, sizeof(dict) / sizeof(dict[0]), "") == ~0u ); // not found
for( int i = 0; i < sizeof(dict) / sizeof(dict[0]); ++i ) {
assert( i == bsearchstr(dict, sizeof(dict) / sizeof(dict[0]), dict[i]) );
}
}
*/