Skip to content
This repository was archived by the owner on May 30, 2021. It is now read-only.

Update binarysearch.cpp #97

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions C++/Linear_search.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/* ****Yash_mittal**** */

#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define endl '\n'
#define pii pair<ll int,ll int>
#define vi vector<ll int>
#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<b;i++)
#define lbnd lower_bound
#define ubnd upper_bound
#define bs binary_search
#define mp make_pair
using namespace std;

#define N 100005

int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n,x;
cin>>n>>x;
int arr[n];
for(int i=0;i<n;i++)
cin>>arr[i];
int fl=0;
for(int i=0;i<n;i++)
{
if(arr[i]==x)
{
cout<<i;
fl=1;
break;
}
}
if(fl==0)
cout<<"Not found";
return 0;
}
5 changes: 3 additions & 2 deletions C++/binary_search.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ int main()
cin>>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)
Expand All @@ -32,7 +33,7 @@ int main()
else {
last = middle - 1;
}
middle = (first + last)/2;
middle = first + (last-first)/2;
}
if(first > last)
{
Expand Down
3 changes: 2 additions & 1 deletion C++/binarysearch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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]<t){
l=c;
}
Expand Down
Binary file added C++/linear_search
Binary file not shown.
46 changes: 46 additions & 0 deletions C++/linear_search.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/* ****Yash_mittal**** */

#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define endl '\n'
#define pii pair<ll int,ll int>
#define vi vector<ll int>
#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<b;i++)
#define lbnd lower_bound
#define ubnd upper_bound
#define bs binary_search
#define mp make_pair
using namespace std;

#define N 100005

int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n,x;
cin>>n>>x;
int arr[n];
for(int i=0;i<n;i++)
cin>>arr[i];
int fl=0;
for(int i=0;i<n;i++)
{
if(arr[i]==x)
{
cout<<i;
fl=1;
break;
}
}
if(fl==0)
cout<<"Not found";
return 0;
}