aboutsummaryrefslogtreecommitdiff
path: root/6.1/key.c
diff options
context:
space:
mode:
Diffstat (limited to '6.1/key.c')
-rw-r--r--6.1/key.c22
1 files changed, 22 insertions, 0 deletions
diff --git a/6.1/key.c b/6.1/key.c
new file mode 100644
index 0000000..33757ee
--- /dev/null
+++ b/6.1/key.c
@@ -0,0 +1,22 @@
+#include <string.h>
+#include "key.h"
+
+int bsearch(char *word, struct key tab[], int n)
+{
+ int cond;
+ int low, high, mid;
+
+ low = 0;
+ high = n - 1;
+ while (low <= high) {
+ mid = (low+high)/2;
+ if ((cond = strcmp(word, tab[mid].word)) < 0)
+ high = mid - 1;
+ else if (cond > 0)
+ low = mid + 1;
+ else
+ return mid;
+ }
+
+ return -1;
+}