Advertisement
Guest User

binary search with index

a guest
Dec 11th, 2019
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.78 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<math.h>
  3.  
  4. struct pair
  5. {
  6.     int first,second;
  7. };
  8.  
  9. struct pair ara[1000];
  10.  
  11. void sort(int n)
  12. {
  13.     for(int i=0;i<n;i++)
  14.     {
  15.         for(int j=i+1;j<n;j++)
  16.         {
  17.             if(ara[i].first>ara[i].second)
  18.             {
  19.                 struct pair temp=ara[i];
  20.                 ara[i]=ara[j];
  21.                 ara[j]=temp;
  22.             }
  23.         }
  24.     }
  25. }
  26.    
  27.  
  28. int main()
  29. {
  30.     int n;
  31.     scanf("%d",&n);
  32.    
  33.     for(int i=0;i<n;i++)
  34.     {
  35.         scanf("%d",&ara[i].first);
  36.         ara[i].second=i+1;
  37.     }
  38.    
  39.     int khujo;
  40.     scanf("%d",&khujo);
  41.    
  42.     sort(n);
  43.    
  44.     int lo=0;
  45.     int hi=n-1;
  46.     int mid;
  47.    
  48.     while(hi>=lo)
  49.     {
  50.         mid=(hi+lo)/2;
  51.         if(ara[mid].first==khujo)
  52.         {
  53.             printf("%d number jaygay pawa gese :D",ara[mid].second);
  54.             return 0;
  55.         }
  56.         else if(ara[mid].first>khujo) hi=mid-1;
  57.         else lo=mid+1;
  58.     }
  59.    
  60.     printf("pai nai :(");
  61.     return 0;
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement