how to do binary search on array which is the numbers on even indexes are ascending and the numbers on odd indexes are descending

huangapple go评论69阅读模式
英文:

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

你可以使用两个二分搜索,一个用于增加的值,另一个用于减少的值。会有一些简单的修改:

  1. 边界不再是 [0, n-1](其中 n 是数组的长度)。对于递减的值和递增的值,开头和结尾的限制将不同。
  2. 不再调整 low = mid + 1high = mid - 1,而是使用 low = mid + 2high = mid - 2,以确保中间索引的奇偶性在搜索过程中不改变。

由于这是一个简单的任务,我将留给读者自己来确定正式的算法。

英文:

You can use two binary searches, one for the increasing and decreasing values each. There will be a few simple modifications:

  1. The bounds will not be [0, n-1] (where n is the length of array). The opening and closing limit will be different for the descending and ascending values respectively.
  2. Instead of adjusting low = mid + 1 or high = mid - 1, you'll use low = mid + 2 and high = 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 替换所有的索引,其中 mn 或者是奇数的 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 becomes 2i) for the first search;

  • replace all indexes by m-2i where m is n or n-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 &lt;= top ) {
mid = (bottom + top) / 2;
mid -= mid % 2;//round down to the nearest even index
if (a[mid] &gt; x) {
top = mid - 2;
}
else if (a[mid] &lt; 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 &lt;= top ) {
mid = (bottom + top) / 2;
mid -= (mid + 1) % 2;// round down to the nearest odd index
if (a[mid] &lt; x)
{
top = mid - 2;
}
else if (a[mid] &gt; x) {
bottom = mid + 2;
}
else {
return mid;
}
}
return -1;
}

huangapple
  • 本文由 发表于 2023年1月9日 10:56:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/75052793.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定