binary-search

Using Collections.binarySearch() for predicate search (i.e., not complete match)

早过忘川 提交于 2019-12-11 15:15:00
问题 I have a list of timestamps sorted in ascending order: List<Instant> timestamps = ...; // note: sorted in ascending order Now, given an input timestamp Instant inputTs , I want to find an entry t in timestamps that satisfies t.isBefore(inputTs) && inputTs.isBefore(t.plusMillis(SOME_CONSTANT)) , i.e., I am simply looking for a t such that inputTs lies within the bounds of some fixed-length duration starting at t . I acknowledge that there can theoretically be multiple such t s, so the search

Array search NP complete [closed]

不羁岁月 提交于 2019-12-11 14:48:54
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 4 years ago . Given an unsorted array of size n, it's obvious that finding whether an element exists in the array takes O(n) time. If we let m = log n then it takes O(2^m) time. Notice that if the array is sorted, a binary search actually takes O(m) time (which is polynomial) but the binary search cannot apply to an unsorted

What data structure to use to have O(log n) key AND value lookup?

风格不统一 提交于 2019-12-11 12:35:24
问题 Having a sorted dict (hash table, map or whatever key/value structure) you can easily have a binary search to look for an item. If we assume the keys are unique but values could be repeated, what data structure can we use to have O(log n) retrieval for keys and also O(log n) query to find count of values=something in the given data? 回答1: Two binary search trees, one for keys, second for values, with mutual pointers will provide the required functionality. The pointers can be many-to-one from

time complexity for this algorithm with two for loops

99封情书 提交于 2019-12-11 12:16:28
问题 Can you explain me how to find time complexity for this sum1=0; for(k=1;k<=n;k*=2) for(j=1;j<n;j++) sum++ I am taking consideration of N as base, and tried calculating how many iteration happens when n=1,2,3...n But i am not able to genralize it. PLease some one explain me, thoroughly. I have even checked other queries of similar type like: saying it follows 1,2,4,8...n so it will be 2^n nlogn But i dint understand. I dont know how they say 2^n=logn . Please any properly explain how to

Binary searching NSArray without having the object to search but a condition

蓝咒 提交于 2019-12-11 11:41:54
问题 So, I have this array of objects with NSNumber s in it. I have a number of my own and I need to find a number in the array that's greater than my number by least margin. How do I employ binary search for this? The method : indexOfObject:inSortedRange:options:usingComparator accepts the object as a parameter and I don't have it. How do I do the binary search with this condition but without an object? 回答1: You need to supply an object - any object specifying the value of the item being searched

Searching a array of strings

帅比萌擦擦* 提交于 2019-12-11 10:26:59
问题 Basically the purpose of this program is to read up to 100 names from file, sort with a bubblesort, and then search for a entered name by binary search. All seems to be working except when I enter a name that is in the list, nothing happens, I'm just prompted to enter a name again. Say a name in the list in Elvis Presley. I am prompted to enter a name. I type in Elvis Presley. I SHOULD recieve Elvis Presley is your friend. Not happening. Any help appreciated. #include <iostream> #include

Can't use binary search with Object Arraylist?

≯℡__Kan透↙ 提交于 2019-12-11 10:25:14
问题 I tried using a binary search with an Arraylist and it gave me this message: The method binarySearch(List>, T) in the type Collections is not applicable for the arguments (ArrayList, String) Here's the code: ArrayList <Object> a = new ArrayList <Object> (); String date = JOptionPane.showInputDialog(null, "Please enter the date.") int index = Collections.binarySearch(a, date); The binary search should return the position of a specific date from an array (a) of several dates. What am I doing

How can I make integer division in Perl OR How can I make my binary search work?

孤人 提交于 2019-12-11 09:28:35
问题 I'm trying to implement binary search. This is my code: #!/usr/bin/perl #use strict; use warnings; @array = (1..100); $number = <STDIN>; $low = 0; $high = $#array; while($low < $high){ print "Searcing $low ---- $high \n"; $mid = $low + ($high - $low)/2; if($array[$mid] == $number){ print "Found in index:" . $mid; last; } elsif($array[$mid] < $number){ $low = $mid + 1; } else{ $high = $mid - 1; } } But it does not work although it is a straight forward implementation (at least it would be in

How to extend a binary search iterator to consume multiple targets

浪子不回头ぞ 提交于 2019-12-11 09:07:09
问题 I have a function, binary_range_search , that is called like so: my $brs_iterator = binary_range_search( target => $range, # eg. [1, 200] search => $ranges # eg. [ {start => 1, end => 1000}, ); # {start => 500, end => 1500} ] brs_iterator->() will iterate over all @$ranges on which $range overlaps. I would like to extend binary_range_search to be able to call it with multiple ranges as its target, eg: target => $target_ranges # eg. [ [1, 200], [50, 300], ... ] search => $search_ranges # as

How do I (C++ STL) binary_search for Abstract classes?

孤人 提交于 2019-12-11 06:25:13
问题 One can use the STL binary search algorithms (binary_search, upper_bound, lower_bound) to search a vector of Base pointers for a derived object, as shown below. Since Base is abstract (protected constructor), one has to instantiate a Derived object for the search functions, which is slightly ugly. I want to search the vector for the first Derived above a given time. Can I do this without arbitrarily picking and instantiating one of my many inherited classes? #include <algorithm> #include