binary-search

Is there a built-in binary-search In Ruby?

和自甴很熟 提交于 2019-12-17 16:20:03
问题 I am looking for a built-in Ruby method that has the same functionality as index but uses a binary search algorithm, and thus requires a pre-sorted array. I know I could write my own implementation, but according to "Ruby#index Method VS Binary Search", the built-in simple iterative search used by index is faster than a pure-Ruby version of binary search, since the built-in method is written in C. Does Ruby provide any built-in methods that do binary search? 回答1: Ruby 2.0 introduced Array

find an element in a sorted matrix [duplicate]

别来无恙 提交于 2019-12-17 09:02:12
问题 This question already has answers here : How do I search for a number in a 2d array sorted left to right and top to bottom? (19 answers) Closed last year . Problem: Given a matrix in which each row and each column is sorted, write a method to find an element in it. It is a classic interview question, here is my solution boolean F(int[][] matrix, int hs, int he, int ws, int we) { if (hs > he || ws > we) return false; int m = (hs + he) / 2; int n = (ws + we) / 2; if (matrix[m][n] == t) { return

find an element in a sorted matrix [duplicate]

落爺英雄遲暮 提交于 2019-12-17 09:01:49
问题 This question already has answers here : How do I search for a number in a 2d array sorted left to right and top to bottom? (19 answers) Closed last year . Problem: Given a matrix in which each row and each column is sorted, write a method to find an element in it. It is a classic interview question, here is my solution boolean F(int[][] matrix, int hs, int he, int ws, int we) { if (hs > he || ws > we) return false; int m = (hs + he) / 2; int n = (ws + we) / 2; if (matrix[m][n] == t) { return

find an element in a sorted matrix [duplicate]

旧巷老猫 提交于 2019-12-17 09:01:06
问题 This question already has answers here : How do I search for a number in a 2d array sorted left to right and top to bottom? (19 answers) Closed last year . Problem: Given a matrix in which each row and each column is sorted, write a method to find an element in it. It is a classic interview question, here is my solution boolean F(int[][] matrix, int hs, int he, int ws, int we) { if (hs > he || ws > we) return false; int m = (hs + he) / 2; int n = (ws + we) / 2; if (matrix[m][n] == t) { return

Can LINQ use binary search when the collection is ordered?

倖福魔咒の 提交于 2019-12-17 08:54:27
问题 Can I somehow "instruct" LINQ to use binary search when the collection that I'm trying to search is ordered. I'm using an ObservableCollection<T> , populated with ordered data, and I'm trying to use Enumerable.First(<Predicate>). In my predicate, I'm filtering by the value of the field my collection's sorted by. 回答1: As far as I know, it's not possible with the built-in methods. However it would be relatively easy to write an extension method that would allow you to write something like that

Find kth smallest element in a binary search tree in Optimum way

限于喜欢 提交于 2019-12-17 02:03:34
问题 I need to find the kth smallest element in the binary search tree without using any static/global variable. How to achieve it efficiently? The solution that I have in my mind is doing the operation in O(n), the worst case since I am planning to do an inorder traversal of the entire tree. But deep down I feel that I am not using the BST property here. Is my assumptive solution correct or is there a better one available ? 回答1: Here's just an outline of the idea: In a BST, the left subtree of

How to find the kth smallest element in the union of two sorted arrays?

半世苍凉 提交于 2019-12-17 02:03:13
问题 This is a homework question. They say it takes O(logN + logM) where N and M are the arrays lengths. Let's name the arrays a and b . Obviously we can ignore all a[i] and b[i] where i > k. First let's compare a[k/2] and b[k/2] . Let b[k/2] > a[k/2] . Therefore we can discard also all b[i] , where i > k/2. Now we have all a[i] , where i < k and all b[i] , where i < k/2 to find the answer. What is the next step? 回答1: You've got it, just keep going! And be careful with the indexes... To simplify a

Binary search (bisection) in Python

时光毁灭记忆、已成空白 提交于 2019-12-16 20:03:33
问题 Is there a library function that performs binary search on a list/tuple and return the position of the item if found and 'False' (-1, None, etc.) if not? I found the functions bisect_left/right in the bisect module, but they still return a position even if the item is not in the list. That's perfectly fine for their intended usage, but I just want to know if an item is in the list or not (don't want to insert anything). I thought of using bisect_left and then checking if the item at that

How to sort a list of objects according to two parameters to compare at Java?

流过昼夜 提交于 2019-12-13 19:07:48
问题 I have a class like that: public class Zern extends Something{ private int costA; private int costB; public int getcostA() { return costA; } public void setcostA(int costA) { this.costA = costA; } public int getcostB() { return costB; } public void setcostB(int costB) { this.costB = costB; } } I have a list that holds that kind of objects: private List<Zern> zerns = new ArrayList<Zern>(MAX_ZERN_SIZE); I will add new objects to my list however I always want to have a ordered list according to

JavaScript - Binary search hangs every time

久未见 提交于 2019-12-13 14:05:49
问题 I have a 2D array, something like the following: [1.11, 23] [2.22, 52] [3.33, 61] ... Where the array is ordered by the first value in each row. I am trying to find a value within the array that is close to the search value - within a certain sensitivity. The way this is set up, and the value of the sensitivity, ensure only one possible match within the array. The search value is the current x-pos of the mouse. The search is called on mousemove , and so is being called often. Originally I had