wrench786

Lab task - (Array - 2)

Oct 7th, 2021
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. #define ll long long
  4. #define ull unsigned long long
  5. #define uom unordered_map
  6. #define pb push_back
  7. #define yes cout<<"YES\n"
  8. #define no cout<<"NO\n"
  9.  
  10. #define dot(x) fixed<<setprecision(x)
  11. #define wrench786 ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
  12.  
  13. #define PI (acos(-1.0))
  14. #define eps 0.00001
  15. const int LIMIT = 10000*500;
  16. const int mod = 1000000007;
  17. using namespace std;
  18.  
  19. int main(){
  20. int test_case,n,i,j;
  21. cout<<"Total Case = ";
  22. cin>>test_case;
  23.  
  24. cout<<"How many Numbers you want to insert?";
  25. cin>>n;
  26. int arr[n];
  27.  
  28. cout<<"Enter "<<n<<" array elements"<<endl;
  29. for(i=0;i<n;i++){
  30. cin>>arr[i];
  31. }
  32.  
  33. for(i=0;i<n-1;i++){
  34. for (j = 0; j < n-i-1; j++){
  35. if (arr[j] > arr[j+1]) swap(arr[j], arr[j+1]);
  36. }
  37. }
  38.  
  39. for(i=1;i<=test_case;i++){
  40. cout<<"Case:"<<i<<endl;
  41. int x;
  42. cout<<"Enter Data You want to search"<<endl;
  43. cin>>x;
  44. // Binary search
  45. int low=0,high=n-1,mid,flag=0,ans;
  46. while(low<=high){
  47. mid = (low+high)/2;
  48.  
  49. if(arr[mid]==x){
  50. flag = 1;
  51. ans = mid;
  52. break;
  53. }
  54. else if(arr[mid]>x){
  55. high = mid-1;
  56. }
  57. else{
  58. low = mid+1;
  59. }
  60. }
  61. if(flag) cout<<x<<" is found at Index "<<ans<<endl;
  62. else cout<<x<<" is not found in the Array"<<endl;
  63. }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment