旋转数组的最小数字(改进版算法)
2014-04-11 15:48
253 查看
#include<iostream> using namespace std; //directly sort if the array has repeat value int drctsort(int a[],int length) { int resu=a[0],i; for(i=0;i<length;i++) if(a[i]<resu) resu=a[i]; return resu; } //core algorithm int minvalue(int a[],int length) { int beg=0,end=length-1,mid; if(a[beg]<a[end]) return a[beg]; while(true){ if((end-beg)==1) break; mid=(beg+end)/2; if(a[beg]<a[mid]) beg=mid; else if(a[mid]<a[end]) end=mid; else return drctsort(a,length); } return a[end]; } int main() { int a[]={1,1,1,0,1}; int b[]={1,0,1,1,1}; int c[]={2,3,4,5,0,1}; int resu=minvalue(a,5); cout<<"min number:"<<resu<<endl; resu=minvalue(b,5); cout<<"min number:"<<resu<<endl; resu=minvalue(c,6); cout<<"min number:"<<resu<<endl; return 0; }
概念说明:旋转数组 : 把一个数组最开始的若干元素搬到数组末尾。
原题:输入一个递增排序数组的一个旋转,输出旋转数组的最小元素。
相关文章推荐
- 算法题目---旋转数组的最小数字
- 九度OJ 1386 旋转数组的最小数字 【算法】
- 九度OJ 1386 旋转数组的最小数字 【算法】
- 求旋转数组的最小数字算法的解析以及完整c语言代码实现
- [剑指offer]算法6 旋转数组的最小数字
- 每天一道算法题——旋转数组的最小数字
- [算法]找出旋转数组的最小数字
- 重拾算法之剑指Offier——旋转数组的最小数字
- 算法复习:旋转数组的最小数字
- 算法-旋转数组的最小数字
- 剑指offer-算法题练习:part6 旋转数组的最小数字
- 算法练习六:旋转数组的最小数字
- 【LeetCode-面试算法经典-Java实现】【153-Find Minimum in Rotated Sorted Array(找旋转数组中的最小数字)】
- 面试算法(七)旋转数组的最小数字
- 【LeetCode-面试算法经典-Java实现】【154-Find Minimum in Rotated Sorted Array II(找旋转数组中的最小数字II)】
- 《剑指Offer》算法题——“旋转数组”的最小数字
- 旋转数组的最小数字
- 旋转数组的最小数字
- 剑指Offer——旋转数组的最小数字
- 剑指offer面试题8:旋转数组的最小数字