Created
December 19, 2016 20:43
-
-
Save Zulcom/12f9fc2f97712f3b5dfed105ecde4679 to your computer and use it in GitHub Desktop.
Theory of algorithms - L3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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