binary-search

Where to choose linear search over binary search

寵の児 提交于 2019-12-05 12:59:36
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. My list of reasons for choosing a linear search over a binary search are as follows: The list is unsorted

Algorithm: Modified Binary Search

…衆ロ難τιáo~ 提交于 2019-12-05 12:32:02
I am trying to tackle a classical interview problem which is basically performing a binary search on a list which first increases then decreases. Even though it's obvious that we can achieve O(log n) I couldn't figure out what is wrong the code below that I've written: #include <iostream> using namespace std; int binarySearch(int *A, int low, int high, int key) { while(low < high) { int mid = (low + high) / 2; if(key < A[mid]) { if(A[mid - 1] < A[mid] && A[mid] < A[mid + 1]) high = mid - 1; else low = mid + 1; } else if(key > A[mid]) { if(A[mid - 1] < A[mid] && A[mid] < A[mid + 1]) low = mid +

Finding an number in montonically increasing and then decreasing sequencecera

 ̄綄美尐妖づ 提交于 2019-12-05 01:50:21
Finding the maximum or minimum value in a sequence that increases montonically and then decreases monotonically can be done in O(log n). However, if i want to check if a number exists in such a sequence, can this also be done in O(log n)? I do not think that is possible. Consider this example: 1 4 5 6 7 10 8 3 2 0. In this example, if I need to find whether the sequence contains '2', I do not have any conditions to divide the search space into half of the original search space. In the worst, case it will be O(n), as you need to check for both halves, when we are trying to search for 2. I would

Binary search for first occurrence of k

落爺英雄遲暮 提交于 2019-12-05 01:41:51
问题 I have code that searches a sorted array and returns the index of the first occurrence of k. I am wondering whether its possible to write this code using while(left<right) instead of while(left<=right) Here is the full code: public static int searchFirstOfK(List<Integer> A, int k) { int left = 0, right = A.size() - 1, result = -1; // A.subList(left, right + 1) is the candidate set. while (left <= right) { int mid = left + ((right - left) / 2); if (A.get(mid) > k) { right = mid - 1; } else if

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

非 Y 不嫁゛ 提交于 2019-12-05 01:26:33
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. 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 can be subtracted to give a distance, but not added together. In general case the both expressions are

How to save CPU cycles when searching for a value in a sorted list?

て烟熏妆下的殇ゞ 提交于 2019-12-05 01:24:23
问题 In CodinGame learning platform, one of the questions used as an example in a C# tutorial is this one: The aim of this exercise is to check the presence of a number in an array. Specifications: The items are integers arranged in ascending order. The array can contain up to 1 million items. The array is never null . Implement the method boolean Answer.Exists(int[] ints, int k) so that it returns true if k belongs to ints , otherwise the method should return false . Important note: Try to save

If an NxM multiplication table is put in order, what is number in the middle?

烈酒焚心 提交于 2019-12-04 23:58:03
问题 If I have a multiplication table sized, for example, 3x5: 1 2 3 4 5 2 4 6 8 10 3 6 9 12 15 And I put all these numbers in order: 1 2 2 3 3 4 4 5 6 6 8 9 10 12 15 What is the number in the middle? In this case, it's 5. N and M are always odd, so there can be only one answer. Is there a fast solution for this? I'm looking for something among the lines of O(N log NM) This is homework of sorts, but I'm really lost with this one. I've come up with some ideas, but they all had some shortcomings:

Difficulty with Collections.binarySearch

笑着哭i 提交于 2019-12-04 22:06:24
I am new to JAVA and Netbeans and this is what I have to do: The user can write a CD title in the input box and then remove the CD from the list by pressing the remove button. If the CD does not exist in the collection, then a message can be displayed in the outbox to state this. I have to use Collections.binarySearch() to do this. This is only a part of the whole program but I already figured out the rest of it. This is what I have done: ArrayList <String> songs = new ArrayList(); Collections.addAll(songs, "Metric - Fantasies", "\nBeatles - Abbey Road", "\nPearl Jam - Ten", "\nDoors - Alive",

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

浪尽此生 提交于 2019-12-04 21:36:04
问题 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? 回答1: Simple solution: go first in X

Binary search if array contains duplicates

随声附和 提交于 2019-12-04 21:14:57
Hi, what is the index of the search key if we search for 24 in the following array using binary search. array = [10,20,21,24,24,24,24,24,30,40,45] I have a doubt regarding binary search that how does it works if a array has duplicate values.Can anybody clarify... The array you proposed has the target value in the middle index, and in the most efficient implementations will return this value before the first level of recursion. This implementation would return '5' (the middle index). To understand the algorithm, just step through the code in a debugger. public class BinarySearch { public static