summary refs log tree commit diff stats
path: root/c/binary-search/binary_search.c
diff options
context:
space:
mode:
Diffstat (limited to 'c/binary-search/binary_search.c')
-rw-r--r--c/binary-search/binary_search.c16
1 files changed, 16 insertions, 0 deletions
diff --git a/c/binary-search/binary_search.c b/c/binary-search/binary_search.c
new file mode 100644
index 0000000..fe122d4
--- /dev/null
+++ b/c/binary-search/binary_search.c
@@ -0,0 +1,16 @@
+#include "binary_search.h"
+
+const int *binary_search(int value, const int *arr, size_t length) {
+    size_t left = 0, right = length;
+    while (left < right) {
+        size_t mid = left + (right - left) / 2;
+
+        if (arr[mid] < value)
+            left = mid + 1;
+        else if (arr[mid] > value)
+            right = mid;
+        else
+            return &arr[mid];
+    }
+    return NULL;
+}
22constant.cc?h=hlt&id=dd66068298b0a11f2a1f195376cba98e0c8570b5'>dd660682 ^
01ce563d ^


92a3d082 ^


4a943d4e ^










01ce563d ^









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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79