-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarySearch.cpp
More file actions
41 lines (33 loc) · 829 Bytes
/
BinarySearch.cpp
File metadata and controls
41 lines (33 loc) · 829 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
#include<iostream.h>
#include<conio.h>
main()
{
int c, first, last, middle, n, search, array[100];
cout<<"Enter number of elements\n";
cin>>n;
cout<<"Enter Elements\n";
for ( c = 0 ; c < n ; c++ )
cin>>array[c];
cout<<"Enter value to find\n";
cin>>search;
first = 0;
last = n - 1;
middle = (first+last)/2;
while( first <= last )
{
if ( array[middle] < search )
first = middle + 1;
else if ( array[middle] == search )
{
cout<<search<<" Found at location "<<middle+1;
break;
}
else
last = middle - 1;
middle = (first + last)/2;
}
if ( first > last )
cout<<"Not found! "<<search<<" is not present in the list.\n";
getch();
return 0;
}