Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- // factoriel
- /*
- int fact (int n) {
- if (n > 1) return n*fact(n-1);
- return 1;
- }
- */
- // dvoichno tyrsene
- int binSearch(int left, int right, int x, int array[]) {
- int mid = (left + right) / 2;
- if( array[mid] == x ) return mid;
- else if( left == right ) return -1;
- else {
- if( x > mid ) binSearch (mid+1, right, x, array);
- else binSearch(left, mid-1, x, array);
- }
- }
- int main() {
- int array[] = {1 ,2 ,3 ,4 ,5 ,6 ,7};
- int x = 8;
- int res = binSearch(0,7,x,array);
- cout << res << endl;
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment