Algorithm 4-6: basic implementation of joined Arrays

Source: Internet
Author: User

This section describes how to implement a key-value table.


Linked List Method


One way is to use a linked list. The basic idea of this method is to store key-value pairs using a linked list. When you need to find a value, scan the entire linked list. If a matched key exists, the corresponding value is returned. When you want to insert a value, scan the entire linked list. If you can find the matching key, it overwrites the corresponding value. If not, add a new key-value pair to the head of the linked list.


The search and insert operations of this algorithm are both complex N and have poor performance.


Code

public class LinkedST
 
  ,Value> {    private class Node {        Key key;        Value value;        Node next;    }     private Node first;     public void insert(Key key, Value value) {        Node node = search(key);        if(node == null) {            node = new Node();            node.key = key;            node.value = value;            node.next = first;            first = node;        } else {            node.value = value;        }    }     public boolean isEmpty() {        return first == null;    }     public Value get(Key key) {        Node node = search(key);        if(node != null)            return node.value;        return null;    }     private Node search(Key key) {        Node node = first;        while(node != null) {            if(node.key.compareTo(key) == 0) {                return node;            }            node = node.next;        }        return null;    }}
 


Array Method


Another method is to use arrays. You can use binary search for the search. Therefore, the complexity of this algorithm is logN, but the insertion complexity is still N. Although it is a little better than the linked list, the insert operation is still too complex to meet performance requirements.


Code

Public class ArrayST
 
  
, Value> {private Key [] keys; // note that key and value must be saved in two arrays. If the key and value are included in an object, Generic Array Creation is triggered. Private Value [] values; private int N; public ArrayST (int capacity) {keys = (Key []) new Comparable [capacity]; values = (Value []) new Object [capacity];} private void debuuplint () {for (int I = 0; I
  
   
= I; j --) {keys [j + 1] = keys [j]; values [j + 1] = values [j];} keys [I] = key; values [I] = value; N ++ ;}} public Value get (Key key) {int I = search (key); // if the corresponding Key is found if (I
   
    
Hi int mid = (hi-lo)/2 + lo; int compare = key. compareTo (keys [mid]); // if the key is small, it indicates that the key to be searched is on the left if (compare <0) {hi = mid ;} else if (compare> 0) {// The Key to be searched is on the right lo = mid + 1;} else {// exactly find the desired key return mid ;}} // not found. Note: if the key is not found, return the position where the key should be inserted instead of N return lo ;}}
   
  
 


Conclusion


Therefore, an algorithm is required to perform search and insert operations with low complexity.


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.