Questions tagged [binary-search]

Questions about the binary search algorithm, which can be used to find elements of an ordered list in O(log n) time.

146 questions
60
votes
3 answers

Why is binary search faster than ternary search?

Searching an array of $N$ elements using binary search takes, in the worst case $\log_2 N$ iterations because, at each step we trim half of our search space. If, instead, we used 'ternary search', we'd cut away two-thirds of our search space at…
36
votes
2 answers

Why is the log in the big-O of binary search not base 2?

I am new to understanding computer science algorithms. I understand the process of the binary search, but I am having a slight misunderstanding with its efficiency. In a size of $s = 2^n$ elements, it would take, on average, $n$ steps to find a…
Cyclopropane
  • 463
  • 1
  • 4
  • 7
15
votes
3 answers

Why is binary search using this weird thing to calculate middle?

I noticed that in many books calculation of midpoint for binary search uses this: int mid = left + (right - left) / 2; Why not use int mid = (left + right) / 2; instead?
Dvole
  • 253
  • 1
  • 2
  • 5
15
votes
3 answers

Is there any study or theory behind combining binary search and interpolation search?

I just read Can this algorithm still be considered a Binary Search algorithm? and recalled that a few years back I wrote an indexer/search for log files to find log entries in large plain text files by date/time window. Whilst doing this, I decided…
Neil Slater
  • 253
  • 1
  • 8
14
votes
3 answers

Can this algorithm still be considered a Binary Search algorithm?

While doing the second code kata (which asks you to implement a binary search algorithm five times, each time with a different method), I've come up with a slightly different solution which works as follows: If i have a sorted array of lenght 100…
11
votes
6 answers

Find the number using binary search against one possible lie

We all know this classic problem, "there is some hidden number and you have to interactively guess it.", which could be solved using binary search when we know that maximum number that we can guess. But what if the interactor may lie to us in one…
9
votes
3 answers

Why is the time complexity of insertion sort not brought down even if we use binary search for the comparisons?

There are two factors that decide the running time of the insertion sort algorithm: the number of comparisons, and the number of movements. In the case of number of comparisons, the sorted part (left side of $j$) of the array is searched linearly…
Somenath Sinha
  • 345
  • 4
  • 11
8
votes
1 answer

Proving that the average case complexity of binary search is O(log n)

I know that the both the average and worst case complexity of binary search is O(log n) and I know how to prove the worst case complexity is O(log n) using recurrence relations. But how would I go about proving that the average case complexity of…
cj1996
  • 83
  • 1
  • 1
  • 5
8
votes
4 answers

Compute square root using (bit) additions and shifts as primitives

Question: Given an $n$-bit natural number $N$, how to compute $\lceil \sqrt{N} \rceil$ using only $O(n)$ (bit) additions and shifts? The tip is to use binary search. However, I could not achieve the required complexity (I got $O(n^2)$). What does…
hengxin
  • 9,671
  • 3
  • 37
  • 75
6
votes
1 answer

Optimize binary search on Segment Tree by storing past result

Goal Let $A[n]$ be an arbitrary array of integers of length $n$. Let $S$ be a segment tree, represented by an array of records: each record containing the left and right bounds ($[l,r]$) of the segment the node covers, and a "sum" value equal to…
concat
  • 311
  • 1
  • 8
6
votes
1 answer

Binary-ish search through partially ordered set

I have an interesting function. It takes subsets of {1,...,N} to positive integers, i.e. $f:P([N]) \rightarrow Z^+$. I know that if S is a subset of S', $f(S) < f(S')$. Also, if S and S' have the same cardinality, the ordering induced by f is…
Craig
  • 163
  • 4
5
votes
1 answer

Fastest search algorithm in a sorted list with certain error rate-limiting constraints

This problem came up during the Google CTF 2017. For background information about the challenge you can search for GoogleCTF A7 ~ Gee cue elle. Problem description: A random number N between 0 and…
5
votes
4 answers

Finding a value in a sorted array in log R time, R is the number of distinct elements

The standard binary search algorithm gives log N time, where N is the total number of elements in the array. When the array has duplicates, I don't see how you could detect those duplicates ahead of time. (Iterating through the array takes N time,…
atenao
  • 161
  • 3
5
votes
1 answer

"Unbounded" binary search in $\log_2(n) + O(?)$ comparisons

Binary search is the well-known algorithm that compares the input value to an entry in a sorted array, and based on the result then decides to check the same input value against another entry either on the left or the right of that array entry. The…
Albert Hendriks
  • 2,481
  • 16
  • 35
4
votes
2 answers

Binary search with alternative comparison cost

I have a sorted array $A$ of non-arbitrary elements. Now, I have another element $c$ and I want to find out where it belongs in the sorting of $A$. The cost of comparing $c$ to $A_i$ is $\Theta(i^2)$. I do not directly care about the number of…
Albert Hendriks
  • 2,481
  • 16
  • 35
1
2 3
9 10