binary-search

Why is Binary Search a divide and conquer algorithm?

杀马特。学长 韩版系。学妹 提交于 2019-11-27 13:00:38
问题 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? 回答1: The book Data Structures

The possible number of binary search trees that can be created with N keys is given by the Nth catalan number. Why?

自古美人都是妖i 提交于 2019-11-27 11:49:58
This has been bothering me for a while. I know that given N keys to arrange in the form of a binary search tree, the possible number of trees that can be created correspond to the Nth number from the Catalan sequence . I have been trying to determine why this is; unable to find anything that might even attempt to explain it intuitively I resort to the collective knowledge of SO. I found other ways to calculate the number of possible trees, but they seemed less intuitive and no explanation was offered beyond how to use it. Plus the wiki page (that link above) even shows an image of the possible

findInterval() with right-closed intervals

怎甘沉沦 提交于 2019-11-27 11:16:33
问题 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),

Which is faster, Hash lookup or Binary search?

元气小坏坏 提交于 2019-11-27 10:29:16
When given a static set of objects (static in the sense that once loaded it seldom if ever changes) into which repeated concurrent lookups are needed with optimal performance, which is better, a HashMap or an array with a binary search using some custom comparator? Is the answer a function of object or struct type? Hash and/or Equal function performance? Hash uniqueness? List size? Hashset size/set size? The size of the set that I'm looking at can be anywhere from 500k to 10m - incase that information is useful. While I'm looking for a C# answer, I think the true mathematical answer lies not

Binary search in a sorted (memory-mapped ?) file in Java

落花浮王杯 提交于 2019-11-27 10:28:17
I am struggling to port a Perl program to Java, and learning Java as I go. A central component of the original program is a Perl module that does string prefix lookups in a +500 GB sorted text file using binary search (essentially, "seek" to a byte offset in the middle of the file, backtrack to nearest newline, compare line prefix with the search string, "seek" to half/double that byte offset, repeat until found...) I have experimented with several database solutions but found that nothing beats this in sheer lookup speed with data sets of this size. Do you know of any existing Java library

Implementation of C lower_bound

泪湿孤枕 提交于 2019-11-27 09:47:10
问题 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

What are the pitfalls in implementing binary search?

耗尽温柔 提交于 2019-11-27 09:06:30
问题 Binary search is harder to implement than it looks. "Although the basic idea of binary search is comparatively straightforward, the details can be surprisingly tricky…" — Donald Knuth. Which bugs are most likely to be introduced into a new binary search implementation? 回答1: Here are some I can think of: Off-by-one errors , when determining the boundary of the next interval Handling of duplicate items , if you are suppose to return the first equal item in the array but instead returned a

Binary search algorithm in python

自古美人都是妖i 提交于 2019-11-27 07:54:24
I am trying to implement the binary search in python and have written it as follows. However, I can't make it stop whenever needle_element is larger than the largest element in the array. Can you help? Thanks. def binary_search(array, needle_element): mid = (len(array)) / 2 if not len(array): raise "Error" if needle_element == array[mid]: return mid elif needle_element > array[mid]: return mid + binary_search(array[mid:],needle_element) elif needle_element < array[mid]: return binary_search(array[:mid],needle_element) else: raise "Error" In the case that needle_element > array[mid] , you

find an element in a sorted matrix [duplicate]

北城余情 提交于 2019-11-27 07:08:18
This question already has an answer here: How do I search for a number in a 2d array sorted left to right and top to bottom? 19 answers Problem: Given a matrix in which each row and each column is sorted, write a method to find an element in it. It is a classic interview question, here is my solution boolean F(int[][] matrix, int hs, int he, int ws, int we) { if (hs > he || ws > we) return false; int m = (hs + he) / 2; int n = (ws + we) / 2; if (matrix[m][n] == t) { return true; } else if (matrix[m][n] < t) { // find the ele in the same row, right to [m][n] F(m, m, n + 1, we); // find the ele

Can LINQ use binary search when the collection is ordered?

浪尽此生 提交于 2019-11-27 07:00:40
Can I somehow "instruct" LINQ to use binary search when the collection that I'm trying to search is ordered. I'm using an ObservableCollection<T> , populated with ordered data, and I'm trying to use Enumerable.First(<Predicate>) . In my predicate, I'm filtering by the value of the field my collection's sorted by. As far as I know, it's not possible with the built-in methods. However it would be relatively easy to write an extension method that would allow you to write something like that : var item = myCollection.BinarySearch(i => i.Id, 42); (assuming, of course, that you collection implements