There are several points for this question to be confirmed first.
- It has to be orderly, and if it's unordered, it's all going to go through.
- Lookup algorithms are related to data structures, and different data structures are suitable for different search algorithms
- The lookup algorithm has a certain relationship to disk I/O, such as when the database is sorted by the index, if each node is read from disk and then judged
Array
If you know the subscript is convenient, the complexity of the search is 1.
If the lookup is for a value, then the sequential traversal is O (n),
Two-point Search
Using binary search can reduce the complexity of time to: O (LOGN)
/** * Binary search, also known as binary lookup, is a highly efficient method of finding. "Two-point lookup Requirements": 1. Sequential storage structure must be used 2. Must be ordered by keyword size. * @author WZJ * * * * Public class binarysearch { Public Static void Main(string[] args) {int[] src =New int[] {1,3,5,7,8,9}; System.out.println (BinarySearch (SRC,3)); System.out.println (BinarySearch (SRC,3,0, src.length-1)); }/** * * Binary search Algorithm * * * * @param srcarray * Ordered array * * @param des * Find element * * @return array subscript for DES, not found return-1 */ Public Static int BinarySearch(int[] Srcarray,intDES) {intLow =0;intHigh = srcarray.length-1; while(Low <= High) {intMiddle = (low + high)/2;if(des = = Srcarray[middle]) {returnMiddle; }Else if(des <srcarray[middle]) {High = middle-1; }Else{low = middle +1; } }return-1; }/** * Binary finds the position of a specific integer in an array of integers (recursion) * @paramdataset * @paramdata * @parambeginIndex * @paramendIndex * @returnindex * * Public Static int BinarySearch(int[] DataSet,intDataintBeginindex,intEndIndex) {intMidindex = (beginindex+endindex)/2;if(Data <dataset[beginindex]| | data>dataset[endindex]| | Beginindex>endindex) {return-1; }if(Data <dataset[midindex]) {returnBinarySearch (dataset,data,beginindex,midindex-1); }Else if(Data>dataset[midindex]) {returnBinarySearch (dataset,data,midindex+1, EndIndex); }Else{returnMidindex; } } }
But insert because all is worth moving after the current node, once, its time complexity is O (n) + O (log n)
Linked list
Can only traverse from the head node, the complexity of the lookup is O (n)
Insert or delete, because only the pointer needs to be moved, the time complexity is O (1) + O (n)
Tree
The search of the tree is mainly the first order traversal, the middle sequence and so on traversal way.
Insert and delete, or faster
Commonly used will have the following derivative methods:
Two-fork Tree
Binary Tree Construction:
Class binarynode{int value; Binarynode left; Binarynode right; Public Binarynode(int value){ This.value=value; This. left =NULL; This. right =NULL; } Public void Add(int value){if(value> This.value){if( This. Right! =NULL){ This. Right.add (value); }Else{ This. right =NewBinarynode (value); } }Else{if( This. Left! =NULL){ This. Left.add (value); }Else{ This. left =NewBinarynode (value); } } }//middle order lookup PublicBinarynodeGet(int value){if( This.value==value){return This; }if( This.value>value){return This. Left.Get(value); }if( This.value<value){return This. Right.Get(value); }return NULL; } }
The complexity of the insertion itself is not high, just a simple node addition. However, because the complexity of finding an insertion position is related to the height of the tree, the Logn may be close to a linear lookup in very poor conditions.
Balanced two-pronged tree
The Balanced binary tree is a two-fork tree that minimizes the number of tall, and its algorithm increases the operation of left-and right-handed operations. Insert complexity will be higher, but will get good search performance.
B+tree
Learn from here
This is about talking about disk I/O related, so in order to reduce disk I/O. You can take advantage of the read-ahead feature of the disk to extract approximately one page-size node into memory at a time.
Let's talk about B-tree first.
A balanced M-way lookup number, which satisfies the following conditions:
- Amount of data in each node < m
- Number of nodes per layer <= m
- The number of child nodes must be completely greater than, less than, or between them. That is, the two values of the parent node cannot be crossed
- Number of values in the leaf node >=M/2
- Number of values in non-leaf nodes = number of child nodes-1
Such as:
As can be seen, three child nodes have two values, three child nodes in the data corresponding to less than, between, greater than this range
B+tree
The difference with the above is:
- All the keywords are in the leaf node
- Parent nodes are stored as pointers to child nodes
- There will be two entrances, one is the root node and the other is a pointer starting from the smallest leaf node
Look for a binary tree compared to the image, because the insertion time is already equivalent to the two-point algorithm, so only need, recursion to find it.
Hash table
To solve some of the objects that are not easy to sort, or find. Like, videos, and so on.
It is used in Java HashMap.
is an array of linked lists
-Hash function for key, hash value, find its corresponding linked list.
-The remaining problem of resolving hash conflicts
-Resolve hash conflicts, which can be compared sequentially after hitting the list
-Here, by the way, a consistent hash. Preset many nodes, select the most recent node deposit, can solve the problem of increasing node data transfer.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Java Lookup algorithm