SHARE
TWEET

Untitled

a guest Oct 14th, 2019 72 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2.  
  3. int BS(int arr[], int l, int h, int key)
  4. {
  5.     while (l <= h) {
  6.         int m = (l + h) / 2;
  7.         if (arr[m] == key)
  8.             return m;
  9.  
  10.         if (arr[m] < key)
  11.             l = m + 1;
  12.  
  13.         else
  14.             h = m - 1;
  15.     }
  16.  
  17.     return -1;
  18. }
  19. int main()
  20. {
  21.     int arr[100],n, i, key;
  22.     scanf("%d", &n);
  23.  
  24.     for (i = 0; i < n; i++)
  25.     {
  26.         scanf("%d", &arr[i]);
  27.     }
  28.  
  29.     scanf("%d", &key);
  30.  
  31.      int pos = BS(arr, 0, n-1, key);
  32.  
  33.     if(pos == -1){
  34.         printf("Not Found!\n");
  35.     }
  36.     else{
  37.         printf("%d Found at %d\n", key, pos);
  38.     }
  39.  
  40.  
  41.     return 0;
  42. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top