Skip to content

Instantly share code, notes, and snippets.

@Zulcom
Created December 19, 2016 20:43
Show Gist options
  • Save Zulcom/12f9fc2f97712f3b5dfed105ecde4679 to your computer and use it in GitHub Desktop.
Save Zulcom/12f9fc2f97712f3b5dfed105ecde4679 to your computer and use it in GitHub Desktop.
Theory of algorithms - L3
#include <fstream>
#include <iostream>
#include <algorithm>
using namespace std;
int binarySearch(int* arr, int value, int left, int right) {
while (left <= right)
{
int middle = (left + right) / 2;
if (arr[middle] == value) return middle;
if (arr[middle] > value) right = middle - 1;
else left = middle + 1;
}
return -1;
}
int main() {
int n;
std::ifstream fin("Massiv.txt");
fin >> n;
int* buff = new int[n];
if (!fin.is_open()) cout << "File not opened";
for (int i = 0; i < n; i++) fin >> buff[i];
sort(buff, buff + n);
for (int i = 0; i < n; i++) cout << buff[i] << " ";
cout << binarySearch(buff, 476, 0, n);
system("pause");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment