//二分法
int minArray(int* numbers, int numbersSize){
int left = 0;
int right = numbersSize - 1;
while (left < right) {
int mid = (left + right) / 2;
if (numbers[mid] > numbers[right]) {
left = mid + 1;
}
else if (numbers[mid] == numbers[right] && numbers[mid] == numbers[left]) {
left += 1;
right -= 1;
}
else {
right = mid;
}
}
return numbers[left];
}
//前后齐头并进
int minArray1(int* numbers, int numbersSize){
int ans = numbers[0];
int left = 0;
int right = numbersSize - 1;
int mid = (numbersSize + 1) / 2;
while (left != mid) {
if (ans > numbers[left]) {
ans = numbers[left];
}
if (ans > numbers[right]) {
ans = numbers[right];
}
left++;
right--;
}
return ans;
}
网友评论