Manpower necessary super Detailed search algorithm (implemented explanation, related features JAVA)

Reference article: https://www.cnblogs.com/maybe2030/p/4715035.html#_labelTop

Binary search Reference: https://blog.csdn.net/lovesummerforever/article/details/24588989

1. binary search

Precautions : binary search prerequisite is the need for an orderly sequence table storage for static lookup tables, no change after a sort, binary search can get good efficiency. But the need for frequent insertion or deletion of data collection, the maintenance of orderly sequencing will bring no small amount of work, it is not recommended . - "lying Data Structure"
Complexity Analysis : in the worst case, the number of keywords comparison log2 (n + 1), and the desired time complexity is O (log2n);
ideas : to begin with the given value intermediate node Compare, if not get results, then the result of the comparison and then narrow
end judgment : the main is to find less time to determine how the general attention min> = max on the line, there are other cases, refer to the article: HTTPS: // Blog. csdn.net/yefengzhichen/article/details/52372407#commentBox

  1. Recursion
/**
 * 针对数组是递增的
 * 返回-1代表没有找到该值
 * 查找成功返回数组下标
 */
int binarySearch_recursion(int[] array,int target,int min,int max){
        if(min<0||max>array.length){
            System.out.println("超出查找范围");
            return -1;
        }
        int mid=(min+max)/2;
        if(target==array[mid]){
            return mid;
        }
        if(min>=max){
            return -1;
        }else if (target>array[mid]){
            return binarySearch_recursion(array,target,mid+1,max);
        }else {
            return binarySearch_recursion(array,target,min,max-1);
        }
    }
  1. cycle
int binarySearch_loop(int [] a , int target){
        int max=a.length-1;
        int min=0;
        while(max>min){
            int mid=(max+min)/2;
            if(target==a[mid]){
                return mid;
            }else if(target>a[mid]){
                min=mid+1;
            }else {
                max=mid-1;
            }
        }
        return -1;
    }

2. Find interpolation (based on a binary search, is ordered to find)

Before introducing interpolation search, first consider a new problem, why the algorithm described above must be a binary, rather than fold or quarter fold more?
For example, at a young age to 100 elements between the range of 1 to 10,000 large array of uniformly distributed Find 5, we will naturally consider a small start looking from the array subscript.
After the above analysis, this binary search to find the way, is not adaptive (that is to say fool). Binary search to find the point is calculated as follows:
MID = (High + Low) / 2, i.e. Low +. 1 = MID / 2 (High-Low); *

By analogy, we can point to find improvements are as follows:
MID = Low + (A-Key [Low]) / ([High] A -a [Low]) * (High-Low),

I.e. the parameters of the above-described ratio improved to 1/2 adaptive, depending on the position in which the keyword in the entire ordered list, so that the change is closer to the mid value keywords key, so that it reduces the number of comparisons indirectly .

1. Application: The average performance of the algorithm is much better than binary search for a greater length of the table, and keyword distribution and relatively uniform look-up table, the interpolation Find

2. Complexity Analysis: Find success or failure time complexity are O (log2 (log2n)).

3. It is noted that since the denominator may find less than or equal to 0, is determined to be before the mid = low + (key-a [low]) * (high-low) / ([low] [high] a -a) whether min> = max.

  int insertionSearch(int[] array,int target,int min,int max){
        if(min<0||max>array.length){
            System.out.println("超出查找范围");
            return -1;
        }
        if(min>=max){
            return -1;
        }
        int mid=min+(target-array[min])/(array[max]-array[min])*(max-min);
        if(target==array[mid]){
            return mid;
        }else if (target>array[mid]){
            return insertionSearch(array,target,mid+1,max);
        }else {
            return insertionSearch(array,target,min,max-1);
        }
    }

(C language version): Reference article https://www.cnblogs.com/lpfuture/p/7112450.html

3. Fibonacci Find

Fibonacci number: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89 ...... (from the third number, the number behind each of the two numbers are the top and f (k) = f (k-1) + f (k-2);

1. The basic idea : are half a poppet algorithm to find, with increasing Fibonacci column, the ratio of two numbers will be closer to around 0.618, select Find points in the number of columns by use of the concept of the golden ratio Find, improve search efficiency. Similarly, Fibonacci Find also belongs to an ordered search algorithm.

2. advantages : Fibonacci lease time or complexity of lookup (log2n) O, but compared with a binary search, to find Fibonacci deed advantage is that it only involves addition and subtraction without division, and the division ratio of added subtraction to take more time, and therefore, in theory, Fibonacci runtime deed look smaller than binary search, but still have to, as the case may be,

3. Note the point : The reason fid [k] -1 to cut first, to comply with the subscript starts at 0, in fact, is to let the Fibonacci element deed as a marker instead of the middle of a marked dichotomy, do not want too complicated to write when I remember expansion arrays.

 //生成斐波那契数列
    int [] produceFib(int size){
        int [] fid=new int[size];
        fid[0]=1;
        fid[1]=1;
        for (int i = 2; i <size ; i++) {
            fid[i]=fid[i-1]+fid[i-2];
        }
        return fid;
    }
    //查找,返回-1表示找不到该值,
    int fibonacciSearch(int [] data,int target){
        int min=0,max=data.length-1;
        int mid=1,k=0;
        int [] fid=produceFib(100);
        //找到有序元素个数在斐波那契数列中最接近的值
        while(max>fid[k]-1){
            k++;
        }
        //扩充数组
        data= Arrays.copyOf(data,data.length*2);
        int s=fid[k]-1;
        //补齐有序表
        for (int i = max+1; i <fid[k]-1 ; i++) {
            data[i]=data[max];
        }

        while (min<=max){
            mid=min+fid[k-1]-1;//根据斐波那契数列进行黄金分割

            if(data[mid]==target){
                if(mid<=data.length-1){
                    return mid;
                }else {
                    return data.length-1;
                }
            }
            if(data[mid]>target){
                max=mid-1;
                k=k-1;
            }
            if(data[mid]<target){
                min=mid+1;
                k=k-2;
            }
        }
        return -1;
    }

Test case

int data[] = {1,3,5,7,9,11,13,15,17,19,21};

Refer to this article for more about the tree:
https://blog.csdn.net/pengzhisen123/article/details/79927377

4. Query tree table

Where we talk about the most simple numerical table lookup algorithm - Binary Find

1) Origin: balanced binary tree is based on the binary tree data structure to find strategies to improve the speed of the dichotomy of the data;

2) Features:
balanced binary tree is thought dichotomy data at regular data assembled into a tree structure, the tree structure data retrieved irrelevant data reduction, greatly enhance the speed of data retrieval; balanced binary tree data structure assembly process has the following rules:

Non-leaf nodes exist only allows up to two child nodes, each data distribution rule is non-leaf nodes of the current node's value is less left child node, the child node on the right is greater than the value of the current node (where the value is based on its own algorithm rules given, such as hash value);

Achieve too much code, you can look at my other blog post (only balanced binary tree) https://mp.csdn.net/mdeditor/97815403#

5. hash lookup

  1. A more detailed article: https://blog.csdn.net/xiaoxik/article/details/74926090
  2. Definitions: By a transformation relationship, so that an appropriate key sequence to the dispersion structure specified size, the more dispersed, smaller the find later time complexity, the spatial complexity is higher.
  • Common areas:
    1) the information security field: the Hash algorithm may be used as the encryption algorithm. . .
    2) Data Structure Field: Hash algorithm is usually used as a quick lookup.
  1. Features:
    1) Flexible hash function is an image, so set the hash function is very flexible, so long as the hash function value thus obtained fall within the range of the long table to allow any of the keywords.
    2) conflicts
    may get different keywords in the same hash address, that is, but, a phenomenon known as conflict.
    3) Hash is a typical space for time algorithm.

  2. Constructor (fetch only the more common, more articles can see the previous reference):

  • Middle-square method:
    1) Implementation: first find the square values of the key, then press the need to take the middle of the square as a hash value of several addresses because: the middle of the square in a few keywords and each is associated, therefore, different keywords will produce different hash addresses with high probability.
    2) Example:Here Insert Picture Description

  • Method random number
    1) Implementation: selecting a random function, the random function value taken as the keyword Address Hash
    2) features: Typically, when a keyword of unequal lengths, constructed using this method is more suitable Hash function.

  • Method I except specimens:
    1) Implementation: taking a keyword is not greater than the remainder after a number p of length m has a Hash table Hash address.
    2) Characteristics: This is the simplest and most common Hash function construction method. Directly modulus, may then take out method after molding process, folding process.
    3) Note that point, except when using specimens from more than law, the choice of p is very important, if the p option would be prone to bad synonymous. It is known from experience: p is preferably not more than selecting a prime number m of length of the table, or may not contain composite number of prime factors of less than 20.

  1. Conflict
    1) Definition: The address is set assuming that the hash table, conflict means there has been recorded by a hash address key derived for the position, the "conflict" is to find another record for the keyword "empty" hash address.
    2) Method:
  • Open-addressable:
    a hash function; length of the hash table entry;
    incremental sequence, there are three emulated:
    1) linear probing re-hash;
    2) detecting the secondary re-hash;
    3) and then detecting scattered pseudorandom columns;

  • And then hashing:
    are different hash function, i.e. the hash function is calculated address when the other synonyms address conflict until the conflict is no longer occur, this method is difficult to produce aggregated, but increase computation time

  • Chain address method:
    all synonyms of keywords stored in the same recording linear table. That does not directly address the existence of hash Hash out the Key, but the Key to store a list, when a collision occurs, the synonymous Key to join the list;

Here Insert Picture Description

  • Public overflow area:
    you can establish a common overflow area used to store the conflicting Key. For example, to set up another hash table, designed to store a synonym for conflict arise.
  1. The java HashMap: When using a linked list to avoid hash collision method (the same hash value), when the chain length is greater than TREEIFY_THRESHOLD (the default is 8), is converted to red-black tree to the list, of course, less than UNTREEIFY_THRESHOLD (default 6), will turn back to the list in order to achieve balanced performance. We see a HashMap data structure (array + + red-black tree list) to better understand the table, the following is an illustration.
    Reference article ·: https://www.jianshu.com/p/ee0de4c99f87

7.

7 ... HashMap extends to the Object object (mainly: coverage equals method is often necessary to also cover hashCode method, because you must ensure that the object equals, hashCode must be the same! )

  • I do not know if you remember the Object equals () and hashCode () method do? (Java - Object class), Object describes the channel:
    the hashCode (): Speaking of General Contract: equals object must have the same hash code.
    equals () method, said: override this method usually necessary to override the hashCode () method, in order to maintain its general contract;

  • First, to ensure that the ancestor class Object described above, then:
    () Default from using the equals == Object is judged;
    the hashCode () is a native method, which is directly dependent on the calculated C.
    So do the children and grandchildren of must follow the ancestors! :
    That if you do not follow it? It will result in the use of data types Hash functions mistakes! ! ! You would not take such a data structure of the hash table! ! !
    What could go wrong? Why wrong? We have seen above the HashMap source, you can easily know:
    lead to miscalculation index, covering the operation failed!

  • The reason:
    Suppose you name equals coverage that is equal to equal (equal to Joe Smith Joe Smith), but you do not cover hashCode ();
    PUT (key) operation, because of your new key and Central Plains and some old key HashMap are two different object, even though they are equals, but due to inherited from the Object hashCode () method gives two different hashCode, hashCode in accordance with the calculated index this step, they belong to two different index!
    This is a direct result of this operation is that once covered, but made the added value of the operation!
    Therefore, to avoid this problem, you must rewrite the hashCode while rewriting equals () of () to ensure that the object equals the HashCode consistent.
    You can see these API classes provided by the official, if it needs to cover equals () so while also covering the hashCode (), so we have to write class.

Comparing the hash and binary tree

Hash: hash table lookup faster, but to consume more memory. So to find more suitable for relatively high speed, sufficient memory space and time

Balanced binary tree: method for establishing balanced binary tree if you want to find faster in the future when using a binary search of the words of advice.

Find a variety of algorithms comparison chart

Here Insert Picture DescriptionNotes:
BST is a binary search tree

Published 36 original articles · won praise 11 · views 10000 +

Guess you like

Origin blog.csdn.net/s_xchenzejian/article/details/97697866