binary-search

Implementing a VHDL binary search on a std_logic_vector [vhdl]

拥有回忆 提交于 2019-12-11 05:52:07
问题 I'm attempting to create synthesizable VHDL (function or procedure) for an ASIC (it must be part of the ASIC) that will look for the first '1' in a standard_logic_vector and output which vector position that '1' was in. For example, I have an 8-bit slv of "10001000" (a '1' in position 3 and 7). If I use this slv, the output should be 4 (the output is 1 based). The actual VHDL will be searching a large slv, up to 512 bits in length. I tried implementing a binary search function but I get

Binary Search with character arrays?

心不动则不痛 提交于 2019-12-11 05:19:28
问题 I'm having a little bit of trouble understanding how to do a binary search with strings. What I have as input is a sentence in the command line prompt which is argv[1], (argv[0] being the name of the executable I output). This is how it looks: ./a.out "This is my sentence." And included in my file is also a .h file that holds two arrays--a nouns array that holds certain set of nouns and a verbs array that holds a certain set of verbs. What I want to do is simply check if the verbs and nouns

Binary Search vs Ternary Search

跟風遠走 提交于 2019-12-11 03:24:48
问题 In terms of time and space complexity, is binary search better than ternary search ? 回答1: Both have constant space, but big O time for Ternary Search is Log_3 N instead of Binary Search's Log_2 N which both come out to log(N) since log_b(N) = log_x(N)/log_x(b) . In practice Ternary Search isn't used because you have to do an extra comparison at each step, which in the general case leads to more comparisons overall. 2 * Log_3(N) comparisons vs Log_2(N) comparisons. 回答2: It probably depends on

Binary Search to guess real numbers

99封情书 提交于 2019-12-11 02:55:47
问题 Considering a fairly common problem - I am thinking of an integer, can you guess it in O(log n) time given that I will answer to your guesses in "high", "low" or "that's the one!" - I have come across a problem that is a slight variation that has me stumped: I am thinking of a positive real number between 1 and N. Guess my number to within one decimal place in O(log log log N) time. I tried solving this by trying to guess 10N instead of N but that would still not give me an O(log log log N)

Stuck on a java assignment, binary search algorithm

一个人想着一个人 提交于 2019-12-11 02:38:26
问题 I have hit this point on an assignment and I was hoping for some guidance. Basically the program is supposed to have a user think of a number between 1-100 and then ask if it is higher or lower than 50. Then the program outputs the midpoint until of the range until the answer is correct. For example if 'h' was entered it would then ask if the number is 75, if the response is then 'l' it would ask if the number is 67, etc. I think I have built the framework but I am really struggling with how

How to implement dynamic binary search for search and insert operations of n element

三世轮回 提交于 2019-12-11 00:14:25
问题 The idea is to use multiple arrays, each of length 2^k, to store n elements, according to binary representation of n.Each array is sorted and different arrays are not ordered in any way. In the above mentioned data structure, SEARCH is carried out by a sequence of binary search on each array. INSERT is carried out by a sequence of merge of arrays of the same length until an empty array is reached. More Detail: Lets suppose we have a vertical array of length 2^k and to each node of that array

Binary search using iterators, why do we use “(end - begin)/2”? [duplicate]

无人久伴 提交于 2019-12-10 17:54:18
问题 This question already has answers here : Why prefer start + (end - start) / 2 over (start + end) / 2 when calculating the middle of an array? (4 answers) Closed 3 years ago . I am studying iterators and have been stuck for 3 days on figuring out why do we use: auto mid = text.begin() + (end - beg) / 2; Code: int main() { vector<int> text{ 10,9,8,7,6,5,4,3,2,1 }; int sought = 3; // text must be sorted // beg and end will denote the range we're searching auto beg = text.begin(), end = text.end(

Binary search over a huge file with unknown line length

荒凉一梦 提交于 2019-12-10 10:38:28
问题 I'm working with huge data CSV file. Each file contains milions of record , each record has a key. The records are sorted by thier key. I dont want to go over the whole file when searching for certian data. I've seen this solution : Reading Huge File in Python But it suggests that you use the same length of lines on the file - which is not supported in my case. I thought about adding a padding to each line and then keeping a fixed line length , but I'd like to know if there is a better way to

Check if a number exists in a given set of ranges

99封情书 提交于 2019-12-10 10:37:13
问题 Suppose we have a set of N ranges, (A1, B1) (A2, B2) (A3, B3) ... (An, Bn), where Ai denotes the starting point and Bi denotes the ending point of a range. (Ai, Bi are positive integers) How do we check using Binary Search if the given integer, say X, exists in at least one of the N ranges? My approach: Sort the ranges by x-coordinate first and then by y-coordinate. Find the smallest x-coordinate greater or equal to X. Check if that range is satisfied. If yes, we have a solution. Now, if that

What is Lazy Binary Search?

淺唱寂寞╮ 提交于 2019-12-09 23:59:28
问题 I don't know whether the term "Lazy" Binary Search is valid, but I was going through some old materials and I just wanted to know if anyone can explain the algorithm of a Lazy Binary Search and compare it to a non-lazy Binary Search. Let's say, we have this array of numbers: 2, 11, 13, 21, 44, 50, 69, 88 How to look for the number 11 using a Lazy Binary Search? 回答1: As far as I am aware "Lazy binary search" is just another name for "Binary search". 回答2: Justin was wrong. The common