# 旋转数组的二分查找

1、什么是旋转数组

2、旋转数组的二分查找之找到给定key

``````public int rotateArraySearch(int[] a, int key) {
if (a == null)
return -1;
int low = 0, high = a.length - 1;
while (low <= high) {
int mid = low + ((high - low) >> 1);
if (a[mid] == key)
return mid;
if (a[mid] > a[low]) { // 表示左边有序
if (key >= a[low] && key < a[mid]) {
high = mid - 1;
} else {
low = mid + 1;
}
} else { // 表示右边有序
if (key >= a[mid] && key < a[high]) {
high = mid - 1;
} else {
low = mid + 1;
}
}
}
return -1;
}``````

3、旋转数组的二分查找之找出最小值

``````public int rotateArrayMin(int[] a){
if(a==null){
System.out.print("The array is null\n");
System.exit(0);
}
int low=0,high=a.length-1;
int mid=low;
while(a[low]>=a[high]){
if(high-low==1){
mid=high;
break;
}
mid=low+((high-low)>>1);
if(a[low]==a[high]&&a[low]==a[mid]){
int result=getMinByOrder(a);
return result;
}
if(a[mid]>=a[low]){
low=mid;
}else{
high=mid;
}
}

return a[mid];
}

private int getMinByOrder(int[] a) {
// TODO Auto-generated method stub
int min=a[0];
int len=a.length;
for(int i=1;i<len;i++){
if(a[i]<min){
min=a[i];
}
}
return min;
}``````