旋转数组的最小数字
#include <iostream> #include <stdio.h> using namespace std; int inorder(int a[],int index1, int index2) { int mini=index1; for(int i = index1 + 1;i <= index2; i++) if(a[mini]>a[i]) a[mini]=a[i]; return a[mini]; } int Min(int a[],int len) { int index1 = 0; int index2 = len-1; int mid = index1; while(a[index1] >= a[index2]) { if(index2 - index1 ==1) return a[index2]; mid = (index1 + index2)/2; if( a[mid] == a[index1] && a[index1] == a[index2]) return inorder(a,index1,index2); if(a[mid] > a[index1]) index1 = mid; else if(a[mid]<a[index2]) index2 = mid; } return a[mid]; } int main() { int a[5]={3,4,5,0,2}; cout << Min(a,5) << endl; return 0; }
posted on 2014-06-22 13:38 XiaoFei Wang 阅读(105) 评论(0) 编辑 收藏 举报