forked from troglobit/snippets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
binsrch.h
57 lines (48 loc) · 1.42 KB
/
binsrch.h
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
/*
** BINSRCH.H - Header file for binary searching of an array for a key.
**
** Public domain demo by Bob Stout
*/
#ifndef BINSRCH__H
#define BINSRCH__H
#include <stddef.h>
#include "sniptype.h"
#if !(__cplusplus)
int BinSearchI(int key, int *r, size_t n);
int BinSearchC(signed char key, signed char *r, size_t n);
int BinSearchB(BYTE key, BYTE *r, size_t n);
int BinSearchS(signed short key, signed short *r, size_t n);
int BinSearchW(WORD key, WORD *r, size_t n);
int BinSearchL(signed long key, signed long *r, size_t n);
int BinSearchDW(DWORD key, DWORD *r, size_t n);
int BinSearchF(float key, float *r, size_t n);
int BinSearchD(double key, double *r, size_t n);
#else /* C++ */
template <class T>
int BinSearch(T key, T *r, size_t n)
{
int high, i, low;
if (n > 1)
{
for (low = 0, high = n-1; high - low > 1; )
{
i = (high+low) / 2;
if (key == r[i])
return i;
else if (key < r[i])
high = i;
else low = i;
}
if (key == r[high])
return high;
}
else if (key == r[low])
return low;
else return -1;
}
else if (r[0] == key)
return 0;
else return -1;
}
#endif /* C/C++ */
#endif /* BINSRCH__H */