From e24eeb5fd2945cdeeffd1ae0e1fd88dd7a105c4d Mon Sep 17 00:00:00 2001 From: adhilcodes Date: Wed, 14 Oct 2020 10:25:20 +0530 Subject: [PATCH 1/3] adding c++ program file --- Binary_search/main.cpp | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 Binary_search/main.cpp diff --git a/Binary_search/main.cpp b/Binary_search/main.cpp new file mode 100644 index 0000000..354ea15 --- /dev/null +++ b/Binary_search/main.cpp @@ -0,0 +1,26 @@ +#include +using namespace std; +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; +} + +int main(void) +{ + int arr[] = { 24, 33, 4, 130, 41 }; + int x = 4; + int n = sizeof(arr) / sizeof(arr[0]); + int result = binarySearch(arr, 0, n - 1, x); + (result == -1) ? cout << "No such Element in array" + : cout << "Element is present at index " << result; + return 0; +} \ No newline at end of file From 4c9acc07bf2621bd5e379e92e69ded4308c82378 Mon Sep 17 00:00:00 2001 From: adhilcodes Date: Wed, 14 Oct 2020 10:53:10 +0530 Subject: [PATCH 2/3] adding the readmefile --- Binary_search/README.md | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Binary_search/README.md diff --git a/Binary_search/README.md b/Binary_search/README.md new file mode 100644 index 0000000..38e78ea --- /dev/null +++ b/Binary_search/README.md @@ -0,0 +1,2 @@ +BINARY ALGORITHM +This is an algorithm to find where an element present in an array(ie; index of element present) \ No newline at end of file From 2deec9e7481b45b5f6cfeaaf5778ebe9c6eaf3d8 Mon Sep 17 00:00:00 2001 From: Abdul Adhil PK <65992809+adhilcodes@users.noreply.github.com> Date: Sun, 18 Oct 2020 21:53:32 +0530 Subject: [PATCH 3/3] Update README.md --- Binary_search/README.md | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/Binary_search/README.md b/Binary_search/README.md index 38e78ea..06ebb67 100644 --- a/Binary_search/README.md +++ b/Binary_search/README.md @@ -1,2 +1,10 @@ BINARY ALGORITHM -This is an algorithm to find where an element present in an array(ie; index of element present) \ No newline at end of file +This algorithm help the user to search a particular element present in an array(using the index of element present in an array) +**How the Algorithm Works** +1. Find the middle element +2. If middle element equal to searched value algorithm stops: + otherwise there are two cases: + 2.1 search the value is less than that of the middle element.Here, + the algorithm go to the step 1 for the part of the array that before middle element + 2.2 search the value is greater than that of the middle element.And here, + the algorithm go to the step 1 for the part of the array that after middle element