Find the minimum value of a rotated array

Source: Internet
Author: User

Find the minimum value of the rotated array:

Suppose a sorted array rotates with an unknown element as a fulcrum, finding the smallest value in the rotated array, assuming there are no duplicate elements in the array.

such as: The original array 1,2,3,4,5,6,7 rotated to get 4,5,6,7,1,2,3. The minimum value after rotation is 1.

Problem Analysis:

Not too much to introduce here, after the rotation of the array can actually be divided into two ordered arrays, the size of the front sub-array is larger than the size of the rear face array.

The smallest element is the dividing line between two arrays.

Program implementation:

1 /***************************************2 FileName FindMinRotateArray.cpp3 Author:godfrey4 CREATEDTIME:2016/5/35 ****************************************/6#include <iostream>7#include <cstring>8#include <vector>9#include <algorithm>Ten#include <stdio.h> One#include <stdlib.h> A  - using namespacestd; -  the intFindminrotatearray (intAintsize) { -     intLow =0; -     intHigh = size-1; -     intmid; +      while(Low <High ) { -Mid = (low + high)/2; +         if(A[mid] > A[high]) {//minimum value in right half ALow = mid +1; at         } -         Else if(A[mid] < A[high]) {//minimum value in left half -High =mid; -         } -     } -     returnA[low]; in } - intMain () to { +     intA[] = {4,5,6,7,1,2,3}; -     intFindminnum = Findminrotatearray (A,sizeof(a)/sizeof(int)); thecout<<"The findminnum:"; *cout<<findminnum<<Endl; $     return 0;Panax Notoginseng}

Operation Result:

Reprint please specify the source:

C + + Blog Park: godfrey_88

http://www.cnblogs.com/gaobaoru-articles/

Find the minimum value of a rotated array

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.