java实现——008旋转数组的最小数字

时间:2021-11-14 11:17:09
 public class T008 {
public static void main(String[] args) {
int[] num = { 3, 4, 5, 1, 2 };
System.out.println(min(num, 5));
}
public static int min(int num[], int len) {
if (num == null || len <= 0) {
System.out.println("invalid");
}
int in1 = 0;
int in2 = len - 1;
int inm = in1;
while (num[in1] >= num[in2]) {
if (in2 - in1 == 1) {
inm = in2;
break;
}
inm = (in2 + in1) / 2;
if (num[in1] == num[inm] && num[in1] == num[in2])
return minInOrder(num, in1, in2);
if (num[inm] >= num[in1]) {
in1 = inm;
} else {
in2 = inm;
}
}
return num[inm];
} public static int minInOrder(int num[], int in1, int in2) {
int result = num[in1];
for (int i = in1 + 1; i <= in2; i++) {
if (num[i] < result)
result = num[i];
}
return result;
}
}