diff --git a/C++/Linear_search.cpp b/C++/Linear_search.cpp new file mode 100644 index 0000000..970ef14 --- /dev/null +++ b/C++/Linear_search.cpp @@ -0,0 +1,46 @@ +/* ****Yash_mittal**** */ + +#include +#define ll long long +#define pb push_back +#define endl '\n' +#define pii pair +#define vi vector +#define all(a) (a).begin(),(a).end() +#define F first +#define S second +#define sz(x) (ll int)x.size() +#define hell 1000000007 +#define rep(i,a,b) for(ll int i=a;i>n>>x; + int arr[n]; + for(int i=0;i>arr[i]; + int fl=0; + for(int i=0;i>num; first = 0; last = count-1; - middle = (first+last)/2; + //middle = (first+last)/2; this will go out of range for big values of first and last + middle = first + (last-first)/2; while (first <= last) { if(arr[middle] < num) @@ -32,7 +33,7 @@ int main() else { last = middle - 1; } - middle = (first + last)/2; + middle = first + (last-first)/2; } if(first > last) { diff --git a/C++/binarysearch.cpp b/C++/binarysearch.cpp index ecce212..a8a2614 100644 --- a/C++/binarysearch.cpp +++ b/C++/binarysearch.cpp @@ -18,7 +18,8 @@ int main() cin>>t; l=0,h=n-1; while((a[l]!=t)&&(a[h]!=t)){ - c=(l+h)/2; + //c=(l+h)/2; c will go out of range for big value of l and h + c = l + (h-l)/2; if(a[c] +#define ll long long +#define pb push_back +#define endl '\n' +#define pii pair +#define vi vector +#define all(a) (a).begin(),(a).end() +#define F first +#define S second +#define sz(x) (ll int)x.size() +#define hell 1000000007 +#define rep(i,a,b) for(ll int i=a;i>n>>x; + int arr[n]; + for(int i=0;i>arr[i]; + int fl=0; + for(int i=0;i