Find a specific value in the rotated ordered array

Source: Internet
Author: User

A rotating array with no repeated elements is required (the original array corresponding to the element is ordered), and the subscript of the given element in the rotating array is obtained (-1 is returned if the element does not exist ). For example, an ordered array is {, 7}, and its rotation array is {, 6, 7, 2 }. Element 6 is in the rotating array. element 2 and element 3 are not in the rotating array. The result is-1 analyzed and traversed, which can be easily done. The time complexity is O (n ), this is certainly not the optimal solution because it is obtained by rotating an ordered array. Ordered, instinctively reflected by binary search. For example, we can see that at least one of the two segments in the middle is ordered (either on the left or on the right ), then binary search can be used in the ordered range; if the ordered Range is no longer used, the second half will be searched. Copy the code int search (int A [], int n, int target) {int beg = 0; int end = n-1; while (beg <= end) {int mid = beg + (end-beg)/2; if (A [mid] = target) return mid; if (A [beg] <= A [mid]) {if (A [beg] <= target & target <A [mid]) end = mid-1; else beg = mid + 1 ;} else {if (A [mid] <target & target <= A [end]) beg = mid + 1; else end = mid-1 ;}} return-1 ;} copying code extensions involves elements that are not repeated. Now, with a slight extension, there can be duplicate elements. Complex elements. Other requirements remain unchanged. The general idea is the same as the original one, this is to compare the relationship between A [beg] And A [mid]. A [beg] <A [mid] ---- ordered A [beg]> A [mid] on the left ---- ordered A on the right [beg] = A [mid] ---- ++ beg copy the code bool search (int A [], int n, int target) {int beg = 0; int end = n-1; while (beg <= end) {int mid = beg + (end-beg)/2; if (A [mid] = target) return true; if (A [beg] <A [mid]) {if (A [beg] <= target & target <A [mid]) end = mid-1; else beg = mid + 1 ;} else if (A [beg]> A [mid]) {if (A [mid] <target & target <= A [end]) beg = mid + 1; else end = mid-1;} else ++ beg;} return false ;}

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.