Questions tagged [binary-search]

Binary search is an efficient algorithm for finding an element in a sorted array. The basic idea is to cut the search space in half in each step. The complexity of the algorithm is O(log(n)).

A binary search (or "half-interval search") algorithm finds the position of a specified value (the input "key") within a sorted array. In each step, the algorithm compares the input key value with the key value of the middle element of the array. If the keys match, then a matching element has been found so its index, or position, is returned. Otherwise, if the sought key is less than the middle element's key, then the algorithm repeats its action on the sub-array to the left of the middle element or, if the input key is greater, on the sub-array to the right. If the remaining array to be searched is reduced to zero, then the key cannot be found in the array and a special "Not found" indication is returned.

A binary search halves the number of items to check with each iteration, so locating an item (or determining its absence) takes logarithmic time. A binary search is a dichotomic divide and conquer search algorithm.

For pseudo-code and examples in multiple languages, check this Code Codex page.

2936 questions
514
votes
3 answers

Optimum way to compare strings in JavaScript?

I am trying to optimize a function which does binary search of strings in JavaScript. Binary search requires you to know whether the key is == the pivot or < the pivot. But this requires two string comparisons in JavaScript, unlike in C like…
HRJ
  • 17,079
  • 11
  • 56
  • 80
213
votes
22 answers

Binary search (bisection) in Python

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…
rslite
  • 81,705
  • 4
  • 44
  • 47
156
votes
15 answers

how to calculate binary search complexity

I heard somebody say that since binary search halves the input required to search hence it is log(n) algorithm. Since I am not from a mathematics background I am not able to relate to it. Can somebody explain it in a little more detail? Does it have…
Bunny Rabbit
  • 8,213
  • 16
  • 66
  • 106
120
votes
9 answers

Where can I get a "useful" C++ binary search algorithm?

I need a binary search algorithm that is compatible with the C++ STL containers, something like std::binary_search in the standard library's header, but I need it to return the iterator that points at the result, not a simple boolean…
Robert Gould
  • 68,773
  • 61
  • 187
  • 272
116
votes
34 answers

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

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…
bragboy
  • 34,892
  • 30
  • 114
  • 171
115
votes
17 answers

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

This is a homework question, binary search has already been introduced: Given two arrays, respectively N and M elements in ascending order, not necessarily unique: What is a time efficient algorithm to find the kth smallest element in the union of…
Michael
  • 41,026
  • 70
  • 193
  • 341
93
votes
14 answers

Calculating mid in binary search

I was reading an algorithms book which had the following algorithm for binary search: public class BinSearch { static int search ( int [ ] A, int K ) { int l = 0 ; int u = A. length −1; int m; while (l <= u ) { m = (l+u) /2; …
Bharat
  • 2,960
  • 2
  • 38
  • 57
79
votes
17 answers

Which is faster, Hash lookup or Binary search?

When given a static set of objects (static in the sense that once loaded it seldom if ever changes) into which repeated concurrent lookups are needed with optimal performance, which is better, a HashMap or an array with a binary search using some…
TheSoftwareJedi
  • 34,421
  • 21
  • 109
  • 151
76
votes
28 answers

Binary Search in Javascript

I'm trying to implement a binary search algorithm in JavaScript. Things seem okay, but my return statements appear to be returning undefined. Can anybody tell me what's wrong here? Fiddle: http://jsfiddle.net/2mBdL/ var a = [ 1, 2, 4, …
4m1r
  • 12,234
  • 9
  • 46
  • 58
69
votes
8 answers

Find the first element in a sorted array that is greater than the target

In a general binary search, we are looking for a value which appears in the array. Sometimes, however, we need to find the first element which is either greater or less than a target. Here is my ugly, incomplete solution: // Assume all elements are…
SecureFish
  • 2,391
  • 7
  • 32
  • 43
69
votes
7 answers

What are the pitfalls in implementing binary search?

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…
joeforker
  • 40,459
  • 37
  • 151
  • 246
60
votes
2 answers

What is the performance impact of non-unique indexes in pandas?

From the pandas documentation, I've gathered that unique-valued indices make certain operations efficient, and that non-unique indices are occasionally tolerated. From the outside, it doesn't look like non-unique indices are taken advantage of in…
ChrisB
  • 4,628
  • 7
  • 29
  • 41
57
votes
2 answers

What are python's equivalents of std::lower_bound and std::upper_bound C++ algorithms?

Does python provide functions for performing binary search on sorted lists, analogous to the std::lower_bound and std::upper_bound algorithms of the C++ Standard Library?
Leon
  • 31,443
  • 4
  • 72
  • 97
52
votes
11 answers

What is the difference between Linear search and Binary search?

What is the difference between Linear search and Binary search?
Smi
51
votes
4 answers

function for finding last item less-than-or-equal to, like lower_bound

Is there a function in that uses binary search, like lower_bound but that returns the last item less-than-or-equal-to according to a given predicate? lower_bound is defined to: Finds the position of the first element in an ordered range that has…
the_mandrill
  • 29,792
  • 6
  • 64
  • 93
1
2 3
99 100