From 804396f9e869ace80e27abcfdfdd76e112c75342 Mon Sep 17 00:00:00 2001 From: Pragati Gupta Date: Fri, 3 Oct 2025 22:40:15 +0530 Subject: [PATCH] Add: Binary Search in C - Implements efficient search in a sorted array using iterative binary search - Time Complexity: O(log n) - Space Complexity: O(1) --- CPP/algorithms/searching/binary_search.cpp | 57 ++++++++++++++-------- 1 file changed, 37 insertions(+), 20 deletions(-) diff --git a/CPP/algorithms/searching/binary_search.cpp b/CPP/algorithms/searching/binary_search.cpp index 8baa159f..287007a0 100644 --- a/CPP/algorithms/searching/binary_search.cpp +++ b/CPP/algorithms/searching/binary_search.cpp @@ -1,29 +1,46 @@ -#include -using namespace std; - -// Function to perform Binary Search +#include + +/* +========================================================= +Algorithm Name: Binary Search +Category: Searching +Difficulty Level: Easy (Beginner friendly) + +Algorithm Description: +Binary search is an efficient algorithm used to find the position +of a target value within a sorted array. It works by repeatedly +dividing the search interval in half. If the value of the search key +is less than the item in the middle of the interval, the algorithm +continues on the left half, otherwise on the right half. + +Time Complexity: +- Best Case: O(1) (target is at the middle) +- Average Case: O(log n) +- Worst Case: O(log n) + +Space Complexity: +- O(1) (iterative implementation uses constant extra space) +========================================================= +*/ + +// Function to perform Binary Search on a sorted array int binarySearch(int arr[], int size, int target) { - int left = 0, right = size - 1; + int left = 0; // Start of the search interval + int right = size - 1; // End of the search interval while (left <= right) { - int mid = left + (right - left) / 2; // Calculate mid index + int mid = left + (right - left) / 2; // Middle index - // Check if the target is at mid if (arr[mid] == target) { - return mid; // Return the index if found - } - - // If target is greater, ignore the left half - if (arr[mid] < target) { - left = mid + 1; - } - // If target is smaller, ignore the right half - else { - right = mid - 1; + return mid; // Target found, return its index + } else if (arr[mid] < target) { + left = mid + 1; // Ignore left half + } else { + right = mid - 1; // Ignore right half } } - return -1; // Return -1 if the target is not found + return -1; // Target not found } int main() { @@ -36,9 +53,9 @@ int main() { // Output the result if (result != -1) { - cout << "Element found at index: " << result << endl; + printf("Element found at index: %d\n", result); } else { - cout << "Element not found!" << endl; + printf("Element not found!\n"); } return 0;