# 旋转数组中的最小值 8

index1为首，index2为尾，indexMid指向中间

Number[index1]大于等于Number[index2]的条件满足时

indexMId指向的数小于index2指向的数，将index2更新为indexMid

package minNumberInRotatedArray8;

public class OriCode_MinInRotatedNumber {

public static void main(String[] args) {

//                int[] numbers = { 3, 4, 5, 1, 2 };

//                int[] numbers = { 1, 0, 1, 1, 1 };

int[] numbers={1,1,1,0,1};

int result = Min(numbers);

System.out.println(result);

}

static int Min(int[] numbers) {

int index1 = 0;

int index2 = numbers.length – 1;

int indexMid = index1;

while (numbers[index1] >= numbers[index2]) {

if (index2 – index1 == 1) {

indexMid = index2;

break;

}

indexMid = (index1 + index2) / 2;

if (numbers[index1] == numbers[index2]

&& numbers[index1] == numbers[indexMid]) {

return MinInOrder(numbers, index1, index2);

}

if (numbers[indexMid] >= numbers[index1]) {

index1 = indexMid;

} else if (numbers[indexMid] <= numbers[index2]) {

index2 = indexMid;

}

}

return numbers[indexMid];

};

static int MinInOrder(int[] numbers, int index1, int index2) {

int result = numbers[index1];

for (int i = index1 + 1; i < index2; i++) {

if (result > numbers[i]) {

result = numbers[i];

}

}

return result;

}

}

原文作者：keedor
原文地址: https://www.cnblogs.com/keedor/p/4379324.html
本文转自网络文章，转载此文章仅为分享知识，如有侵权，请联系博主进行删除。