forked from Hawstein/cracking-the-coding-interview
-
Notifications
You must be signed in to change notification settings - Fork 0
/
9.3.cpp
33 lines (32 loc) · 761 Bytes
/
9.3.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
using namespace std;
int search(int a[], int low, int high, int x){
while(low <= high){
int mid = low + (high - low)/2;
if(a[mid] == x) return mid;
if(a[mid] >= a[low]) {
if(x<a[mid] && x>=a[low])
high = mid - 1;
else
low = mid + 1;
}
else {
if(x>a[mid] && x<a[low])
low = mid + 1;
else
high = mid - 1;
}
}
return -1;
}
int main(){
int a[12] = {
15, 16, 19, 20, 25, 1, 3, 4, 5, 7, 10, 14
};
int b[19] = {
2,2,2,2,2,2,2,2,3,2,2,2,2,2,2,2,2,2,2
};
cout<<search(a, 0, 11, 3)<<endl;
cout<<search(b, 0, 18, 3)<<endl;
return 0;
}