[LeetCode-interview algorithm classic-Java implementation] [033-Search in Rotated Sorted Array (Search in the rotating Array)],-javarotated
[033-Search in Rotated Sorted Array (Search in Rotated Array )][LeetCode-interview algorithm classic-Java implementation] [directory indexes for all questions]Original question
Suppose a sorted array is rotated at some unknown to you beforehand.
(I. e .,0 1 2 4 5 6 7
Might become4 5 6 7 0 1 2
).
You are given a target value to search. If found in the array return its index, otherwise return-1.
You may assume no duplicate exists in the array.
Theme
Assume that an ordered array is rotated with an unknown pivot. (That is, 0 1 2 4 5 6 7 may become 4 5 6 7 0 1 2 ).
Specify a target value and search in the array. If it exists, the corresponding subscript is returned; otherwise,-1 is returned.
Suppose there are no duplicate values in the array.
Solutions
First, find the subscript corresponding to the smallest element in the array. If the subscript is 0, the entire array is ordered. If it is not, the array is divided into two ordered intervals, determine the element to be searched in the ordered interval and perform the search.
Code Implementation
Algorithm Implementation class
Public class Solution {public int search (int [] nums, int target) {if (nums! = Null & nums. length> 0) {// find the subscript int minIndex = searchMinIndex (nums, 0, nums. length-1); // the entire array is globally ordered if (minIndex = 0) {return binarySearch (nums, 0, nums. length-1, target);} // There are two partial ordered intervals, for example, 4 5 6 7 8 9 0 1 2 3 else {// returns the subscript if (nums [nums. length-1] = target) {return nums. length-1;} // target may be in the next ordered interval else if (nums [nums. length-1]> target) {return binarySearch (nums, minIndex, nums. length-1, target);} // target may be else {return binarySearch (nums, 0, minIndex-1, target) in the previous ordered interval );}}} return-1 ;} /*** Binary Search ** @ param nums array * @ param start position * @ param end position * @ param target search target * @ return match element subscript */public int binarySearch (int [] nums, int start, int end, int target) {int mid; while (start <= end) {mid = start + (end-start)> 1 ); if (nums [mid] = target) {return mid;} else if (nums [mid]> target) {end = mid-1 ;} else {start = mid + 1 ;}} return-1 ;} /*** find the subscript of the smallest element ** @ param nums array * @ param start position * @ param end position * @ return the subscript of the smallest element */public int searchMinIndex (int [] nums, int start, int end) {int mid; while (start <end) {mid = start + (end-start)> 1 ); // if (nums [mid]> nums [mid + 1]) {return mid + 1 ;} // indicates that the intermediate value is in the first ordered array. else if (nums [mid]> nums [start]) {start = mid ;} // indicates that the intermediate value is in the second ordered array. else {end = mid ;}/// indicates that the entire array is ordered return 0 ;}}
Evaluation Result
Click the image. If you do not release the image, drag it to a position. After the image is released, you can view the complete image in the new window.
Note
Please refer to the following link for more information: http://blog.csdn.net/derrantcm/article/details/47064941]
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.