binary-search

TypeError: list indices must be integers, not float

落花浮王杯 提交于 2019-12-03 10:46:55
I have a python 3.x program that is producing an error: def main(): names = ['Ava Fischer', 'Bob White', 'Chris Rich', 'Danielle Porter', 'Gordon Pike', 'Hannah Beauregard', 'Matt Hoyle', 'Ross Harrison', 'Sasha Ricci', 'Xavier Adams'] entered = input('Enter the name of whom you would you like to search for:') binary_search(names, entered) if position == -1: print("Sorry the name entered is not part of the list.") else: print(entered, " is part of the list and is number ", position, " on the list.") input('Press<enter>') def binary_search(names, entered): first = 0 last = len(names) - 1

Why we write lo+(hi-lo)/2 in binary search? [duplicate]

只谈情不闲聊 提交于 2019-12-03 07:14:13
问题 This question already has answers here : Calculating mid in binary search (5 answers) Closed 5 years ago . I was reading about binary search...I know that the traditional way of finding mid value is like mid=(hi+lo)/2 But i also see that to avoid overflow mid value is calculated like that mid=lo+(hi-lo)/2 But why?? I couldn't find the actual reason..Can anyone give me the reason with example?? It is different from other question because other questions didn't have the answer that i wanted

Maximum subarray sum modulo M

穿精又带淫゛_ 提交于 2019-12-03 03:56:47
问题 Most of us are familiar with the maximum sum subarray problem. I came across a variant of this problem which asks the programmer to output the maximum of all subarray sums modulo some number M. The naive approach to solve this variant would be to find all possible subarray sums (which would be of the order of N^2 where N is the size of the array). Of course, this is not good enough. The question is - how can we do better? Example: Let us consider the following array: 6 6 11 15 12 1 Let M = 13

Parallel Binary Search

与世无争的帅哥 提交于 2019-12-03 03:36:25
I'm just starting to learn about parallel programming, and I'm looking at binary search. This can't really be optimized by throwing more processors at it right? I know it's supposedly dividing and conquering, but you're really "decreasing and conquering" (from Wikipedia ). Or could you possibly parallelize the comparisons? (if X is less than array[mid] , search from low to mid - 1 ; else if X is greater than array[mid] search from mid + 1 to high , else return mid , the index of X ) Or how about you give half of the array to one processor to do binary search on, and the other half to another?

binary search middle value calculation

我的梦境 提交于 2019-12-03 02:38:32
The following is the pseudocode I got from a TopCoder tutorial about binary search binary_search(A, target): lo = 1, hi = size(A) while lo <= hi: mid = lo + (hi-lo)/2 if A[mid] == target: return mid else if A[mid] < target: lo = mid+1 else: hi = mid-1 // target was not found Why do we calculate the middle value as mid = lo + (hi - lo) / 2 ? Whats wrong with (hi + lo) / 2 I have a slight idea that it might be to prevent overflows but I'm not sure, perhaps someone can explain it to me and if there are other reasons behind this. Yes, (hi + lo) / 2 may overflow. This was an actual bug in Java

Performance of numpy.searchsorted is poor on structured arrays

血红的双手。 提交于 2019-12-03 02:03:18
Sorry in advance if I'm misusing any terms, feel free to correct that. I have a sorted array with dtype '<f16, |S30' . When I use searchsorted on its first field, it works really slow (about 0.4 seconds for 3 million items). That is much longer than bisect takes to do the same on a plain Python list of tuples. %timeit a['f0'].searchsorted(400.) 1 loops, best of 3: 398 ms per loop However, if I copy the float part to another, separate array, the search is faster than bisect : b = a['f0'].copy() %timeit b.searchsorted(400.) 1000000 loops, best of 3: 945 ns per loop My questions are: Am I doing

Binary Search to Compute Square root (Java)

雨燕双飞 提交于 2019-12-02 23:16:39
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); System.out.print("Enter A Valid Integer: "); int value = console.nextInt(); calculateSquareRoot(value); } public static int calculateSquareRoot(int value) { while (value > 0) { double sqrt = (int) Math.sqrt(value); System.out.println(sqrt); } return -1; } } The fact that it has

C# lambda expressions and IComparer

∥☆過路亽.° 提交于 2019-12-02 22:43:54
I am using lambda expressions to sort and search an array in C#. I don't want to implement the IComparer interface in my class, because I need to sort and search on multiple member fields. class Widget { public int foo; public void Bar() { Widget[] widgets; Array.Sort(widgets, (a, b) => a.foo.CompareTo(b.foo)); Widget x = new Widget(); x.foo = 5; int index = Array.BinarySearch(widgets, x, (a, b) => a.foo.CompareTo(b.foo)); } } While the sort works fine, the binary search gives a compilation error Cannot convert lambda expression to type 'System.Collections.IComparer<Widget>' because it is not

In C, is it guaranteed that 1/2 == 0? [duplicate]

旧时模样 提交于 2019-12-02 21:56:21
问题 This question already has answers here : What is the behavior of integer division? (5 answers) Closed 6 years ago . Is it guaranteed in C that 1/2 == 0 ? I need that for an implementation of binary search: /* * len is the array length * ary is an array of ptrs * f is a compare function * found is a ptr to the found element in the array * both i and offset are unsigned integers, used as indexes */ for(i = len/2; !found && i < len; i += offset) { res = f(c->ary[i]); if (res == 0) { found = c-

Why we write lo+(hi-lo)/2 in binary search? [duplicate]

你说的曾经没有我的故事 提交于 2019-12-02 20:49:52
This question already has an answer here: Calculating mid in binary search 5 answers I was reading about binary search...I know that the traditional way of finding mid value is like mid=(hi+lo)/2 But i also see that to avoid overflow mid value is calculated like that mid=lo+(hi-lo)/2 But why?? I couldn't find the actual reason..Can anyone give me the reason with example?? It is different from other question because other questions didn't have the answer that i wanted with example... Suppose you are searching a 4000000000-element array using 32-bit unsigned int as indexes. The first step made