Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 A[n+1]

like image 695
uray Avatar asked Oct 30 '10 04:10

uray


People also ask

Is there anything faster than binary search?

Hashing. For implementing associative arrays, hash tables, a data structure that maps keys to records using a hash function, are generally faster than binary search on a sorted array of records. Most hash table implementations require only amortized constant time on average.

Which one is faster binary search or sequential search?

A binary search is usually slower than a sequential search on sorted array of data.

How much faster is binary search than linear search?

This makes the Binary search is suggested for the searching tasks. But, the Binary search needs to order the elements in a list. We must consider choosing the best sorting algorithm. According to the simulation, it concludes that the Binary search algorithm is 1,000 times faster than the Linear search algorithm.

Is exponential search faster than binary?

In simple words, if you have N elements and if e is in the first sqrt(N) elements, then exponential search will be faster, else binary search will be faster.


2 Answers

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 trees which work by storing in a hash table all prefixes for which you are storing at least one integer with that prefix. This enables you to perform a binary search to find the length of the longest matching prefix. This enables you to find the successor of an element for which you are searching in time O(log w) where w is the number of bits in a word. There are some details to work though to make this work and use only linear space, but they aren't too bad (see the link below).

Second, you can use fusion trees, which use bit tricks to enable you to perform w^O(1) comparisons in just a constant number of instructions, yielding a running time of O(log n / log w).

The optimum tradeoff between these two data structures occurs when log w = sqrt(log n), giving a running time of O(sqrt(log n)).

For details on the above, see lectures 12 and 13 of Erik Demaine's course: http://courses.csail.mit.edu/6.851/spring07/lec.html

like image 122
jonderry Avatar answered Oct 10 '22 00:10

jonderry


One possibility is to treat it like finding the roots of a function. Basically, finding:

a[i] <= i <= a[i + 1] 

Is equivalent to:

a[i] - i <= 0 <= a[i + 1] - i 

Then you could try something like Newton's method and so on. These kinds of algorithms frequently converge faster than a binary search when they work, but I don't know of one that is guaranteed to converge for all input.

http://en.wikipedia.org/wiki/Root-finding_algorithm

like image 26
xscott Avatar answered Oct 09 '22 23:10

xscott