Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.69 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<windows.h>
  3.  
  4.  
  5. int binarySearch(int* m,int key,int left,int right)
  6. {
  7. int middle = (left + right) / 2;
  8.  
  9. if(left > right) return 0;
  10.  
  11. if(key > m[middle]) return binarySearch(m,key,middle + 1,right);
  12. else if(key < m[middle]) return binarySearch(m,key,left,middle - 1);
  13. else return 1;
  14. }
  15.  
  16. int main()
  17. {
  18. int m[] = {1,2,3,4,5,6,7,8,9,12};
  19.  
  20.  
  21. int a = 0;
  22. int i = 0;
  23. for(;i<10;i++)
  24. {
  25. printf("%d ",m[i]);
  26. }
  27. printf("\n");
  28.  
  29. while(true)
  30. {
  31. printf("\n\nfound element : ");
  32. scanf("%i",&a);
  33. if(binarySearch(m,a,0,9)) printf("Element was found");
  34. else printf("Element was not found");
  35. }
  36. //printf("%d",a);
  37.  
  38.  
  39.  
  40. return 0;
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement