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
48
votes
11 answers

How to perform a binary search on IList?

Simple question - given an IList how do you perform a binary search without writing the method yourself and without copying the data to a type with build-in binary search support. My current status is the following. List.BinarySearch() is not…
Daniel Brückner
  • 59,031
  • 16
  • 99
  • 143
43
votes
1 answer

How to get the iterator for a successful binary_search?

I want to get the iterator for the element I'm testing for in binary-search. But it only returns a bool indicating whether it found the value or not. How to get the iterator?
nakiya
  • 14,063
  • 21
  • 79
  • 118
41
votes
6 answers

How to apply binary search O(log n) on a sorted linked list?

Recently I came across one interesting question on linked list. Sorted singly linked list is given and we have to search one element from this list. Time complexity should not be more than O(log n). This seems that we need to apply binary search on…
Umesh K
  • 13,436
  • 25
  • 87
  • 129
41
votes
4 answers

Binary Search Tree Implementation in C++ STL?

Do you know, please, if C++ STL contains a Binary Search Tree (BST) implementation, or if I should construct my own BST object? In case STL conains no implementation of BST, are there any libraries available? My goal is to be able to find the…
Bunkai.Satori
  • 4,698
  • 13
  • 49
  • 77
40
votes
15 answers

Maximum subarray sum modulo M

Most of us are familiar with the maximum sum subarray problem. I came across a variant of this problem which asks the programmer to output the maximum of all subarray sums modulo some number M. The naive approach to solve this variant would be to…
Bhoot
  • 2,614
  • 1
  • 19
  • 36
39
votes
3 answers

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

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…
Jonah
  • 15,806
  • 22
  • 87
  • 161
39
votes
22 answers

Where is binary search used in practice?

Every programmer is taught that binary search is a good, fast way to search an ordered list of data. There are many toy textbook examples of using binary search, but what about in real programming: where is binary search actually used in real-life…
tjdonaldson
  • 609
  • 1
  • 6
  • 9
39
votes
4 answers

Difference between binary search and binary search tree?

What is the difference between binary search and binary search tree? Are they the same? Reading the internet it seems the second is only for trees (up to 2 children nodes) and binary search doesn't follow this rule. I didn't quite get it.
RollRoll
  • 8,133
  • 20
  • 76
  • 135
38
votes
13 answers

Faster than binary search for ordered list

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…
uray
  • 11,254
  • 13
  • 54
  • 74
35
votes
7 answers

binary search middle value calculation

The following is the pseudocode I got from a TopCoder tutorial about binary search binary_search(A, target): lo = 1, hi = size(A) while lo <= hi: mid = lo + (hi-lo)/2 if A[mid] == target: return mid else…
binsearch
  • 351
  • 1
  • 3
  • 3
33
votes
8 answers

Binary search in a sorted (memory-mapped ?) file in Java

I am struggling to port a Perl program to Java, and learning Java as I go. A central component of the original program is a Perl module that does string prefix lookups in a +500 GB sorted text file using binary search (essentially, "seek" to a byte…
sds
  • 373
  • 1
  • 4
  • 7
33
votes
8 answers

Implementation of C lower_bound

Based on the following definition found here Returns an iterator pointing to the first element in the sorted range [first,last) which does not compare less than value. The comparison is done using either operator< for the first version,…
Shamim Hafiz - MSFT
  • 21,454
  • 43
  • 116
  • 176
33
votes
7 answers

Find local minima in an array

Given an array of integers, find the local minima. An element A[i] is defined as a local minimum if A[i-1] > A[i] and A[i] < A[i+1] where i = 1...n-2. In case of boundary elements, the number has to be just smaller than its adjacent number. I know…
devsda
  • 4,112
  • 9
  • 50
  • 87
32
votes
17 answers

Why is Binary Search a divide and conquer algorithm?

I was asked if a Binary Search is a divide and conquer algorithm at an exam. My answer was yes, because you divided the problem into smaller subproblems, until you reached your result. But the examinators asked where the conquer part in it was,…
31
votes
8 answers

Find the nearest/closest value in a sorted List

I was wondering if it is possible to find the closest element in a sorted List for a element that is not in the list. For example if we had the values [1,3,6,7] and we are looking for the element closest to 4, it should return 3, because 3 is the…
drBet
  • 465
  • 1
  • 4
  • 7