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
22
votes
3 answers

When to use "=" in binary search condition?

I am quite confused about the scenarios when to use the = in binary search. For example, this is what i found from wiki, in which it is using while (imin <= imax) int binary_search(int A[], int key, int imin, int imax) { // continue searching…
skydoor
  • 25,218
  • 52
  • 147
  • 201
22
votes
4 answers

The possible number of binary search trees that can be created with N keys is given by the Nth catalan number. Why?

This has been bothering me for a while. I know that given N keys to arrange in the form of a binary search tree, the possible number of trees that can be created correspond to the Nth number from the Catalan sequence. I have been trying to…
Sergio Morales
  • 2,600
  • 6
  • 32
  • 40
21
votes
3 answers

Confuse in Binary Search when to use left < right ; left <= right ; and few more

I've difficulty in understanding when to use: while (left < right ) { } vs when to use: while (left <= right ) { } Also while setting left and right boundaries sometimes we use: left = mid and sometime we use left = mid + 1; similarly right…
Ankit Kotak
  • 383
  • 4
  • 11
21
votes
3 answers

What is the difference between partition_point and lower_bound?

C++11 includes the algorithm std::partition_point(). However for all the cases I have tried it gives the same answer as std::lower_bound(). The only difference being the convenient T& value parameter. Did I miss something or are these two functions…
Ankur S
  • 548
  • 3
  • 18
21
votes
5 answers

Why is Arrays.binarySearch not improving the performance compared to walking the array?

I gave a shot at solving the Hackerland Radio Transmitters programming challange. To summarize, challenge goes as follows: Hackerland is a one-dimensional city with n houses, where each house i is located at some xi on the x-axis. The Mayor wants…
Koray Tugay
  • 22,894
  • 45
  • 188
  • 319
21
votes
9 answers

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

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…
RoundPi
  • 5,819
  • 7
  • 49
  • 75
20
votes
2 answers

Binary Search Terminating Condition

Whenever I perform binary search iteratively I am always confused about whether I should use while (low < high) or while(low <= high). Although both will work, can someone tell me what might be the practical advantage of one over the other?
Zanko
  • 4,298
  • 4
  • 31
  • 54
20
votes
3 answers

findInterval() with right-closed intervals

The great findInterval() function in R uses left-closed sub-intervals in its vec argument, as shown in its docs: if i <- findInterval(x,v), we have v[i[j]] <= x[j] < v[i[j] + 1] If I want right-closed sub-intervals, what are my options? The best…
Ken Williams
  • 22,756
  • 10
  • 85
  • 147
20
votes
3 answers

How many comparisons will binary search make in the worst case using this algorithm?

Hi there below is the pseudo code for my binary search implementation: Input: (A[0...n-1], K) begin l ← 0; r ← n-1 while l ≤ r do m ← floor((l+r)/2) if K > A[m] then l ← m+1 else if K < A[m] then r ← m-1 else return m …
Harry Tiron
  • 287
  • 2
  • 3
  • 9
19
votes
3 answers

Difference between basic binary search for upper bound and lower bound?

In the article http://community.topcoder.com/tc?module=Static&d1=tutorials&d2=binarySearch, the author discusses binary search. He makes a distinction between finding the lowest value where something is true, and the highest value where something is…
John Targaryen
  • 1,109
  • 1
  • 13
  • 29
19
votes
2 answers

Ruby 2.0.0 Array#bsearch behavior

I noticed that as of Ruby 2.0.0 the array class has a bsearch method that I was testing and I'm not getting the behavior I'd expect. Why does it return a value for 2 and 5 but nil for -1, 1, and 4? arr_in = [-1, 1, 2, 4, 5] arr_in.bsearch { |x| x…
jshort
  • 1,006
  • 8
  • 23
18
votes
6 answers

Binary search for no uniform distribution

The binary search is highly efficient for uniform distributions. Each member of your list has equal 'hit' probability. That's why you try the center each time. Is there an efficient algorithm for no uniform distributions ? e.g. a distribution…
ic3
  • 7,917
  • 14
  • 67
  • 115
18
votes
5 answers

How to perform binary search on NSArray?

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…
Barjavel
  • 1,626
  • 3
  • 19
  • 31
17
votes
14 answers

Binary search algorithm in python

I am trying to implement the binary search in python and have written it as follows. However, I can't make it stop whenever needle_element is larger than the largest element in the array. Can you help? Thanks. def binary_search(array,…
AbdulFattah Popoola
  • 949
  • 2
  • 13
  • 22
17
votes
2 answers

Find Kth Smallest Pair Distance - Analysis

Question: This is a problem from LeetCode: Given an integer array, return the k-th smallest distance among all the pairs. The distance of a pair (A, B) is defined as the absolute difference between A and B. Example: Input: nums = [1,3,1] k =…
OLIVER.KOO
  • 5,654
  • 3
  • 30
  • 62