binary-search

Right Threading a Binary Tree

I'm having a hell of a time trying to figure this one out. Everywhere I look, I seem to be only running into explanations on how to actually traverse through the list non-recursively (the part I actually understand). Can anyone out there hammer in how exactly I can go through the list initially and find the actual predecessor/successor n...

solving this NullPointerException on java's binarySearch

I'm solving sphere's online judge shortest path problem. This bit of code is giving me trouble: int sourceIndex = Arrays.binarySearch(citiesIds,source); int destinationIndex= Arrays.binarySearch(citiesIds, destination); double [] distancesFromSource = g.distancesFrom(sourceIndex); int destinationDistance = (int)distancesFromSource[de...

Linq and Binary Search - Improve this slow Where statement?

I've got two collections, each containing about 40,000 items. The elements in list 2 are linked to the elements of list one via a foreign key. For each element of list one, I want to find the corresponding element in list two. Something like this: foreach(var item in list1) { var match = list2.Where(child => child.ID == item.ChildI...

In Python, find item in list of dicts, using bisect

I have a list of dicts, something like this: test_data = [ { 'offset':0, 'data':1500 }, { 'offset':1270, 'data':120 }, { 'offset':2117, 'data':30 }, { 'offset':4055, 'data':30000 }, ] The dict items are sorted in the list according to the 'offset' data. The real data could be much longer. What I want to do is lookup a...

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

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 ...

Arrays.binarySearch doesnt work like it should

I have string array [1, 2, 3] and i search for all of those numbers using Arrays.binarySearch, it find 1 and 2, but with 3 it returns -1. any idea why it works that way? what is better alternative to always working search in array/collection? ...

Find a missing 32bit integer among a unsorted array containing at most 4 billion ints

Hi, This is the problem described in Programming pearls. I can not understand binary search method descrbied by the author. Can any one helps to elaborate? Thanks. EDIT: I can understand binary search in general. I just can not understand how to apply binary search in this special case. How to decide the missing number is in or not in...

What's the most efficient way to compare two blocks of memory?

I need a comparison function for blocks of memory for doing binary searches on arrays of bytes in the D programming language. It does not need to have any useful semantics. It only needs to be fast and be a valid comparison function (one that produces a total ordering). The blocks of memory to be compared are already known to be the s...

C# Array.BinarySearch Problem

Can anyone explain why this is happening? ie. Even when 175 is present in the array at location 7, the array.binarysearch is returning a negative value? Please see this image: ...

Fastest method for running a binary search on a file in C?

For example, let's say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste of time to copy the entire file into an array and then run binary search...I've effectively made it a linear time algorithm, because I'll ha...

Binary search in C with recursive function accepting only length

I am solving "Programming Pearls" exercises. 4.11 say: Write and prove the correctness of a recursive binary search function in C or C++ with this declaration: int binarysearch(DataType x[], int n); Use this function alone; do not call any other recursive function. I came up with: int bsearch_rec_array_only(int key...

Can LINQ use binary search when the collection is ordered?

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. ...

Java: Code Review: Binary Search

I wrote this for self education. How does it look? (It passes all my junit tests.) /** * find needle in haystack * @param haystack a sorted list * @param needle * @return i such that <code>haystack[i] == needle</code>, or -1 if it is not found */ public static int find(char[] haystack, char needle) { int minWindow = 0; // lower ...

Working on a binary search tree

I have insert working, and i have preorder,inorder,and post order working. I am working on retrieveItem where i try to find using a key. Thats when i realize that in my driver file(main()) that my class instance holds no information.( data aData) So, that makes it impossible to do the retrieve. Down in main() i have data aData; Th...

Excel Find Speed vs. VBA binary Search?

How good/fast is Excel VBA's Find vs. binary search? My platform is Office 11|2003 and I'll be searching for strings against Column A on three sheets of values. Total number of rows ~140,000 If worth it which Library & functions should I reference to do the sorting and then the binary search? Binary searching strings/text reportedly ...

How to write a binary algorithm for C/C++

I am having trouble to write the binary algorithm in C/C++. My question is like that: Apply binary algorithm to search for a number from 1 to 100 in a number guessing game. The user will respond with 'y' for a correct guess, 'h' if the guess is too high or 'l' if the guess is too low. I don't have any idea to apply it. Can someone ju...

Binary search on unsorted arrays?

I came across this document Binary Search Revisited where the authors have proved/explained that binary search can be used for unsorted arrays (lists) as well. I haven't grokked much of the document on a first reading. Have any of you already explored this ? ...

Why isn't Collections.binarySearch() working with this comparable?

I have this Player class which implements the Comparable interface. Then I have an ArrayList of Players. I'm trying to use binarySearch() on the list of Players to find one Player, but Java is giving me a "cannot find symbol: method binarySearch(java.util.ArrayList< Player>,Player)". This the Player class: class Player implements Compa...

C# Binary Search Variation

The list is sorted. I have a List and I d like to do binary search on it. T has members like StartIndex, EndIndex etc. I can do binary search on the list with StartIndex, ie: I have implemented IComparable for this. I need to twist this a little as the following: I want to find a StartIndex that might be OffBy a small value. For exam...

C# Binary Search on 2 indexes

Hello, I have an object with attributes ; startIndex, endIndex I am able to do binary search based on startIndex by implementing the following : int IComparable.CompareTo(object obj) { Repeat r = (Repeat)obj; return this.startIndex.CompareTo(r.startIndex); } However with the same Repea...