Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 37 additions & 20 deletions CPP/algorithms/searching/binary_search.cpp
Original file line number Diff line number Diff line change
@@ -1,29 +1,46 @@
#include <iostream>
using namespace std;

// Function to perform Binary Search
#include <stdio.h>

/*
=========================================================
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() {
Expand All @@ -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;
Expand Down