privateintminSequentialSearch(int[] array){ int indexMin = 0; for (int i = 1; i < array.length; i++) { if (array[i] < array[indexMin]) { indexMin = i; } }
return array[indexMin]; } }
time complexity
The time complexity is logarithmic level $ O (logN) $.
Solution 2 [Java]: time complexity $ O (n / 2) $
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
publicclass{ publicintminNumberInRotateArray(int[] array){ if (array.length == 0) { return0; }
int min = array[0];
for (int i = 1; i < array.length; ++i) { if (array[i] < min) { return array[i]; } }
Return min; } }
Thinking
After the rotation, from left to right comparison of two adjacent numbers will always find a set of numbers, the first number is greater than the second number, the second number is the minimum. Because the second number is the number of all subsequent rotation of the operation to move the last of a series of numbers.