-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary_search.cpp
More file actions
44 lines (37 loc) · 934 Bytes
/
binary_search.cpp
File metadata and controls
44 lines (37 loc) · 934 Bytes
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
#include <iostream>
//#include <cstdlib>
using namespace std;
int main()
{
int arr[11] = {12, 23, 23, 34, 45, 55, 72, 93, 107, 157, 212};
cout << "Enter a number you want to search" << endl;
/*int first = 0, num, last = (sizeof(arr))/(sizeof(*arr)), mid;
cin >> num;
//cout << first << last;
do
{
mid = (first+last)/2;
//cout << "Number found at position " << arr[mid];
if (arr[mid]==num)
{
cout << "Number found at position " << mid;
break;
}
else if (arr[mid]>num)
last = mid-1;
else
first = mid +1;
}while (first<=last); */
int num;
cin >> num;
int len = (sizeof(arr))/(sizeof(*arr));
for (int i =0; i<len; i++)
{
if (arr[i] == num)
{
cout << "Found in position: " << i+1;
break;
}
}
return 0;
}