旋转数组之元素查找
转载请注明作者与出处: franciscolv http://www.cnblogs.com/franciscolv/archive/2011/11/21/2257536.html
折半算法的改进 O(lgn)
/**
* 折半
* @param a
* @param k
* @return
*/
public static int findK0(int[] a, int k) {
if (a == null)
return -1;
int l = 0, r = a.length;
while (l + 1 < r) {
int m = l + (r - l) / 2;
if (a[m] >= a[l]) {
if (k < a[m]) {
if (k >= a[l])
r = m;
else
l = m;
} else {
l = m;
}
} else {
if (k >= a[m]) {
if (k <= a[r - 1])
l = m;
else
r = m;
} else {
r = m;
}
}
}
return a[l] == k ? l : -1;
}