From 5dbd45a3f08e59e7973fe2ae8f6bc3de77b7e73d Mon Sep 17 00:00:00 2001 From: Faizan Ahamed Date: Sun, 26 Apr 2020 14:18:01 +0530 Subject: [PATCH 01/11] error in line 23 i.e a constant value in line 23. int a[n]; give error while running i.e. expression must have a constant value. so, rectifying this we can use pointer. int* a = new int[n]; --- search/Binary Search.cpp | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/search/Binary Search.cpp b/search/Binary Search.cpp index 2e90855e8..83c99926a 100644 --- a/search/Binary Search.cpp +++ b/search/Binary Search.cpp @@ -20,7 +20,13 @@ int main(int argc, char const *argv[]) cout << "Enter size of array: "; cin >> n; cout << "Enter array elements: "; + /* int a[n]; + getting error size must be declare + so for rectifying this use pointer. + int a[n]; replace by int* a = new int[n]; + */ + int* a = new int[n]; for (int i = 0; i < n; ++i) { cin >> a[i]; @@ -33,4 +39,4 @@ int main(int argc, char const *argv[]) else cout << key << " not found" << endl; return 0; -} \ No newline at end of file +} From 1eef89591e4bbe4f06ecd2cf0b7a506e8550ea73 Mon Sep 17 00:00:00 2001 From: Faizan Ahamed Date: Sun, 26 Apr 2020 14:37:59 +0530 Subject: [PATCH 02/11] Update Binary Search.cpp --- search/Binary Search.cpp | 19 ++++--------------- 1 file changed, 4 insertions(+), 15 deletions(-) diff --git a/search/Binary Search.cpp b/search/Binary Search.cpp index 83c99926a..7edf7b696 100644 --- a/search/Binary Search.cpp +++ b/search/Binary Search.cpp @@ -1,9 +1,6 @@ #include -using namespace std; -int binary_search(int a[], int l, int r, int key) -{ - while (l <= r) - { +int binary_search(int a[], int l, int r, int key) { + while (l <= r) { int m = l + (r - l) / 2; if (key == a[m]) return m; @@ -14,21 +11,13 @@ int binary_search(int a[], int l, int r, int key) } return -1; } -int main(int argc, char const *argv[]) -{ +int main(int argc, char const *argv[]) { int n, key; cout << "Enter size of array: "; cin >> n; cout << "Enter array elements: "; - /* - int a[n]; - getting error size must be declare - so for rectifying this use pointer. - int a[n]; replace by int* a = new int[n]; - */ int* a = new int[n]; - for (int i = 0; i < n; ++i) - { + for (int i = 0; i < n; i++) { cin >> a[i]; } cout << "Enter search key: "; From 2f6208532bc12a462c708d2194fc44429a75e7d1 Mon Sep 17 00:00:00 2001 From: Faizan Ahamed Date: Sun, 26 Apr 2020 14:42:55 +0530 Subject: [PATCH 03/11] Update Binary Search.cpp --- search/Binary Search.cpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/search/Binary Search.cpp b/search/Binary Search.cpp index 7edf7b696..8c28611d9 100644 --- a/search/Binary Search.cpp +++ b/search/Binary Search.cpp @@ -13,19 +13,19 @@ int binary_search(int a[], int l, int r, int key) { } int main(int argc, char const *argv[]) { int n, key; - cout << "Enter size of array: "; - cin >> n; - cout << "Enter array elements: "; + std::cout << "Enter size of array: "; + std::cin >> n; + std::cout << "Enter array elements: "; int* a = new int[n]; for (int i = 0; i < n; i++) { - cin >> a[i]; + std::cin >> a[i]; } - cout << "Enter search key: "; - cin >> key; + std::cout << "Enter search key: "; + std::cin >> key; int res = binary_search(a, 0, n - 1, key); if (res != -1) - cout << key << " found at index " << res << endl; + std::cout << key << " found at index " << res << endl; else - cout << key << " not found" << endl; + std::cout << key << " not found" << endl; return 0; } From 9543e522f2e7b4f51c1aab81193a1cc76e487dd6 Mon Sep 17 00:00:00 2001 From: Faizan Ahamed Date: Sun, 26 Apr 2020 14:46:20 +0530 Subject: [PATCH 04/11] Update Binary Search.cpp --- search/Binary Search.cpp | 52 ++++++++++++++++++++-------------------- 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/search/Binary Search.cpp b/search/Binary Search.cpp index 8c28611d9..dc46b5e02 100644 --- a/search/Binary Search.cpp +++ b/search/Binary Search.cpp @@ -1,31 +1,31 @@ #include int binary_search(int a[], int l, int r, int key) { - while (l <= r) { - int m = l + (r - l) / 2; - if (key == a[m]) - return m; - else if (key < a[m]) - r = m - 1; - else - l = m + 1; - } - return -1; +while (l <= r) { +int m = l + (r - l) / 2; +if (key == a[m]) +return m; +else if (key < a[m]) +r = m - 1; +else +l = m + 1; +} +return -1; } int main(int argc, char const *argv[]) { - int n, key; - std::cout << "Enter size of array: "; - std::cin >> n; - std::cout << "Enter array elements: "; - int* a = new int[n]; - for (int i = 0; i < n; i++) { - std::cin >> a[i]; - } - std::cout << "Enter search key: "; - std::cin >> key; - int res = binary_search(a, 0, n - 1, key); - if (res != -1) - std::cout << key << " found at index " << res << endl; - else - std::cout << key << " not found" << endl; - return 0; +int n, key; +std::cout << "Enter size of array: "; +std::cin >> n; +std::cout << "Enter array elements: "; +int* a = new int[n]; +for (int i = 0; i < n; i++) { +std::cin >> a[i]; +} +std::cout << "Enter search key: "; +std::cin >> key; +int res = binary_search(a, 0, n - 1, key); +if (res != -1) +std::cout << key << " found at index " << res << endl; +else +std::cout << key << " not found" << endl; +return 0; } From dd09663ece4b12e4191f9c5b1984080c884e87cf Mon Sep 17 00:00:00 2001 From: Faizan Ahamed Date: Sun, 26 Apr 2020 14:49:01 +0530 Subject: [PATCH 05/11] Update Binary Search.cpp --- search/Binary Search.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/search/Binary Search.cpp b/search/Binary Search.cpp index dc46b5e02..71e7c2bd0 100644 --- a/search/Binary Search.cpp +++ b/search/Binary Search.cpp @@ -11,7 +11,7 @@ l = m + 1; } return -1; } -int main(int argc, char const *argv[]) { +int main() { int n, key; std::cout << "Enter size of array: "; std::cin >> n; @@ -24,7 +24,7 @@ std::cout << "Enter search key: "; std::cin >> key; int res = binary_search(a, 0, n - 1, key); if (res != -1) -std::cout << key << " found at index " << res << endl; +std::cout << key << " found at index " << res << std::endl; else std::cout << key << " not found" << endl; return 0; From 6616ae320ba1bb60d6bff2f4e3be293d9294a1d6 Mon Sep 17 00:00:00 2001 From: Faizan Ahamed Date: Sun, 26 Apr 2020 14:54:26 +0530 Subject: [PATCH 06/11] Update Binary Search.cpp --- search/Binary Search.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/search/Binary Search.cpp b/search/Binary Search.cpp index 71e7c2bd0..17ce0df5a 100644 --- a/search/Binary Search.cpp +++ b/search/Binary Search.cpp @@ -11,7 +11,7 @@ l = m + 1; } return -1; } -int main() { +int main(int argc, char const* argv[]) { int n, key; std::cout << "Enter size of array: "; std::cin >> n; @@ -26,6 +26,6 @@ int res = binary_search(a, 0, n - 1, key); if (res != -1) std::cout << key << " found at index " << res << std::endl; else -std::cout << key << " not found" << endl; +std::cout << key << " not found" << endl::endl; return 0; } From 9813693baabbee61492ca3b400172d529da1b5e2 Mon Sep 17 00:00:00 2001 From: Faizan Ahamed Date: Sun, 26 Apr 2020 14:56:30 +0530 Subject: [PATCH 07/11] Update Binary Search.cpp --- search/Binary Search.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/search/Binary Search.cpp b/search/Binary Search.cpp index 17ce0df5a..8fc28e97b 100644 --- a/search/Binary Search.cpp +++ b/search/Binary Search.cpp @@ -26,6 +26,6 @@ int res = binary_search(a, 0, n - 1, key); if (res != -1) std::cout << key << " found at index " << res << std::endl; else -std::cout << key << " not found" << endl::endl; +std::cout << key << " not found" << std::endl; return 0; } From cd7105afcf8f0e021d2870995a13ef57acf41d84 Mon Sep 17 00:00:00 2001 From: Faizan Ahamed Date: Sun, 26 Apr 2020 14:57:59 +0530 Subject: [PATCH 08/11] Rename Binary Search.cpp to binary_search.cpp --- search/{Binary Search.cpp => binary_search.cpp} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename search/{Binary Search.cpp => binary_search.cpp} (100%) diff --git a/search/Binary Search.cpp b/search/binary_search.cpp similarity index 100% rename from search/Binary Search.cpp rename to search/binary_search.cpp From b8a6a6470f90561111b9a5e68097569e352e432e Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Sun, 26 Apr 2020 09:28:13 +0000 Subject: [PATCH 09/11] updating DIRECTORY.md --- DIRECTORY.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DIRECTORY.md b/DIRECTORY.md index f7fb6e9b7..23d2ee377 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -149,7 +149,7 @@ * [Segtree](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/range_queries/segTree.cpp) ## Search - * [Binary Search](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/search/Binary%20Search.cpp) + * [Binary Search](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/search/binary_search.cpp) * [Exponential Search](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/search/exponential_search.cpp) * [Hash Search](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/search/hash_search.cpp) * [Interpolation Search](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/search/Interpolation%20Search.cpp) From 08b1f6fde090e37e063d1f6ab6ba813cbdc4426b Mon Sep 17 00:00:00 2001 From: Faizan Ahamed Date: Mon, 27 Apr 2020 10:21:11 +0530 Subject: [PATCH 10/11] Update binary_search.cpp --- search/binary_search.cpp | 57 +++++++++++++++++++++------------------- 1 file changed, 30 insertions(+), 27 deletions(-) diff --git a/search/binary_search.cpp b/search/binary_search.cpp index 8fc28e97b..0c78733b0 100644 --- a/search/binary_search.cpp +++ b/search/binary_search.cpp @@ -1,31 +1,34 @@ #include +//binary_search function int binary_search(int a[], int l, int r, int key) { -while (l <= r) { -int m = l + (r - l) / 2; -if (key == a[m]) -return m; -else if (key < a[m]) -r = m - 1; -else -l = m + 1; -} -return -1; -} + while (l <= r) { + int m = l + (r - l) / 2; + if (key == a[m]) + return m; + else if (key < a[m]) + r = m - 1; + else + l = m + 1; + } + return -1; + } int main(int argc, char const* argv[]) { -int n, key; -std::cout << "Enter size of array: "; -std::cin >> n; -std::cout << "Enter array elements: "; -int* a = new int[n]; -for (int i = 0; i < n; i++) { -std::cin >> a[i]; -} -std::cout << "Enter search key: "; -std::cin >> key; -int res = binary_search(a, 0, n - 1, key); -if (res != -1) -std::cout << key << " found at index " << res << std::endl; -else -std::cout << key << " not found" << std::endl; -return 0; + int n, key; + std::cout << "Enter size of array: "; + std::cin >> n; + std::cout << "Enter array elements: "; + int* a = new int[n]; +//this loop use for store value in Array + for (int i = 0; i < n; i++) { + std::cin >> a[i]; + } + std::cout << "Enter search key: "; + std::cin >> key; +//this is use for find value in given array + int res = binary_search(a, 0, n - 1, key); + if (res != -1) + std::cout << key << " found at index " << res << std::endl; + else + std::cout << key << " not found" << std::endl; + return 0; } From 948348a4d8e6d429c0b6fd32fecf54d9f5b0456f Mon Sep 17 00:00:00 2001 From: Faizan Ahamed Date: Mon, 27 Apr 2020 10:22:30 +0530 Subject: [PATCH 11/11] Update binary_search.cpp --- search/binary_search.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/search/binary_search.cpp b/search/binary_search.cpp index 0c78733b0..9933c9816 100644 --- a/search/binary_search.cpp +++ b/search/binary_search.cpp @@ -1,5 +1,5 @@ #include -//binary_search function +// binary_search function int binary_search(int a[], int l, int r, int key) { while (l <= r) { int m = l + (r - l) / 2; @@ -18,13 +18,13 @@ int main(int argc, char const* argv[]) { std::cin >> n; std::cout << "Enter array elements: "; int* a = new int[n]; -//this loop use for store value in Array +// this loop use for store value in Array for (int i = 0; i < n; i++) { std::cin >> a[i]; } std::cout << "Enter search key: "; std::cin >> key; -//this is use for find value in given array +// this is use for find value in given array int res = binary_search(a, 0, n - 1, key); if (res != -1) std::cout << key << " found at index " << res << std::endl;