Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- // Возвращает индекс элемента или -1, если элемента нет.
- int BinarySearch(const double* arr, int count, double element) {
- int b = 0; // Все слева от b меньше element.
- int e = count;
- while (e > b) {
- int m = (b + e) / 2;
- if (arr[m] < element) {
- b = m + 1;
- } else {
- e = m;
- }
- }
- return (b < count && arr[b] == element) ? b : -1;
- }
- int main() {
- int n = 0;
- std::cin >> n;
- double* arr = new double[n];
- for (int i = 0; i < n; ++i) {
- std::cin >> arr[i];
- }
- double k = .0;
- std::cin >> k;
- std::cout << BinarySearch(arr, n, k);
- delete[] arr;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement