in LintCode Binary Search ~ read.

Find Minimum in Rotated Sorted Array

LintCode-159.Find Minimum in Rotated Sorted Array

Suppose a sorted array is rotated at some pivot unknown to you beforehand.

(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).

Find the minimum element.

Notice

You may assume no duplicate exists in the array.

Example

Given [4, 5, 6, 7, 0, 1, 2] return 0

public class Solution {  
    /**
     * @param num: a rotated sorted array
     * @return: the minimum number in the array
     */
    public int findMin(int[] num) {
        if (num == null || num.length == 0) {
            return -1;
        }

        int low = 0, high = num.length - 1;

        if (num[low] < num[high]) {
            return num[low];
        }

        while (low + 1 < high) {
            int middle = low + (high - low) / 2;

            if (num[low] < num[middle]) {
                low = middle;
            } else {
                high = middle;
            }
        }

        return Math.min(num[low], num[high]);
    }
}

Hope this helps,
Michael