Faster than binary search for ordered list

后端 未结 11 1719
自闭症患者
自闭症患者 2020-12-12 13:51

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

相关标签:
11条回答
  • 2020-12-12 14:01

    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

    0 讨论(0)
  • 2020-12-12 14:01

    You can always put them in a hash table, then search will be O(1). It will be memory intensive though and if you keep adding items, the hash table might need to be re-bucketed. Re-bucketing is O(n) but it will get amortized to O(1). It essentially depends on whether you can afford that space and the potential cache misses.

    0 讨论(0)
  • 2020-12-12 14:02

    In binary search you split the list into two "sublists" and you only search the sublist that may contain the value. Depending on how large your array is, you could see a speedup if you split the array into more than two splices.

    You can determine which region of the array you have to search, by keeping an index, that you search first. Like in a telephone book of a large city, where you can see from the outside, where you have to start to search. (I have trouble expressing my idea in text, and I did not find an english link yet that explains it better).

    0 讨论(0)
  • 2020-12-12 14:03

    It's been mentioned in misc comments, but I would think a natural & simple answer to this particular question ("any-type, values in an array") would be an Interpolation Search:

    Instead of calculating the midpoint, interpolation search estimates the position of the target value, taking into account the lowest and highest elements in the array as well as length of the array. It works on the basis that the midpoint is not the best guess in many cases. For example, if the target value is close to the highest element in the array, it is likely to be located near the end of the array.

    Quote from: https://en.wikipedia.org/wiki/Binary_search_algorithm

    Main page: https://en.wikipedia.org/wiki/Interpolation_search

    Under the assumption of a uniform distribution it can approach O(log log N)

    Since CPUs are so fast compared to memory access these days (program for RAM like you once did for disk) the index / comparison calculations are likely cheap compared to each data fetch. It might also be possible to eke out a little more performance with a linear search once the search is sufficiently narrowed (exploiting memory / cache locality).

    0 讨论(0)
  • 2020-12-12 14:06

    If you have a huge amount of numbers to find, and by some fluke they are ALSO sorted, you could do it in O(n + m) where m is the number of numbers to find. Basically just your typical merge algorithm, with slight modification to record which value each checked number would be inserted before, if it was to be inserted into the array.

    You can always trade off space... And time of other operations. Assuming all your elements are constant size p bits, you can make a massive array which stores, for each possible value you could look up, the index of the next bigger value currently stored. This array needs to be 2^p*lg(n) bits, where n is the number values stored. Each insertion or deletion is O(2^p) but typically around 2^p/n, because you have to go through updating all those indices.

    But your lookup is now O(1)!

    OK, OK, it's not really practical. But dividing the input into blocks in a similar fashion could possibly reduce the constant in front of your log. Possibly.

    0 讨论(0)
  • 2020-12-12 14:07

    What about the following algo? it is called Exponential Search and is one of the variations of binary search. http://en.m.wikipedia.org/wiki/Exponential_search

    Searching for element k in sorted array A of size n. Lookup A[2^i] for i=0, 1, 2,... until you go beyond k's position in A. then do a binary search on the part of the array left (smaller) than i.

    int exponential_search(int A[], int key)
    {
      // lower and upper bound for binary search
      int lower_bound = 0;
      int upper_bound = 1;
    
      // calculate lower and upper bound
      while (A[upper_bound] < key) {
        lower_bound = upper_bound;
       upper_bound = upper_bound * 2;
      }
      return binary_search(A, key, lower_bound, upper_bound);
    }
    

    This algo will run on O(log idx) where idx is the index of k in A. (both stpes are in log idx). In the worst case, the algo is in O(log idx), if k is amongst the largest elements of A or bigger than any element of A. The multiplicative constant is larger than for binary search but the algo would run faster for very large arrays and when looking for data that's towards the beginning of the array.

    I'D like to have some idea of the minimal size n where this algo becomes preferable to binary search, but I don't know.

    0 讨论(0)
提交回复
热议问题