Advertisement
Guest User

Binary Search

a guest
Nov 17th, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.73 KB | None | 0 0
  1. #include<stdio.h>
  2. int main()
  3. {
  4. int arr[8], i, l=0, r=7, mid, key, f=0;
  5.  
  6. for(i=0; i<8; i++)
  7. {
  8. scanf("%d", &arr[i]);
  9. }
  10.  
  11. printf("Given Key: ");
  12. scanf("%d", &key);
  13.  
  14. for(i=0; i<8; i++)
  15. {
  16.  
  17. while(l<=r)
  18. {
  19. mid=(l+r)/2;
  20.  
  21. if(arr[mid]==key)
  22. {
  23. printf("%d at found %d\n", key, i+1);
  24. f=1;
  25. break;
  26. }
  27.  
  28. else if(arr[mid]>key)
  29. {
  30. r=mid-1;
  31. }
  32. else if(arr[mid]<key)
  33. {
  34. l=mid+1;
  35. }
  36.  
  37. }
  38. }
  39. if(f==0)
  40. {
  41. printf("Not Found\n");
  42. }
  43. return 0;
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement