From 61c7ce4e50e07672a065a4741ba83975e5c04d7c Mon Sep 17 00:00:00 2001 From: meghaa11 <65069099+meghaa11@users.noreply.github.com> Date: Mon, 19 Oct 2020 22:51:29 +0530 Subject: [PATCH] Create binary_search.c --- SearchAlgorithms/binary_search.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 SearchAlgorithms/binary_search.c diff --git a/SearchAlgorithms/binary_search.c b/SearchAlgorithms/binary_search.c new file mode 100644 index 0000000..11c5af6 --- /dev/null +++ b/SearchAlgorithms/binary_search.c @@ -0,0 +1,13 @@ +int binarySearch(int arr[], int l, int r, int x) +{ + if (r >= l) + { + int mid = l + (r - l) / 2; + if (arr[mid] == x) + return mid; + if (arr[mid] > x) + return binarySearch(arr, l, mid - 1, x); + return binarySearch(arr, mid + 1, r, x); + } + return -1; +}