The smallest number//title of the rotated array: Moves the first number of elements of an array to the end of the array, which we call the rotation of the array. Enter a rotation of an incrementally sorted array, outputting the smallest element in the rotated array. For example: the array {3,4,5,1,2} is a rotation of {1,2,3,4,5}, and the minimum element is 1. #include <stdio.h> #include <assert.h>int min_equ (int *src, int left, int. right) {int i = 0;int ret = Src[left]; ASSERT (src! = NULL); for (; I < right; ++i) {if (ret >src[i]) return src[i];} return ret;} int find_a_num (int *src, int len) {int left = 0;int right = Len-1;int mid = Left;assert (src! = NULL); while (Src[left] = Src[right]) {if (Right-left = = 1) {mid = Right;break;} Mid = (left + right)/2;if (Src[left] = [Src[right] && src[mid] = = Src[right]) {return min_equ (src, left, right);} if (Src[mid]>src[left]) {left = mid;} if (Src[mid]<src[right]) {right = mid;}} return src[mid];} int main () {int src[] = {3, 4, 5, 1, 2};int dst[] = {1, 0, 1, 1, 1};int len = sizeof (SRC)/sizeof (src[0]); int len1 = s Izeof (DST)/sizeof (dst[0]);p rintf ("%d\n", Find_a_num (SRC, len));p rintf ("%d\n", Find_a_num (DST, len1)); return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
"C language" Enter a rotation of an incrementally sorted array, outputting the smallest element in the rotated array