binary-search

Python binary search-like function to find first number in sorted list greater than a specific value

I'm trying to write a function in Python that finds the first number in a sorted list greater than a specific value that I pass in as an argument. I've found examples online that use simple list comprehensions to achieve this, but for my purposes I need to be performing this operation frequently and on large lists, so a search that runs...

Python binary search always returns target not found value

I've written the following code to do a binary search for a value, target, in a list or tuple, collection. def binary(collection, target): """Binary search Takes a sorted list or tuple, collection, then searches for target Returns -1 if item isn't found. """ length = len(collection) minimum = 0 maximum = length -...

Super fast autocomplete using binary search in sorted file (300000 lines)

In my Android app I want to have an input field with autocomplete. The number of items will be about 300000. The best solution seems to be to put the items into a file (on sdcard), one item per line, each line would have the same number of characters so that I can seek to specific line number. If the user enters something in the text fie...

Binary Search to Compute Square root (Java)

I need help writing a program that uses binary search to recursively compute a square root (rounded down to the nearest integer) of an input non-negative integer. This is what I have so far: import java.util.Scanner; public class Sqrt { public static void main(String[] args) { Scanner console = new Scanner(System.in); Sys...

How to convert linear search to binary search?

- This is my find() method using Binary Search algorithm: It works just as you would expect it to. No problems at all. public int find(long searchKey) { int lowerBound = 0; int upperBound = nElems - 1; int currentIndex; while(true) { currentIndex = (lowerBound + upperBound) / 2; if(a[currentIndex] == searchKey) retu...

How to do binary search in a std::multiset without constructing a key_type object?

I have a container like this: // Sort functor struct SortByTime : std::binary_function<const TimeSortableData &, const TimeSortableData &, bool> { bool operator()(const TimeSortableData & a, const TimeSortableData & b) const { return a.GetTime() < b.GetTime(); } }; // Container that sorts by time typedef std::mult...

Binary search on C++ string does not work.

What is wrong with the following code? How come it does not found the letter using my implementation of a binary search? #include <iostream> #include <string> #include <algorithm> #include <cctype> #include <cwctype> using namespace std; bool contains(string s, char a){ int m = 0; int n = s.length()-1; while (m != n) { int k...

How can I implement binary search in Perl?

I would like to implement a binary search algorithm in Perl. My 'array' is sorted in decreasing order (not an actual array, but a function that gets an index and returns values). the problem is that there might be stretches of identical values. If my searched value is in such a stretch, I want to return the first index that contains it. ...

Efficiency of Algorithm

This is a direct quote from the textbook, Invitation to Computer Science by G.Michael Scneider and Judith L.Gersting. At the end of Section 3.4.2, we talked about the tradeoff between using sequential search on an unsorted list as opposed to sorting the list and then using binary search. If the list size is n=100,000 about how many ...

Problems with binary search function.

Having trouble with the binary_search function listed at the top. not sure where to go with it. I'm not very familiar with binary searching. #include <iostream> #include <cstdlib> #include <fstream> using namespace std; void get_input(ifstream& fin, int a[], int size, int & array_size); void binary_search (int a[], int & array_size) ...

How to write Objective-C Blocks inline?

I am trying to implement a binary search using objective-c blocks. I am using the function indexOfObject:inSortedRange:options:usingComparator:. Here is an example. // A pile of data. NSUInteger amount = 900000; // A number to search for. NSNumber* number = [NSNumber numberWithInt:724242]; // Create some array. NSMutableArray* array = ...

Is the following array well defined for binary search?

Hello, I have a file with consisted of int;int values in each line. Both columns are ascending, row by row. I plan to load that file into an array with following code: while( ! feof($f) ) { $line = fgets( $f, 32 ); $tmp = explode( ";", $line ); $elements[] = array( $tmp[0] => $tmp[1] ); } I intend to use this array to do ...