Algorithm-8. Binary search in ordered array

Source: Internet
Author: User
Tags comparable

1. Specific algorithms

/** * Algorithm 3.2 binary lookup (based on ordered array) * Created by Huazhou on 2015/11/29.    */public class Binarysearchst<key extends Comparable<key>, value> {private key[] keys;    Private value[] Vals;    private int N;        public binarysearchst (int capacity) {keys = (key[]) new comparable[capacity];    Vals = (value[]) new object[capacity];    } public int size () {return N;    } public boolean IsEmpty () {return size () = = 0;        Public Value get (key key) {if (IsEmpty ()) {return null;        } int i = rank (key);        if (i < N && Keys[i].compareto (key) = = 0) {return vals[i];        } else{return null;        }} public int rank (key key) {int lo = 0, hi = N-1;            while (lo <= hi) {int mid = lo + (Hi-lo)/2;            int cmp = Key.compareto (Keys[mid]);            if (CMP < 0) Hi = mid-1;            else if (cmp > 0) lo = mid + 1; else return miD    } return lo;        }//Lookup key, find the update value, otherwise create a new element public void put (key key, value val) {int i = rank (key);            if (i < N && Keys[i].compareto (key) = = 0) {Vals[i] = val;        Return            } for (int j = N; j > i; j--) {keys[j] = keys[j-1];        VALS[J] = vals[j-1];        } Keys[i] = key;        Vals[i] = val;    n++;        } public void Delete (key key) {if (IsEmpty ()) return;        Compute rank int i = rank (key);        Key not in table if (i = = N | | keys[i].compareto (KEY)! = 0) {return;            } for (int j = i; J < N-1; J + +) {Keys[j] = keys[j+1];        VALS[J] = vals[j+1];        } n--;  KEYS[N] = null;        To avoid loitering vals[n] = null;    Resize if full if (n > 0 && n = = KEYS.LENGTH/4) resize (KEYS.LENGTH/2); }}

The implementation of this symbol table uses two arrays to hold the keys and values. Like an array-based stack, the put () method moves all the larger keys backward one cell before inserting the new element.

The rank () method implements the classic algorithm described in the body to calculate the number of keys less than a given key. It first compares the key to the middle key, returns its index if it is equal, or, if it is less than the middle key, looks in the left half, or greater than the right half.

Public Key min () {        return keys[0];    }    Public Key Max () {        return keys[n-1];    }    Public Key Select (int k) {        return keys[k];    }    Public Key ceiling (key key) {        int i = rank (key);        return keys[i];    }    Public Key floor (key key) {        int i = rank (key);        if (i < N && Key.compareto (keys[i]) = = 0) return keys[i];        if (i = = 0) return null;        else return keys[i-1];    }    Public boolean contains (key key) {        return get (key)! = null;    }    Public iterable<key> keys (key lo, key hi) {        queue<key> q = new queue<key> ();        for (int i = rank (lo); i < rank (hi); i++) {            q.enqueue (keys[i]);        }        if (contains (HI)) {            q.enqueue (Keys[rank (HI)]);        }        return q;    }

2. Algorithm analysis

The recursive implementation of rank () also allows us to immediately get a conclusion: The binary lookup is fast, because the recursive relationship can explain the upper bound of the number of comparisons required by the algorithm.

proposition: binary lookup in an ordered array of n keys requires (lgn+1) a second comparison (whether or not it succeeds).

Proof: The analysis here is similar to the analysis of merge sort (but relatively simple). The number of comparisons that are required to find a key in a symbol table with a size of n in the order of C (N). Obviously we have C (0) =0,c (1) = 1, and for n>0 we can write an inductive relationship that corresponds directly to the recursive method:

C (N) <=c (└n/2┘) +1

Whether the lookup continues on the left or right side of the middle element, the size of the subarray does not exceed └n/2┘, we need to compare it once to check whether the middle element and the found key are equal, and decide to continue looking for the left or right sub-array. This recursion is easy when N is a power minus 1 o'clock (n=2n-1) of 2. First of all, because of └n/2┘=2n-1-1, so we have:

C (2n-1) <=c (2n-1-1) +1

Use this formula to replace the first item to the right of the inequality to get:

C (2n-1) <=c (2n-2-1) +1+1

Repeat the above step n-2 times to get:

C (2n-1) <=c () +n

The final result is:

C (N) =c (2n) <=n+1<lgn+1

For the general N, the exact conclusion is more complex, but it is not difficult to generalize through the above argument. The time required to find the two points must be within the logarithmic range.

Although it is possible to ensure that the time required for lookups is of a number of levels, BINARYSEARCHST still cannot support us in dealing with large problems with a frequencycounter-like program because the put () method is still too slow. Binary lookup reduces the number of comparisons but does not reduce the time it takes to run because it cannot change the following facts:

In the case where the keys are randomly arranged, the number of times that a symbol table based on an ordered array is required to access the array is the square level of the array length (in practice the arrangement of the keys is not random, but still fits nicely with the model).

proposition: inserting a new element into an ordered array of size n, in the worst case, requires access to the ~2N group, so inserting n elements into an empty symbol table requires access to the ~n2 number of times in the worst case scenario.

Proof: ditto proposition.

3. Summary

In general, binary lookups are much faster than sequential lookups and are the best choice for many practical applications. Of course, binary search is also not suitable for many applications. For example, it cannot process the Leipzig Corpora database because the find and insert operations are mixed, and the symbol table is too large. As we emphasize, modern applications need to be able to support efficient discovery and insertion of symbolic table implementations of both operations. That is, we need to be able to arbitrarily insert (and perhaps delete) key-value pairs while constructing a large symbol table, and also to be able to complete the lookup operation.

" Source Download "

Algorithm-8. Binary search in ordered array

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.