-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinary search algorithm.cpp
42 lines (35 loc) · 1.13 KB
/
binary search algorithm.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
using namespace std;
// Function to perform binary search
int binarySearch(int arr[], int n, int target) {
int left = 0, right = n - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (arr[mid] == target)
return mid; // Target found, return index
if (arr[mid] < target)
left = mid + 1; // Search the right half
else
right = mid - 1; // Search the left half
}
return -1; // Target not found
}
int main() {
int n, target;
cout << "Enter the number of elements in the array: ";
cin >> n;
int arr[n];
cout << "Enter the elements of the sorted array:" << endl;
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
cout << "Enter the target value to search: ";
cin >> target;
int result = binarySearch(arr, n, target);
if (result != -1) {
cout << "Target value " << target << " found at index: " << result << endl;
} else {
cout << "Target value " << target << " is not present in the array." << endl;
}
return 0;
}