diff options
| author | Martial Simon <msimon_fr@hotmail.com> | 2025-09-15 01:08:27 +0200 |
|---|---|---|
| committer | Martial Simon <msimon_fr@hotmail.com> | 2025-09-15 01:08:27 +0200 |
| commit | c9b6b9a5ca082fe7c1b6f58d7713f785a9eb6a5c (patch) | |
| tree | 3e4f42f93c7ae89a364e4d51fff6e5cec4e55fa9 /graphs/piscine/binary_search_ptr/bsearch.c | |
add: graphs et rushs
Diffstat (limited to 'graphs/piscine/binary_search_ptr/bsearch.c')
| -rw-r--r-- | graphs/piscine/binary_search_ptr/bsearch.c | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/graphs/piscine/binary_search_ptr/bsearch.c b/graphs/piscine/binary_search_ptr/bsearch.c new file mode 100644 index 0000000..bdc189c --- /dev/null +++ b/graphs/piscine/binary_search_ptr/bsearch.c @@ -0,0 +1,38 @@ +#include "bsearch.h" + +#include <stddef.h> + +int *binary_search(int *begin, int *end, int elt) +{ + if (begin == end) + { + return begin; + } + if (begin > end) + { + if (elt > *begin) + { + return begin + 1; + } + return begin; + } + + size_t m = (end - begin) / 2; + + if (begin[m] == elt) + { + return begin + m; + } + + if (begin[m] > elt) + { + return binary_search(begin, begin + m, elt); + } + + if (m == 0) + { + m++; + } + + return binary_search(begin + m, end, elt); +} |
