-
Notifications
You must be signed in to change notification settings - Fork 0
/
Binarysearch.cpp
112 lines (101 loc) · 1.72 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
Parmar Fenil Milankumar
Information & Technology
Vishwakarma Goverment Engineering College
*/
#include<iostream>
#include<vector>
#include<bits/stdc++.h>
using namespace std;
int binarysearch(vector<int> &v,int i,int j,int n,int x)
{
if(x>v[n-1] || x<v[0])
{
cout<<"ERROR: Number not found in given array!"<<endl;
return 0;
}
int mid=(i+j)/2;
if(v[mid] == x)
{
cout<<"Found X at "<<mid<<" Position "<<endl;
}
else
{
if(x<v[mid])
{
j=mid;
binarysearch(v,i,j,n,x);
}
else
{
i=mid+1;
binarysearch(v,i,j,n,x);
}
}
cout<<"Loop!"<<endl;
return 0;
}
int linearsearch(vector<int> &v,int n,int x)
{
for(int i=1;i<n;i++)
{
if(x==v[i])
{
cout<<"Number Found At Position:"<<i<<endl;
return 0;
}
}
cout<<"ERROR:Value not found:";
}
main()
{
vector<int> v;
int n,i;
cout<<"Enter Number:";
cin>>n;
for(i=0;i<n;i++)
{
int x1;
cout<<i<<")=";
cin>>x1;
v.push_back(x1);
}
// sort(v.begin(),v.end());
// for(i=0;i<n;i++)
// {
// cout<<v[i]<<" ";
// }
// binarysearch(v,0,n,n,x);
int option;
cout<<"(1) For Binarysearch \n (2) For Linearsearch \n (3)To see your array \n (4) To Exit"<<endl;
cout<<"Enter YOur Option:";
cin>>option;
int x;
cout<<"Enter the number you want to search: ";
cin>>x;
switch(option)
{
case 1:
sort(v.begin(),v.end());
cout<<"Sorted Array ";
for(i=0;i<n;i++)
{
cout<<v[i]<<" ";
}
binarysearch(v,0,n,n,x);
break;
case 2:
linearsearch(v,n,x);
break;
case 3:
for(i=0;i<n;i++)
{
cout<<v[i]<<" ";
}
break;
case 4:
exit(0);
default:
cout<<"Enter Correct Option!";
}
}