binary-search

Faster than binary search for ordered list

隐身守侯 提交于 2019-11-28 15:49:07
is there an algorithm that is faster than binary search, for searching in sorted values of array? in my case, I have a sorted values (could be any type values) in an A array, I need to return n if the value I was looking is in range of A[n] and A[n+1] You can do better than O(log n) if the values are integers, in which case the best worst-case running time you can achieve, in terms of n, is O(sqrt(log n)). Otherwise, there is no way to beat O(log n) unless there are patterns in the input sequence. There are two approaches used to beat O(log n) in the case of integers. First, you can use y-fast

What are the pitfalls in implementing binary search?

血红的双手。 提交于 2019-11-28 15:15:01
Binary search is harder to implement than it looks. "Although the basic idea of binary search is comparatively straightforward, the details can be surprisingly tricky…" — Donald Knuth. Which bugs are most likely to be introduced into a new binary search implementation? Here are some I can think of: Off-by-one errors , when determining the boundary of the next interval Handling of duplicate items , if you are suppose to return the first equal item in the array but instead returned a subsequent equal item Numerical underflows/overflows when computing indices, with huge arrays Recursive vs non

Bash Script Binary Search

ぐ巨炮叔叔 提交于 2019-11-28 14:24:08
Write a bash script to do a binary search. Read student names and grades from a file into an array. Prompt the user for a student name. Find the name in the array and display the grade. The data in the file is below: Ann:A Bob:C Cindy:B Dean:F Emily:A Frank:C Ginger:D Hal:B Ivy:A Justin:F Karen:D I have done the following but I am stuck on what to do next #!/bin/bash echo "please enter students Name: " read student echo "$student + $Grade" ((i=0)) while read students[$i] ; do ((i++)) done < students.dat first=0 last=$(students[@]) ((mid=0)) Name=`echo ${students[$mid]} | cut -d: -f1` Grade=

Collections.binarySearch(List list, K key) clarification. Java

烂漫一生 提交于 2019-11-28 11:26:33
Given the following statement, taken from this Oracle java tutorial, related to the binarySearch() method of the class Collections: The return value is the same for both forms. If the List contains the search key, its index is returned. If not, the return value is (-(insertion point) - 1) , where the insertion point is the point at which the value would be inserted into the List, or the index of the first element greater than the value or list.size() if all elements in the List are less than the specified value. Why does the return value of binarySearch() not return only the negative index

Java equivalent of c++ equal_range (or lower_bound & upper_bound)

六眼飞鱼酱① 提交于 2019-11-28 11:08:54
I have a List of object sorted and I want to find the first occurrence and the last occurrence of an object. In C++, I can easily use std::equal_range (or just one lower_bound and one upper_bound). For example: bool mygreater (int i,int j) { return (i>j); } int main () { int myints[] = {10,20,30,30,20,10,10,20}; std::vector<int> v(myints,myints+8); // 10 20 30 30 20 10 10 20 std::pair<std::vector<int>::iterator,std::vector<int>::iterator> bounds; // using default comparison: std::sort (v.begin(), v.end()); // 10 10 10 20 20 20 30 30 bounds=std::equal_range (v.begin(), v.end(), 20); // ^ ^ //

binary search efficiency vs. linear search efficiency in fortran

荒凉一梦 提交于 2019-11-28 10:11:12
This question is about the efficiency of a linear search vs. the efficiency of a binary search for a pre-sorted array in contiguous storage... I have an application written in fortran (77!). One frequent operation for my part of the code is to find the index in an array such that gx(i) <= xin < gx(i+1) . I've currently implemented this as a binary search -- sorry for the statement labels and goto -- I've commented what the equivalent statments would be using fortran 90... i=1 ih=nx/2 201 continue !do while (.true.) if((xin.le.gx(i)).and.(xin.gt.gx(i+1)))then !found what we want ilow=i+1; ihigh

How to implement binary search in JavaScript

北战南征 提交于 2019-11-28 10:04:31
问题 https://www.khanacademy.org/computing/computer-science/algorithms/binary-search/p/challenge-binary-search I was following the pseudo code to implement algorithm on the link but don't know what's wrong with my code. Here is my code : /* Returns either the index of the location in the array, or -1 if the array did not contain the targetValue */ var doSearch = function(array, targetValue) { var min = 0; var max = array.length - 1; var guess; while(min < max) { guess = (max + min) / 2; if (array

Find a missing 32bit integer among a unsorted array containing at most 4 billion ints

拈花ヽ惹草 提交于 2019-11-28 09:27:16
This is the problem described in Programming pearls . I can not understand binary search method descrbied by the author. Can any one helps to elaborate? Thanks. EDIT: I can understand binary search in general. I just can not understand how to apply binary search in this special case. How to decide the missing number is in or not in some range so that we can choose another. English is not my native language, that is one reason I can not understand the author well. So, use plain english please:) EDIT: Thank you all for your great answer and comments ! The most important lesson I leant from

How to perform binary search on NSArray?

眉间皱痕 提交于 2019-11-28 09:06:52
What is the simplest way to do a binary search on an (already) sorted NSArray ? Some potential ways I have spotted so far include: The use of CFArrayBSearchValues (mentioned here ) - would this work on an NSArray ? The method indexOfObject:inSortedRange:options:usingComparator: of NSArray assumes the array is sorted and takes an opts param of type NSBinarySearchingOptions - does this mean it performs a binary search? The docs just say: Returns the index, within a specified range, of an object compared with elements in the array using a given NSComparator block. Write my own binary search

How to use recursion in creating a binary search algorithm

一世执手 提交于 2019-11-28 08:41:04
I have been using my time off university to practice Java through coding algorithms. One of the algorithms I coded was the binary search: public class BinarySearch { private static int list[] = {3, 6, 7, 8, 9, 10}; public static void main(String[] args) { BinarySearch b = new BinarySearch(); b.binarySearch(list); } public void binarySearch(int[] args) { System.out.println("Binary search."); int upperBound = args.length; int lowerBound = 1; int midpoint = (upperBound + lowerBound) / 2; int difference = upperBound - lowerBound; int search = 7; for (int i = 0; i < args.length; i++) { if (search <