Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define ll long long
- #define ull unsigned long long
- #define uom unordered_map
- #define pb push_back
- #define yes cout<<"YES\n"
- #define no cout<<"NO\n"
- #define dot(x) fixed<<setprecision(x)
- #define wrench786 ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
- #define PI (acos(-1.0))
- #define eps 0.00001
- const int LIMIT = 10000*500;
- const int mod = 1000000007;
- using namespace std;
- int main(){
- int test_case,n,i,j;
- cout<<"Total Case = ";
- cin>>test_case;
- cout<<"How many Numbers you want to insert?";
- cin>>n;
- int arr[n];
- cout<<"Enter "<<n<<" array elements"<<endl;
- for(i=0;i<n;i++){
- cin>>arr[i];
- }
- for(i=0;i<n-1;i++){
- for (j = 0; j < n-i-1; j++){
- if (arr[j] > arr[j+1]) swap(arr[j], arr[j+1]);
- }
- }
- for(i=1;i<=test_case;i++){
- cout<<"Case:"<<i<<endl;
- int x;
- cout<<"Enter Data You want to search"<<endl;
- cin>>x;
- // Binary search
- int low=0,high=n-1,mid,flag=0,ans;
- while(low<=high){
- mid = (low+high)/2;
- if(arr[mid]==x){
- flag = 1;
- ans = mid;
- break;
- }
- else if(arr[mid]>x){
- high = mid-1;
- }
- else{
- low = mid+1;
- }
- }
- if(flag) cout<<x<<" is found at Index "<<ans<<endl;
- else cout<<x<<" is not found in the Array"<<endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment