From a9205b61d3fee482f027437f530a5ebeb10822f7 Mon Sep 17 00:00:00 2001 From: Ragubans <56040758+Ragubans@users.noreply.github.com> Date: Sun, 18 Oct 2020 23:18:22 +0530 Subject: [PATCH] Create Binary Search Algorithm.cpp --- algorithms/Binary Search Algorithm.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 algorithms/Binary Search Algorithm.cpp diff --git a/algorithms/Binary Search Algorithm.cpp b/algorithms/Binary Search Algorithm.cpp new file mode 100644 index 00000000..c18d1dd1 --- /dev/null +++ b/algorithms/Binary Search Algorithm.cpp @@ -0,0 +1,14 @@ +#include +using namespace std; +int binarySearch(int arr[], int p, int r, int num) { + if (p <= r) { + int mid = (p + r)/2; + if (arr[mid] == num) + return mid ; + if (arr[mid] > num) + return binarySearch(arr, p, mid-1, num); + if (arr[mid] > num) + return binarySearch(arr, mid+1, r, num); + } + return -1; +}