英文:
how to do binary search on array which is the numbers on even indexes are ascending and the numbers on odd indexes are descending
问题
在这种情况下,我认为您应该分别对偶数索引和奇数索引进行二分查找。
英文:
how to do binary search on array which is the numbers on even indexes are ascending and the numbers on odd indexes are descending example the array {-3,10,0,9,5,0,7,-1} and i want to find a number : x=5
i think i should do binary search on even indexes alone, and on odd indexes alone
答案1
得分: 2
你可以使用两个二分搜索,一个用于增加的值,另一个用于减少的值。会有一些简单的修改:
- 边界不再是
[0, n-1]
(其中n
是数组的长度)。对于递减的值和递增的值,开头和结尾的限制将不同。 - 不再调整
low = mid + 1
或high = mid - 1
,而是使用low = mid + 2
和high = mid - 2
,以确保中间索引的奇偶性在搜索过程中不改变。
由于这是一个简单的任务,我将留给读者自己来确定正式的算法。
英文:
You can use two binary searches, one for the increasing and decreasing values each. There will be a few simple modifications:
- The bounds will not be
[0, n-1]
(wheren
is the length of array). The opening and closing limit will be different for the descending and ascending values respectively. - Instead of adjusting
low = mid + 1
orhigh = mid - 1
, you'll uselow = mid + 2
andhigh = mid - 2
so that the parity of the middle index doesn't change during the search.
Since this is a simple task, I'll leave the formal algorithm for the reader to figure out.
答案2
得分: 1
你说得对,你可以执行两个单独的二分搜索。一个在
(-3,0,5,0,7)
另一个在
(-1,0,9,10).
至于实现,你可以使用标准算法,并且
-
用它们的双倍替换所有的索引(
i
变成2i
)进行第一个搜索; -
对于第二个搜索,用
m-2i
替换所有的索引,其中m
是n
或者是奇数的n-1
。
你不能使用单个搜索来完成,将偶数索引替换为 2i
,将奇数索引替换为 m-2i
,因为不能保证这会对应一个递增序列(例如 (-3,-1,0,0,5,9,7,10)
)。需要进行预先合并,需要线性时间。
英文:
You are right, you can perform two separate binary searches. One on
(-3,0,5,0,7)
and the other on
(-1,0,9,10).
As regards the implementation, you can use a standard algorithm and
-
replace all indexes by their double (
i
becomes2i
) for the first search; -
replace all indexes by
m-2i
wherem
isn
orn-1
, whichever is odd for the second search.
You cannot do with a single search, replacing the even indexes by 2i
and the odd ones by m-2i
because there is no guarantee that this would correspond to a growing sequence (e.g. (-3,-1,0,0,5,9,7,10)
). A preliminary merge is required, taking linear time.
答案3
得分: 0
// 时间复杂度 O(log(n))
public static int find(int[] a, int x) {
int bottom = 0, top = 0, mid = 0;
if (a.length % 2 == 0) {
top = a.length - 2;
} else {
top = a.length - 1;
}
while (bottom <= top) {
mid = (bottom + top) / 2;
mid -= mid % 2; // 向下取整到最接近的偶数索引
if (a[mid] > x) {
top = mid - 2;
} else if (a[mid] < x) {
bottom = mid + 2;
} else {
return mid;
}
}
bottom = 1;
top = 0;
mid = 0;
if (a.length % 2 == 0) {
top = a.length - 1;
} else {
top = a.length - 2;
}
while (bottom <= top) {
mid = (bottom + top) / 2;
mid -= (mid + 1) % 2; // 向下取整到最接近的奇数索引
if (a[mid] < x) {
top = mid - 2;
} else if (a[mid] > x) {
bottom = mid + 2;
} else {
return mid;
}
}
return -1;
}
英文:
//time complexity O(log(n))
public static int find(int[] a, int x) {
int bottom = 0 , top = 0 , mid=0;
if(a.length % 2 == 0 ) {
top = a.length - 2;
}
else
{
top = a.length - 1;
}
while (bottom <= top ) {
mid = (bottom + top) / 2;
mid -= mid % 2;//round down to the nearest even index
if (a[mid] > x) {
top = mid - 2;
}
else if (a[mid] < x) {
bottom = mid + 2;
}
else {
return mid;
}
}
bottom = 1 ; top=0; mid=0;
if(a.length % 2 == 0 ) {
top = a.length - 1;
}
else
{
top = a.length - 2;
}
while (bottom <= top ) {
mid = (bottom + top) / 2;
mid -= (mid + 1) % 2;// round down to the nearest odd index
if (a[mid] < x)
{
top = mid - 2;
}
else if (a[mid] > x) {
bottom = mid + 2;
}
else {
return mid;
}
}
return -1;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论