From 97dbbc4707295c27ea88db20ef18c3d551a92f37 Mon Sep 17 00:00:00 2001 From: Ruksina <85981396+Ruksina01@users.noreply.github.com> Date: Sat, 12 Oct 2024 00:45:56 +0530 Subject: [PATCH] Update and rename Binary_search.cpp to Update BinarySearch.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit — Time complexity remains O(log n). — Recursive approach uses extra space: O(log n) due to call stack. — Iterative approach preferred for space optimization: O(1) space complexity. --- C++/Binary_search.cpp | 32 ---------- C++/Update BinarySearch.md | 116 +++++++++++++++++++++++++++++++++++++ 2 files changed, 116 insertions(+), 32 deletions(-) delete mode 100644 C++/Binary_search.cpp create mode 100644 C++/Update BinarySearch.md diff --git a/C++/Binary_search.cpp b/C++/Binary_search.cpp deleted file mode 100644 index 8500492..0000000 --- a/C++/Binary_search.cpp +++ /dev/null @@ -1,32 +0,0 @@ -#include -#include - -int binarySearchRecursive(const std::vector& arr, int target, int left, int right) { - if (left > right) { - return -1; // Target not found - } - - int mid = left + (right - left) / 2; - - if (arr[mid] == target) { - return mid; // Target found at index 'mid' - } else if (arr[mid] < target) { - return binarySearchRecursive(arr, target, mid + 1, right); - } else { - return binarySearchRecursive(arr, target, left, mid - 1); - } -} - -int main() { - std::vector arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; - int target = 7; - int result = binarySearchRecursive(arr, target, 0, arr.size() - 1); - - if (result != -1) { - std::cout << "Target " << target << " found at index " << result << std::endl; - } else { - std::cout << "Target " << target << " not found in the array." << std::endl; - } - - return 0; -} diff --git a/C++/Update BinarySearch.md b/C++/Update BinarySearch.md new file mode 100644 index 0000000..29dae12 --- /dev/null +++ b/C++/Update BinarySearch.md @@ -0,0 +1,116 @@ +# Binary Search Implementation + +## Overview +Binary Search is an efficient algorithm for finding an item from a sorted list of items. It works by repeatedly dividing the portion of the list that could contain the item in half until you've narrowed down the possible locations to just one. + +## Iterative Binary Search Implementation + +### C++ Code +```cpp +#include +#include +using namespace std; + +// Function to perform binary search iteratively +// Time Complexity: O(log n), Space Complexity: O(1) +int binarySearch(vector &arr, int target) +{ + int n = arr.size(); + int start = 0; + int end = n - 1; + + // Loop until the start index exceeds the end index + while (start <= end) + { + // Calculate mid index to avoid overflow + int mid = start + (end - start) / 2; + + // Check if the target is found at mid index + if (target == arr[mid]) + return mid; // Return the mid index if found + else if (target > arr[mid]) + start = mid + 1; // Search in right half + else + end = mid - 1; // Search in left half + } + return -1; // Return -1 if the target is not found +} + +int main() +{ + vector arr = {-1, 0, 3, 4, 5, 9, 12}; // Example sorted array + cout << binarySearch(arr, 3) << endl; + return 0; +} +``` +### Recursion Binary Serach Implemenation + +### C++ Code +```cpp +#include +#include +using namespace std; + +// Function to perform binary search recusrively +// Time Complexity: O(log n), Space Complexity: O(log n) +int recursiveBinarySearch(vector &arr, int target, int start, int end) +{ + // Base condition: if search range is valid + if (start <= end) + { + // Calculate the middle index (avoids overflow) + int mid = start + (end - start) / 2; + + // If target is less than mid element, search left half + if (target < arr[mid]) + { + return recursiveBinarySearch(arr, target, start, mid - 1); + } + // If target is greater than mid element, search right half + else if (target > arr[mid]) + { + return recursiveBinarySearch(arr, target, mid + 1, end); + } + // Target found at mid index + else + { + return mid; + } + } + + // Target not found in array + return -1; +} +int main() +{ + vector arr = {-1, 0, 3, 4, 5, 9, 12}; // Example sorted array + int n = arr.size(); + cout << recursiveBinarySearch(arr, 9, 0, n - 1) << endl; + return 0; +} + +``` +## Complexity Analysis + +### Iterative Binary Search +- **Time Complexity**: O(log n) + - The algorithm halves the search space with each comparison, making it efficient for large datasets. + +- **Space Complexity**: O(1) + - The iterative approach uses a constant amount of space, as it does not require additional space for function call stacks. + +### Recursive Binary Search +- **Time Complexity**: O(log n) + - Similar to the iterative version, it also reduces the search space by half with each recursive call. + +- **Space Complexity**: O(log n) + - The recursive approach utilizes the call stack, which can lead to increased memory usage for deep recursion. + +### Why Iterative is Preferred +- **Memory Efficiency**: The iterative approach uses O(1) space, making it more memory-efficient than the recursive version, which requires O(log n) space due to the call stack. + +- **Performance**: Iterative solutions generally have lower overhead than recursive solutions. Each recursive call involves function call overhead, which can slow down execution. + +- **Avoiding Stack Overflow**: The iterative approach can handle larger datasets without the risk of stack overflow errors that can occur with deep recursion in the recursive version. + +In summary, the iterative binary search is often favored in practice due to its efficiency in both time and space, making it a more robust solution for larger datasets.