The binary search idea is to find the minimum value in an ordered array.

Source: Internet
Author: User

Thought: The biggest feature of a circular ordered array is that when binary search is used, one side is always ordered. With this feature, value is used to store the historical minimum value.

When the order on the left is obtained, use a [low] to compare with the value to obtain the minimum value on the current left, and then jump to the right to see if there is a smaller value;

If the order is on the right, compare a [Mid] with value to get the minimum value on the right, and then jump to the left to check whether there are smaller values.

The algorithm complexity is O (logn)

The Code is as follows:

# Include <iostream> # include <stdio. h> # include <assert. h> using namespace STD; int binary_search (int * a, int size) {assert (! = NULL); int low = 0; int value = A [0]; // initialize int high = size-1; while (low <= high) {int mid = (low + high)/2; if (a [low] <= A [Mid]) // left ordered, note the use of '=' {if (a [low] <value) // compare with the minimum value of the Order on the left {value = A [low]; Low = Mid + 1; // jump to the right and try to find the minimum value} else low = Mid + 1;} else // right ordered {if (a [Mid] <value) // compare with the minimum ordered value on the right {value = A [Mid]; high = mid-1; // jump to the left and try to find the minimum value} elsehigh = mid-1 ;}} return Value;} int main () {int A [10] = {,}; cout <binary_search (A, 10 ); return 0 ;}


The binary search idea is to find the minimum value in an ordered 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.