binary-search

Determining if a BigInteger is Prime in Java

旧时模样 提交于 2019-12-25 07:21:15
问题 I am trying hands on validation of whether a BigInteger number entered is a Prime Number or not! But, it is running fine for smaller numbers like 13,31 ,but it yields error in the case of 15 ;by declaring it as a Prime. I am unable to figure-out the mistake,probably it is hidden in the squareroot() method approach involving binary-search ! Please view the code and help me point out the mistake!!! Calling code :- boolean p=prime(BigInteger.valueOf(15)); System.out.println("P="+p); Called code

Finding the closest value to n, without going over, in a sorted array

空扰寡人 提交于 2019-12-25 05:36:08
问题 For this problem I have a sorted Array of doubles. I need to be able to quickly and efficiently find index of the value that is closest to, but not over, n. Efficiency is key, the assignment states is can be done in O(log n) so I assume it can be done with some sort of modified binary search. I know this has been asked before but all answers I found either assumed an unsorted array or simply looped through the entire array comparing differences. Any guidance is appreciated. Thank you. 回答1:

Does binary search have logarithmic performance of deque C++ data structure?

隐身守侯 提交于 2019-12-24 11:06:38
问题 The standard says that std::binary_search(...) and the two related functions std::lower_bound(...) and std::upper_bound(...) are O(log n) if the data structure has random access. So, given that, I presume that these algorithms have O(log n) performance on std::deque (assuming its contents are kept sorted by the user). However, it seems that the internal representation of std::deque is tricky (it's broken into chunks), so I was wondering: does the requirement of O(log n) search hold for std:

triplet sum using binary search

筅森魡賤 提交于 2019-12-24 02:30:41
问题 I was thinking of various approaches for finding the triplet sum and I came across this finding a triplet having a given sum. So I thought of giving it a try. My algorithm: 1) Sort the numbers //O(nlogn) 2) Initialize low=0 and high=size-1 3) loop till low<high a) if sum-arr[high]-arr[low]< 0 , then decrement high b) if third number is not in range of (low,high) then break the loop c) else search for third number using binary search But I am not getting correct output. I don't know where my

binary search in python weird behavior

烈酒焚心 提交于 2019-12-23 21:17:07
问题 Please look at this code: def chop(array, search): lo = 0 high = len(array) - 1 while lo <= high: mid = (high + lo) /2 if array[mid] == search: return 'true' elif search > array[mid]: low = mid + 1 else: high = mid - 1 return 'false' if __name__ == '__main__': a = [1,2,3,4,5,6,7,8,9,10] print chop(a, 3) I wrote this little script which is supposed to search for number in array - regular binary search. So I run the script, and for example when I put in chop(a, 1) I get true, when I put in chop

Searching with fork in C

邮差的信 提交于 2019-12-23 05:13:37
问题 I'm supposed to be getting comfortable with fork, and I saw an exercise that said to use a fork call to search an array indexed from 0 to 15. We're to assume that each process can only do two things...(1) is to check to see if an array is length 1, and (2) compare a single element of an array to the number were searching for. Basically i pass it a number, and its supposed to do a finite number of forks and return the index of that number. Here's my code.. #define MAXINDEX 16 int forkSearch

Which search is faster, binary search or using prefix tree?

落爺英雄遲暮 提交于 2019-12-22 08:29:15
问题 Suppose I have a list of strings and a prefix tree of those strings, and I would like to locate a string given a key, which one is more faster? binary search or prefix tree search? Why and what's the time complexity? Thanks! 回答1: Both techniques have their advantages, and their drawbacks: Suffix tree Advantages: O(N) building complexity O(M) search of a pattern of length M They allow online construction Drawbacks: Space inefficient Really complex construction algorithms Binary search (with

Where to choose linear search over binary search

孤街醉人 提交于 2019-12-22 08:14:03
问题 After having searched the internet I was not able to satisfy myself that I had found a comprehensive set of situations in which a linear search would be preferable to a binary search. I am essentially wondering whether it would be possible to compile a relatively definitive list of advice (from the point of view of general programming as one might find in industry). Alternatively I would much appreciate it if it could be verified that indeed I have seen all there is to say on the subject. 回答1

what's the difference between mid=(beg+end)/2 and mid=beg+(end-beg)/2 in binary search?

空扰寡人 提交于 2019-12-22 04:07:41
问题 It is a problem from C++ primer fifth edition problem 3.26, I don't know the difference between them ? May be the second one can avoid overflow. 回答1: May be the second one can avoid overflow. Exactly. There's no guarantee that beg+end is representable; but in the second case the intermediate values, as well as the expected result, are no larger than end , so there is no danger of overflow. The second form can also be used for affine types like pointers and other random-access iterators, which

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

♀尐吖头ヾ 提交于 2019-12-22 03:43:08
问题 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 an item in the list given a particular offset value, which is not exactly one of those values, but in that range. So, a binary search is what I want to do. I am now aware of the