binary-search

Safe integer middle value formula

我与影子孤独终老i 提交于 2019-11-29 04:23:14
I am looking for an efficient formula working in Java which calculates the following expression: (low + high) / 2 which is used for binary search. So far, I have been using "low + (high - low) / 2" and "high - (high - low) / 2" to avoid overflow and underflows in some cases, but not both. Now I am looking for an efficient way to do this, which would for for any integer (assuming integers range from -MAX_INT - 1 to MAX_INT). UPDATE : Combining the answers from Jander and Peter G. and experimenting a while I got the following formulas for middle value element and its immediate neighbors: Lowest

First occurrence in a binary search

﹥>﹥吖頭↗ 提交于 2019-11-29 00:48:07
问题 I'm tinkering with some code and I realized something I never knew. A normal binary search will return a random index in a data set for a key that occurs more than once. How can I modify this code below to return the first occurrence ? Is this something people do? //ripped from the JDK public static int binarySearchValue(InvertedContainer.InvertedIndex[] a, long key) { return bSearchVal(a, 0, a.length, key); } private static int bSearchVal(InvertedContainer.InvertedIndex[] a, int fromIndex,

binary search in an array in Perl

僤鯓⒐⒋嵵緔 提交于 2019-11-28 20:51:15
I have an array of hex numbers, and I need to go over other numbers and check if they appear in that array. Right now i'm using a foreach loop that goes over the entire array each time. Is there a way to make it faster by sorting the array at first, and then implementing binary search on it. The code at the moment: sub is_bad_str{ my ($str, @keys) = @_; my $flag = 0; my ($key, $hex_num); if ($str =~ m/14'h([0-9a-f][0-9a-f][0-9a-f][0-9a-f])/;){ #'# fixes bad highlighting $hex_num = $1; } if (defined $hex_num){ foreach $key (@keys){ if ($hex_num =~ /\Q$key\E/i){ $flag = 1; last; } } } if (($flag

Why is Binary Search a divide and conquer algorithm?

五迷三道 提交于 2019-11-28 20:38:18
I was asked if a Binary Search is a divide and conquer algorithm at an exam. My answer was yes, because you divided the problem into smaller subproblems, until you reached your result. But the examinators asked where the conquer part in it was, which I was unable to answer. They also disapproved that it actually was a divide and conquer algorithm. But everywhere I go on the web, it says that it is, so I would like to know why, and where the conquer part of it is? Kenci The book Data Structures and Algorithm Analysis in Java, 2nd edtition, Mark Allen Weiss Says that a D&C algorithm should have

Difference between basic binary search for upper bound and lower bound?

冷暖自知 提交于 2019-11-28 20:36:32
问题 In the article http://community.topcoder.com/tc?module=Static&d1=tutorials&d2=binarySearch, the author discusses binary search. He makes a distinction between finding the lowest value where something is true, and the highest value where something is false. The array being searched looks something like: false false false true true I am curious as to why these two cases are different. Why can't you just find the lowest value which is true, then subtract one to find the highest value which is

Where is binary search used in practice?

橙三吉。 提交于 2019-11-28 19:12:41
Every programmer is taught that binary search is a good, fast way to search an ordered list of data. There are many toy textbook examples of using binary search, but what about in real programming: where is binary search actually used in real-life programs? Binary search is used everywhere . Take any sorted collection from any language library (Java, .NET, C++ STL and so on) and they all will use (or have the option to use) binary search to find values. While true that you have to implement it rarely, you still have to understand the principles behind it to take advantage of it. Binary search

Why in Java (high + low) / 2 is wrong but (high + low) >>> 1 is not?

家住魔仙堡 提交于 2019-11-28 19:00:56
问题 I understand the >>> fixes the overflow: when adding two big positive longs you may endup with a negative number. Can someone explain how this bitwise shift magically fixes the overflow problem? And how it is different than >> ? My suspicious: I think it has to do with the fact that Java uses two-compliments so the overflow is the right number if we had the extra space but because we don't it becomes negative. So when you shift and paddle with zero it magically gets fixed due to the two

findInterval() with right-closed intervals

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-28 18:21:56
The great findInterval() function in R uses left-closed sub-intervals in its vec argument, as shown in its docs: if i <- findInterval(x,v) , we have v[i[j]] <= x[j] < v[i[j] + 1] If I want right-closed sub-intervals, what are my options? The best I've come up with is this: findInterval.rightClosed <- function(x, vec, ...) { fi <- findInterval(x, vec, ...) fi - (x==vec[fi]) } Another one also works: findInterval.rightClosed2 <- function(x, vec, ...) { length(vec) - findInterval(-x, -rev(vec), ...) } Here's a little test: x <- c(3, 6, 7, 7, 29, 37, 52) vec <- c(2, 5, 6, 35) findInterval(x, vec)

Binary search to find the rotation point in a rotated sorted list

依然范特西╮ 提交于 2019-11-28 17:42:08
问题 I have a sorted list which is rotated and would like to do a binary search on that list to find the minimum element. Lets suppose initial list is {1,2,3,4,5,6,7,8} rotated list can be like {5,6,7,8,1,2,3,4} Normal binary search doesn't work in this case. Any idea how to do this. -- Edit I have one another condition. What if the list is not sorted?? 回答1: A slight modification on the binary search algorithm is all you need; here's the solution in complete runnable Java (see Serg's answer for

Implementation of C lower_bound

五迷三道 提交于 2019-11-28 16:53:49
Based on the following definition found here Returns an iterator pointing to the first element in the sorted range [first,last) which does not compare less than value. The comparison is done using either operator< for the first version, or comp for the second. What would be the C equivalent implementation of lower_bound(). I understand that it would be a modification of binary search, but can't seem to quite pinpoint to exact implementation. int lower_bound(int a[], int lowIndex, int upperIndex, int e); Sample Case: int a[]= {2,2, 2, 7 }; lower_bound(a, 0, 1,2) would return 0 --> upperIndex is