From c9b6b9a5ca082fe7c1b6f58d7713f785a9eb6a5c Mon Sep 17 00:00:00 2001 From: Martial Simon Date: Mon, 15 Sep 2025 01:08:27 +0200 Subject: add: graphs et rushs --- graphs/piscine/binary_search_ptr/bsearch.c | 38 ++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 graphs/piscine/binary_search_ptr/bsearch.c (limited to 'graphs/piscine/binary_search_ptr/bsearch.c') 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 + +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); +} -- cgit v1.2.3