/* 搜索旋转排序数组--二分查找。 */ #include<stdio.h> #include<malloc.h> #include<string.h> #include<stdlib.h> #include<math.h> #include<string.h> #include <iostream> int search(int* nums, int numsSize, int target){ if(numsSize<=0){ return -1; } int left=0,right=numsSize-1,mid; while(left<=right){ mid=(left+right)/2; if(nums[mid]==target){ return mid; } if(nums[left]<=nums[mid]){ if(nums[left]<=target&&target<=nums[mid]){ right=mid-1; }else{ left=mid+1; } }else{ if(nums[mid]<=target&&target<=nums[right]){ left=mid+1; }else{ right=mid-1; } } } return -1; } int main() { int nums[]={4,5,6,7,0,1,2},target=0; int rs=search(nums,7,target); printf("%d ",rs); return 0; }