binary-search

Average performance of binary search algorithm?

人走茶凉 提交于 2019-12-04 06:05:51
问题 http://en.wikipedia.org/wiki/Binary_search_algorithm#Average_performance BinarySearch(int A[], int value, int low, int high) { int mid; if (high < low) return -1; mid = (low + high) / 2; if (A[mid] > value) return BinarySearch(A, value, low, mid-1); else if (A[mid] < value) return BinarySearch(A, value, mid+1, high); else return mid; } If the integer I'm trying to find is always in the array, can anyone help me write a program that can calculate the average performance of binary search

Java collection binarySearch not working properly

南笙酒味 提交于 2019-12-04 04:33:51
问题 I am just trying to use native Java binarySearch hoping it can always find the first occurrence. But it's not always return the first occurrence, what have I done wrong here ? import java.util.*; class BinarySearchWithComparator { public static void main(String[] args) { // Please scroll down to see 'User' class implementation. List<User> l = new ArrayList<User>(); l.add(new User(10, "A")); l.add(new User(10, "A")); l.add(new User(10, "A")); l.add(new User(20, "B")); l.add(new User(20, "B"));

Sort vector of objects for binary search

淺唱寂寞╮ 提交于 2019-12-03 21:07:05
I have the following class: struct EdgeExtended { int neighborNodeId; int weight; int arrayPointer; bool isCrossEdge; }; I want to have a vector of such objects, sort it by neighborNodeId. Then I want to search for a particular neighborNodeId and return a reference to the found object inside the vector by binary search. Previously I used a map for that, so it was something like that: map<int, EdgeExtended> neighbours; ..... auto it = neighbours.find(dnodeId); if (it != neighbours.end()) { edgeMap = it->second; } Instead of map<int, EdgeExtended> neighbours; I want to have vector<EdgeExtended>

O(n log(n)) algorithm that checks if sum of 2 numbers in a int[] = given number

一曲冷凌霜 提交于 2019-12-03 20:25:18
问题 I am supposed to create a O(n log(n)) algorithm that checks if sum of 2 numbers in a int[] == given number. eg. Given [1,4,7,2,3,4] there will be a sum 8 (1+7) but not 20 The given answer suggested Binary Sort or Merge Sort, but they just gave the merge sort algorithm without the logic processing this particular requirement. Then another answer was: Suppose x is the sum we want to check, z is the set of elements in this array: The following algorithm solves the problem: Sort the elements in S

Is golden section search better than binary search?

天大地大妈咪最大 提交于 2019-12-03 17:41:56
问题 Recently I've heard an opinion that binary search may be improved by taking by splitting the range by phi (golden ration) instead of by 2. This was a big surprise to me, because I've never heard about such optimization. Is this true? Would this have been true if division by 2 and by phi was equally performant? If not, are there any general conditions under which golden section search would perform faster than binary search? UPD: Edited to remove link to a non-relevant Wikipedia article. Sorry

Get index of closest value with binary search

妖精的绣舞 提交于 2019-12-03 16:14:13
I want to do a binary search in python: def binarySearch(data, val): Where data is a sorted array and value is the value being searched for. If the value is found, I want to return the index (such that data[index] = val ). If the value is not found, I want to return the index of the item that is closest to that value. Here is what I've got: def binarySearch(data, val): high = len(data)-1 low = 0 while True: index = (high + low) / 2 if data[index] == val: return index if data[index] < val: low = index if data[index] > val: high = index Something like this should work. It returns an array with

Complexity of Binary Search

断了今生、忘了曾经 提交于 2019-12-03 15:01:19
I am watching the Berkley Uni online lecture and stuck on the below. Problem : Assume you have a collection of CD that is already sorted. You want to find the list of CD with whose title starts with "Best Of." Solution : We will use binary search to find the first case of "Best Of" and then we print until the tile is no longer "Best Of" Additional question : Find the complexity of this Algorithm. Upper Bound : Binary Search Upper Bound is O(log n), so once we have found it then we print let say k title. so it is O(logn + k) Lower Bound : Binary Search lower Bound is Omega(1) assuming we are

Looking for an algorithm (version of 2-dimensional binary search)

纵然是瞬间 提交于 2019-12-03 13:22:10
Easy problem and known algorithm: I have a big array with 100 members. First X members are 0, and the rest are 1. Find X. I am solving it by a binary search: Check member 50, if it is 0 - check member 75, etc, until I find adjacent 0 and 1. I am looking for an optimized algorithm for the same problem in 2-dimensions: I have 2-dimensional array 100*100. Those members that are on rows 0-X AND on columns 0-Y are 0, and the rest are 1. How to find Y and X? Simple solution: go first in X-direction and then in Y-direction. Check (0,50); If it is 0, check (0,75); until You find adjacent 0 and 1. Then

Binary Search Problems? [duplicate]

霸气de小男生 提交于 2019-12-03 12:23:54
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: What are the pitfalls in implementing binary search? I was perusing the wikipedia page for Binary Search and stumbled on a quote by Knuth below: "Although the basic idea of binary search is comparatively straightforward, the details can be surprisingly tricky" I recall implementing several Binary Searches as part of my Computer Science curriculum, but don't remember it being terribly tricky. However, this

binary search middle value calculation

蓝咒 提交于 2019-12-03 11:06:01
问题 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