Sabab

Binary Search

Feb 4th, 2024
912
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.41 KB | None | 0 0
  1. #include<stdio.h>
  2. int main(){
  3. int a[100]={23,29,31,37,42,43,43,65,68,73,76,89,92,99,100,105};
  4.  
  5. int start=0,end=16,mid,loc=-1;
  6. int element=42;
  7. while(start<=end){
  8. mid=(start+end)/2;
  9.  
  10. if(a[mid]==element){
  11. loc=mid;
  12. break;
  13. }
  14. else if(a[mid]<element){
  15. start=mid+1;
  16. }
  17. else{
  18.  
  19. end=mid-1;
  20. }
  21.  
  22.  
  23. }
  24.  
  25. if(loc==-1){
  26.  
  27. printf("Location not Found");
  28. }
  29. else{
  30.  
  31. printf("Location:%d",loc);
  32. }
  33.  
  34. return 0;
  35. }
  36.  
  37.  
Advertisement
Add Comment
Please, Sign In to add comment