Guest User

Untitled

a guest
Oct 22nd, 2019
96
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. using namespace std;
  5.  
  6. int BinSearch (int arr[], int left, int right, int key)
  7. {
  8. int mid = 0;
  9. while (true)
  10. {
  11. mid = (left + right) / 2;
  12. if (key < arr[mid])
  13. right = mid - 1;
  14. else if (key > arr[mid])
  15. left = mid + 1;
  16. else
  17. return mid;
  18.  
  19. if (left > right)
  20. return -1;
  21. }
  22. }
  23.  
  24. int main() {
  25. ifstream fin ("binsearch.in");
  26. ofstream fout ("binsearch.out");
  27. int n;
  28. fin >> n;
  29. int a[n];
  30. for (int i = 0; i < n; i++)
  31. fin >> a[i];
  32.  
  33. int m;
  34. fin >> m;
  35.  
  36. for (int i = 0; i < m; i++) {
  37. int tmp;
  38. fin >> tmp;
  39. fout << BinSearch(arr[], 0, m, tmp) << endl;
  40. }
  41.  
  42. fin.close();
  43. fout.close();
  44. return 0;
  45.  
  46. }
RAW Paste Data