Algorithm-7. Sequential search in unordered linked list, unordered Algorithm

Source: Internet
Author: User

Algorithm-7. Sequential search in unordered linked list, unordered Algorithm

1. Basic Ideas

A simple choice for the data structure used in the symbol table is the linked list. Each node stores a key-value pair, as shown in the code in the algorithm. Get () is used to compare the keys to be searched and the keys in each node using the equals () method. If the match succeeds, the corresponding value is returned. Otherwise, null is returned. The implementation of put () is also a traversal table. You can use the equals () method to compare the keys to be searched and the keys in each node. If the matching succeeds, we will use the value specified by the second parameter to update the value associated with the key, otherwise, we will create a new node with the given key-Value Pair and insert it to the beginning of the linked list. This method is also called sequential search: in the search, we traverse all the keys in the symbol table one by one and use the equals () method to find the key that matches the searched key.

2. Specific Algorithms

/*** Algorithm 3.1 sequential search (based on unordered linked list) * Created by huazhou on. */public class SequentialSearchST <Key, Value> {private Node first; // The first Node of the linked list // the definition of the linked list Node private class Node {Key key; Value val; Node next; public Node (Key key, Value val, Node next) {this. key = key; this. val = val; this. next = next ;}// find the given Key and return the associated public Value get (key Key) {for (Node x = first; x! = Null; x = x. next) {if (key. equals (x. key) {return x. val; // hit} return null; // missed} // search for the given Key. If it is found, the value is updated. Otherwise, the public void put (key Key key, value val) {for (Node x = first; x! = Null; x = x. next) {if (key. equals (x. key) {// hit, update x. val = val; return ;}} first = new Node (key, val, first); // missed, new Node }}

The symbol table uses a private internal Node class to save keys and values in the linked list. The get () method searches the linked list sequentially to find the given key (the associated value is returned if the key is found ). The put () implementation also searches the linked list sequentially to find the given key. If it finds the key, it updates the associated value, otherwise, it will create a new node with the given key-Value Pair and insert it to the beginning of the linked list.

3. Algorithm Analysis

Proposition:In the symbol table that contains N pairs of key values based on the (unordered) linked list, the unhit search and insert operations must be compared N times. Hit search requires N comparisons in the worst case. In particular, it is required to insert N different keys to an empty table ~ N2/2 comparisons.

Proof:When we look for a key that does not exist in the table, we will compare each key in the table with the given key. Because duplicate keys are not allowed, we need to find them again before each insert operation.

Inference:Insert N different keys to an empty table ~ N2/2 comparisons.

4. Summary

Finding an existing key does not require a linear time. One measurement method is to find each key in the table and divide the total time by N. When the probability of finding each key in a table is the same, this result is the comparison number required for an average query. We call it a random hit. Although the search mode of the symbol table use case is unlikely to be random, this model can always adapt well. We can easily obtain the average number of comparisons required for random hits ~ N/2: The get () method in the algorithm needs to compare the first key twice, and the second key needs to be compared twice, the average number of comparisons is (1 + 2 +... + N)/N = (N + 1)/2 ~ N/2.

These analyses completely prove that the implementation based on the linked list and sequential search are very inefficient.

 

 

Source code download]

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.