Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- using namespace std;
- int BinarySearch(vector<int> tablica, int szukana) {
- int l = 0, p = tablica.size() - 1, sr = tablica.size()/2;
- while (tablica[sr]!=szukana) {
- if (tablica[sr] < szukana) {
- l = sr + 1;
- }
- else {
- p = sr - 1;
- }
- sr = (p - l) / 2 + l;
- if (l > p)
- return -1;
- }
- return sr;
- }
- int main()
- {
- vector<int> c = {1,2,3,5,6,7,8,9,10,11,12};
- /*for (int i = 0; i <= 12; i++) {
- cout <<i<<" "<< BinarySearch(c, i) << endl;
- }*/
- cout << BinarySearch(c, 8);
- }
Advertisement
Add Comment
Please, Sign In to add comment