-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary_searching.cpp
More file actions
53 lines (51 loc) · 1.03 KB
/
Copy pathbinary_searching.cpp
File metadata and controls
53 lines (51 loc) · 1.03 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
53
#include<iostream>
#define max 6
using namespace std;
int main()
{
int n, temp, i, data, beg=0, end=5, mid;
cout << "How many you want to enter?\n";
cin >> n;
int arr[n];
cout << "Enter the element: ";
for (i = 0; i < n; i++)
{
cin >> arr[i];
}
for (i = 0; i < n - 1; i++)
{
for (int j = i + 1; j < n; j++)
{
if (arr[i] > arr[j])
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
cout<<"Which number you want to search? ";
cin>> data;
mid=(beg+end)/2;
if(data<arr[mid]){
end = mid-1;
}
else if(data>arr[mid]){
beg = mid+1;
}
while(beg<=end && arr[mid]!=data){
if(data<arr[mid]){
end = mid-1;
}
else if(data>arr[mid]){
beg = mid+1;
}
mid=(beg+end)/2;
}
if(arr[mid]==data){
cout<<"Data is found.";
}
else{
cout<<"Data not found.";
}
}