binary-search

Binary Search in Python

Is there a library function that performs binary search on a list/tuple and return the position of the item if found and 'False' (-1, None, etc.) if not? I found the functions bisect_left/right in the bisect module, but they still return a position even if the item is not in the list. That's perfectly fine for their intended usage, but ...

Binary Search in Array

How would I implement a binary search using just an array? ...

Binary search optimization in c?

I am writing a key record look up where the I have an index between the key and the rec number. This is sorted on the key. Is there away to do this better that what I have for speed optimization? typedef struct { char key[MAX_KEYLEN]; int rec; } KeyRecPair; typedef struct { KeyRecPair *map; int numRecs; } Key...

Which is faster, Hash lookup or Binary search?

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

What function in the std library is there to binary search a vector and find an element?

I've got a node struct struct Node{CString text, int id;}; in a sorted vector. I'm wondering if there's a function in algorithm that will do a binary search of the vector and find an element. ...

Where can I get a "useful" C++ binary search algorithm?

I need a binary search algorithm that is compatible with the C++ standard containers, something like std::binary_search in the standard library's <algorithm> header, but unlike std::binary_search, I need it to return the iterator that points at the result, not a simple boolean telling me if the element exists (On a side note, what the ...

What is the O time in determining if a value is in a sorted array?

I have a sorted array of 5000 integers. How fast can I tell if a random integer is a member of the array? An answer in general, C and Ruby would be nice. The array values are of the form c*c+1 where c can be any integer from 1 to 5000 i.e. [2, 5, 10, 17, 26, 37, 50 ...] ...

What are the pitfalls in implementing binary search?

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

Find subset totals for huge data set

Hi all, 1st of all: I'm not a programmer, never learnt programming/algorithms. Actually I have to program, mostly in awk, or ruby, some bash. In today's task, I have a huge data set (float numbers) in a plain text file, one record/line, and a sum of all numbers of the set, but the sum is wrong, because some of the numbers (can be only...

Where is binary search used in practice?

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 for a line in a text file using Javascript

Is there a way to do a disk-based binary search for a particular key in a text file in Javascript? The text file is too big to be loaded into memory, but sorted by the key values. In particular I am looking for a way to mimic Perl's Search::Dict functionality in Javascript. For e.g. If I have a file foo.txt: a 1 b 10 c 5 z 4 look(c,f...

What is the difference between Linear search and Binary search?

What is the difference between Linear search and Binary search? ...

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

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

Is there a built-in method to binary search through the entries in the .NET EventLog.Entries collection?

I am developing a log parsing service that captures specific security events in the Windows Event Log. My initial thought was to use Microsoft's LogParser, but I am not looking for any functionality beyond selecting specific Instance/Event IDs already known in advance. After some benchmarking, I found that iterating over the entire .NE...

Debugging and Binary Search

"Programming Pearls" in the column 2 ("AHA! Algorithm") talks about how binary search aids in various processes like sorting, tree traversals. But it mentions that binary seach can be used in "program debugging". Can somebody please explain how this is done? ...

Ruby on Rails, ActiveRecord, Binary search.

If I had the following table. create_table :my_table, :id => false do |t| t.string :key_column t.string :value_column end How would I ensure that the rows are optimaly stored for binary search by the field of :key? And how would I make sure binary search is used? ...

Implement binary search in objects

Is there any way to implement binary search in a ArrayList with objects? In this example the ArrayList will be sorted with the field 'id'. class User{ public int id; public string name; } ArrayList<User> users = new ArrayList<User>(); sortById(users); int id = 66 User searchuser = getUserById(users,id); How would the "User getUse...

How to perform a binary search on IList<T>?

Simple question - given an IList<T> how do you perform a binary search without writing the method yourself and without copying the data to a type with build-in binary search support. My current status is the following. List<T>.BinarySearch() is not a member of IList<T> There is no equivalent of the ArrayList.Adapter() method for List<T...

Fast average without division

I have a binary search loop which gets hit many times in the execution path. A profiler shows that the division part of the search (finding the middle index given the high and low indices of the search range) is actually the most costly part of the search, by a factor of about 4. (I think) it is not critical for efficient binary search...

writing a Comparator for a compound object for binary searching

I have a class, and list of instances, that looks something like this (field names changed to protect the innocent/proprietary): public class Bloat { public long timeInMilliseconds; public long spaceInBytes; public long costInPennies; } public class BloatProducer { final private List<Bloat> bloatList = new ArrayList<Blo...