From 3739f77015665f743eab495ad15fc82fe22baf24 Mon Sep 17 00:00:00 2001 From: Namita Singh <112771891+namitaa15@users.noreply.github.com> Date: Fri, 11 Jul 2025 14:31:41 +0530 Subject: [PATCH] Update and rename c++ to binary_search_c++.cpp --- Binary Search/binary_search_c++.cpp | 88 ++++++++++++++++++++++++++ Binary Search/c++ | 97 ----------------------------- 2 files changed, 88 insertions(+), 97 deletions(-) create mode 100644 Binary Search/binary_search_c++.cpp delete mode 100644 Binary Search/c++ diff --git a/Binary Search/binary_search_c++.cpp b/Binary Search/binary_search_c++.cpp new file mode 100644 index 00000000..b85b3cbc --- /dev/null +++ b/Binary Search/binary_search_c++.cpp @@ -0,0 +1,88 @@ +//Iterative Binary Search +#include +using namespace std; + +// Iterative Binary Search function +int binarySearch(int arr[], int size, int target) { + int left = 0, right = size - 1; + + while (left <= right) { + int mid = left + (right - left) / 2; + + if (arr[mid] == target) + return mid; // Found, return index + + if (arr[mid] < target) + left = mid + 1; // Move to right half + else + right = mid - 1; // Move to left half + } + return -1; // Not found +} + +int main() { + int n; + cout << "Enter the size of array: "; + cin >> n; + + int arr[n]; + cout << "Enter " << n << " elements (in ascending order): "; + for (int i = 0; i < n; ++i) + cin >> arr[i]; + + int target; + cout << "Enter element to search: "; + cin >> target; + + int result = binarySearch(arr, n, target); + + if (result != -1) + cout << "Element found at position " << (result + 1) << endl; + else + cout << "Element not found in array." << endl; + + return 0; +} + +// Recursive Binary Search +#include +using namespace std; + +// Recursive Binary Search function +int binarySearchRecursive(int arr[], int left, int right, int target) { + if (left > right) + return -1; // Not found + + int mid = left + (right - left) / 2; + + if (arr[mid] == target) + return mid; // Found + if (arr[mid] > target) + return binarySearchRecursive(arr, left, mid - 1, target); // Left half + else + return binarySearchRecursive(arr, mid + 1, right, target); // Right half +} + +int main() { + int n; + cout << "Enter the size of array: "; + cin >> n; + + int arr[n]; + cout << "Enter " << n << " elements (in ascending order): "; + for (int i = 0; i < n; ++i) + cin >> arr[i]; + + int target; + cout << "Enter element to search: "; + cin >> target; + + int result = binarySearchRecursive(arr, 0, n - 1, target); + + if (result != -1) + cout << "Element found at position " << (result + 1) << endl; + else + cout << "Element not found in array." << endl; + + return 0; +} diff --git a/Binary Search/c++ b/Binary Search/c++ deleted file mode 100644 index 28708ab0..00000000 --- a/Binary Search/c++ +++ /dev/null @@ -1,97 +0,0 @@ -//doing binary search by taking input from user -#include -using namespace std; -int main() -{ - int i, arr[10], num, first, last, middle; - cout<<"Enter 10 Elements (in ascending order): "; - for(i=0; i<10; i++) - cin>>arr[i]; - cout<<"\nEnter Element to be Search: "; - cin>>num; - first = 0; - last = 9; - middle = (first+last)/2; - while(first <= last) - { - if(arr[middle]last) - cout<<"\nThe number, "< -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[] = { 2, 3, 4, 10, 40 }; //defining array beforehand - int x = 10; - int n = sizeof(arr) / sizeof(arr[0]); - int result = binarySearch(arr, 0, n - 1, x); - (result == -1) ? cout << "Element is not present in array" : cout << "Element is present at index " << result; - return 0; -} - - - -//by iterative method - -#include -using namespace std; -int binarySearch(int arr[], int l, int r, int x) -{ - while (l <= r) { - int m = l + (r - l) / 2; - if (arr[m] == x) - return m; - if (arr[m] < x) - l = m + 1; - else - r = m - 1; - } - return -1; -} - -int main(void) -{ - int arr[] = { 2, 3, 4, 10, 40 }; - int x = 10; - int n = sizeof(arr) / sizeof(arr[0]); - int result = binarySearch(arr, 0, n - 1, x); - (result == -1) ? cout << "Element is not present in array" - : cout << "Element is present at index " << result; - return 0; -}