-
Notifications
You must be signed in to change notification settings - Fork 0
/
binarysearch.cpp
60 lines (56 loc) · 1.32 KB
/
binarysearch.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>
using namespace std;
int binarysearch(int arr[], int left, int right, int n)
{
while (left <= right)
{
int mid = left + (right - left) / 2;
if (arr[mid] == n)
return mid;
else if (n < arr[mid])
{
right = mid - 1;
}
else
{
left = mid + 1;
}
}
return -1;
}
int main()
{
int arr[10], num;
cout << "Enter the elements of the array:" << endl;
for (int i = 0; i < 10; i++)
{
cin >> arr[i];
}
cout << "Enter the value whose position you want to check:" << endl;
cin >> num;
int output = binarysearch(arr, 0, 9, num);
if (output == -1)
{
cout << "Cannot find the position of this value" << endl;
}
else
{
int rem;
rem = (output + 1);
if (rem == 1)
{
cout << "The element is in " << output + 1 << "st position." << endl;
}
else if (rem == 3)
{
cout << "The element is in " << output + 1 << "rd position." << endl;
}
else if (rem == 2)
cout << "The element is in " << output + 1 << "nd position." << endl;
else
{
cout << "The element is in " << output + 1 << "th position." << endl;
}
}
return 0;
}