The second sections 11 offer prove safety questions: smallest number of rotation of the array (JAVA version)

Topic: the beginning of an array of several elements moved to the end of the array, the array we call rotation. Enter a sorted array an incremental rotation of the output rotary smallest element of the array. For example, an array {3,4,5,1,2} {1,2,3,4,5} is a rotation of the array to a minimum.

Problem-solving ideas:
1, violent solution, once traversed from start to finish, we can find the smallest element, complexity is O (n), but did not take advantage of characteristics of the rotating array of input, and certainly not meet the requirements of the interviewer .
2, binary search, where the array may be ordered as two sub-arrays, binary search is very effective in an ordered array, complexity is O (logn).

Specific analysis:
1, to analyze the characteristics of the array, the two ordered arrays, all the previous values is greater than a value of the array after the array is equal to the minimum value should appear on a first element in the array.
2, reduced binary search to find the comparison value and the range depends on the intermediate values to be searched. Providing two pointers, index1 points to the first element, index2 points to the last element. If the intermediate element pointed indexMid element index1 point greater than or equal, then, after a certain smallest element indexMid. If the element pointed indexMid index2 equal to or less than the element pointed to, the smallest element is necessarily before indexMid indexMid or pointed element. This judgment is the use of conditions to continue to narrow down your search.
3, the end condition:
The last element will point to a final pre index1 subarray, the first element will point index2 a subarray, the distance between them is 1. And it is the smallest element index2 points.
4, special circumstances:
if the number is rotated to 0, that is, there is only one array in ascending order, this time there is no need to find, because the first element is the smallest element.
There is another situation often encountered in the look, the situation is to have the same elements. For example {1, 0, 1, 1, 1} is {0, 1, 1, 1, 1} is a rotation. index1, index2, element values are directed indexMid 1, this minimum judgment time is not located, it is necessary only to traverse sequentially.

code show as below:

public  class Solution {
     public  static Integer min ( int [] Array) {
         IF (Array == null || be array.length == 0 ) {
             return  null ; 
        } 
        
        int Low = 0 ;
         int High-be array.length =. 1 ;
         int Low = MID; // if this is in itself an increasing array 
        
        // If into this loop is described an array of rotating 
        the while (array [Low]> = array [High]) {
             IF (High-Low ==. 1 ) { 
                MID = High ;
                 BREAK; 
            } 
            
            MID = (High + Low) / 2 ;
             // , then the sequential search only if high, low, mid point to the same element, the right array repeated the digital 
            IF (Array [MID]> = Array [Low ]) { 
                Low = MID; 
            } the else  IF (Array [MID] <= Array [High]) { 
                High = MID; 
            } 
        } 
        return Array [MID]; 
    } 
    // sequential search 
    public  static  int minInOrder ( int [] Array) {
         int min = Array [0 ];
        for (int i = 1; i < array.length; i++) {
            if (array[i] < min) {
                min = array[i];
            }
        }
        return min;
    }
    public static void main(String[] args) {
        int[] array = {3,4,5,1,2};
        Integer result = min(array);
        System.out.println(result);
    }
}

 

Guess you like

Origin www.cnblogs.com/xhlwjy/p/11259566.html