Adrita

binary search recursive ( ds lab 1) 3rd sem

Jan 7th, 2020
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.54 KB | None | 0 0
  1. #include<stdio.h>
  2. int binary(int l,int r,int ar[],int n)
  3. {
  4. int m=(l+r)/2;
  5. if(l>r)
  6. return -1;
  7. else if(ar[m]==n)
  8. return m;
  9. else if(n>ar[m])
  10. binary(m+1,r,ar,n);
  11. else if(n<ar[m])
  12. binary(l,m-1,ar,n);
  13. }
  14. int main()
  15. {
  16. int a,arr[100],i,n;
  17. for(i=0;;i++)
  18. {
  19. scanf("%d",&a);
  20. if(a>=0)
  21. arr[i]=a;
  22. else
  23. break;
  24. }
  25. scanf("%d",&n);
  26. int l,r;
  27. l=0,r=i-1;
  28. int res=binary(l,r,arr,n);
  29. printf("%d",res);
  30. }
Advertisement
Add Comment
Please, Sign In to add comment