To move several elements to the end of an array, we call this a rotated array. Find the smallest number in the rotation array

For example: {7,8,9,1,2,3,4,5,6} this is a rotating array, minimum number 1,2,3,4,5,6,7,8,9 {1}

Analysis: If the complexity of traversal event is O(N), binary search can be used to find the time complexity of O(logN).

#include <stdio.h> int TheArray(int *a,int len) { int front,rear,mid; front=0; rear=len-1; while(a[front]>=a[rear]) { if(rear-front==1) { mid=rear; break; } mid=(front+rear)/2; if(a[mid]>=a[front]) { front=mid; }else if(a[mid]<=a[rear]) { rear=mid; } } return a[mid]; } int main() {int a[]={4,5,6,7,8,9,1,2,3}; int len=sizeof(a)/sizeof(a[0]); int ret=TheArray(a,len); printf("%d\n",ret); }Copy the code