简体   繁体   English

如何对数组进行二进制搜索,即偶数索引上的数字是升序的,奇数索引上的数字是降序的

[英]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如何对数组进行二进制搜索,偶数索引上的数字是升序的,奇数索引上的数字是降序的,例如数组 {-3,10,0,9,5,0,7,-1} 我想找一个数字:x=5

i think i should do binary search on even indexes alone, and on odd indexes alone我想我应该单独对偶数索引和奇数索引进行二进制搜索

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).边界不会是[0, n-1] (其中n是数组的长度)。 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.您将使用low = mid + 2high = mid - 2而不是调整low = mid + 1high = mid - 1 ,这样中间索引的奇偶性在搜索过程中不会改变。

Since this is a simple task, I'll leave the formal algorithm for the reader to figure out.由于这是一项简单的任务,我将把正式的算法留给读者去弄清楚。

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;将所有索引替换为第一次搜索的双精度( i变为2i );

  • replace all indexes by m-2i where m is n or n-1 , whichever is odd for the second search.m-2i替换所有索引,其中mnn-1 ,以第二次搜索的奇数为准。


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 (eg (-3,-1,0,0,5,9,7,10) ).您不能通过单个搜索来完成,用2i替换偶数索引,用m-2i替换奇数索引,因为不能保证这会对应于一个增长的序列(例如(-3,-1,0,0,5,9,7,10) )。 A preliminary merge is required, taking linear time.需要进行初步合并,这需要线性时间。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 你如何按降序和升序对奇数和偶数数组进行排序? - How do you sort odd and even array numbers in descending and ascending order? 如何在不使用集合的情况下按升序打印偶数和按降序打印奇数 - How to print even numbers in ascending order and odd numbers in descending order without using collection 通过偶数递增和奇数递减的数组进行二进制搜索? - Binary search through an array of even numbers increasing and odd numbers decreasing? 按升序对所有偶数进行排序,然后按集合中的降序对所有奇数进行排序 - Sort all even numbers in ascending order and then sort all odd numbers in descending order in a collection 从二维数组返回包含最大偶数个数的行索引 - From Two-Dimensional array return row indexes which contains the biggest number of even numbers 以奇数索引反转队列中的数字 - Reverse numbers in a queue at odd-indexes 对偶数和奇数数组中的奇数进行排序 - Sort odd numbers in Array of even and odd numbers 如何以降序对数组进行二进制搜索? - How to binary search in an array which is in an descending order? 我只想按升序对数组中的奇数进行排序,并将偶数留在它们在 java 中的原始位置 - I want to sort only odd numbers in an array in ascending order and leaving even numbers at their orignal place in java 如何在同一个数组中分离偶数和奇数? - How to seperate even and odd numbers in the same array?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM