-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp18PositionUsingArrayBinarySearch.cpp
More file actions
52 lines (41 loc) · 1.25 KB
/
p18PositionUsingArrayBinarySearch.cpp
File metadata and controls
52 lines (41 loc) · 1.25 KB
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
43
44
45
46
47
48
49
50
51
52
/*Write a Program to find the position of an element in an array using Binary search Algorithm*/
#include <iostream>
using namespace std;
int binarySearch(int arr[], int size, int target)
{
int left = 0;
int right = size - 1;
while (left <= right)
{
int mid = left + (right - left) / 2;
// If the target is present at mid
if (arr[mid] == target)
return mid; // Return the index of the element
// 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 -1; // Return -1 if the element is not found
}
int main()
{
cout << "ABHISHEK SINGH 2315272/2435222";
int arr[] = {2, 5, 8, 12, 15, 18, 23, 35, 42, 56}; // Example sorted array
int size = sizeof(arr) / sizeof(arr[0]); // Calculate the size of the array
int target;
cout << "Enter the element to search for: ";
cin >> target;
int result = binarySearch(arr, size, target);
if (result != -1)
{
cout << "Element found at index: " << result << endl;
}
else
{
cout << "Element not found in the array!" << endl;
}
return 0;
}