common lookup

Simple lookup:

 

     Search in order:

     Starting from one end of the linear table, compare the keyword of each record with the given value in turn. If the keyword of a record is equal to the given value, it means the search is successful, and the record number is returned; if all records in the linear table are After the comparison, if the record with the keyword equal to the given value is not found, it means that the search fails and a failure value is returned.

public class QuerySearch {
	public static int[] Data = { 65, 21, 92, 11, 55, 42, 89, 8, 5, 9, 81, 33, 28, 89, 90, 28, 122, 76, 30, 7 };

	public static int COUNT = 1;

	public static void main(String args[]) {
		int key = 21;
		if (Linear_Search(key)) {
			System.out.println();
			System.out.println("Search Time = " + COUNT);
		} else {
			System.out.println();
			System.out.println("No Found");
		}
	}

	public static boolean Linear_Search(int key) {
		int i;
		for (i = 0; i < 20; i++) {
			System.out.print("[" + (int) Data[i] + "]");
			if ((int) key == (int) Data[i])
				return true;
			COUNT++;
		}
		return false;
	}
}

 

 

     Find in half:



 

     Also known as binary search. This search method requires that the data in the lookup table be stored in a linear structure, and also requires that the data in the lookup table be arranged in order from small to large by keywords.

public class BinarySearch {

	public static void main(String[] args) {
		int[] is = { 1, 4, 7, 12, 23, 34, 45, 78 };
		int index = binarySearch(is, 1);
		if (index < 0) {
			System.out.println("No current value exists");
			return;
		}
		System.out.println(index);
	}

	public static int binarySearch(int[] is, int key) {
		int low = 0, high = 0, mid = 0;
		high = is.length;
		while (high >= low) {
			mid = (high + low) / 2;
			if (is[mid] == key) {
				return mid;
			} else if (is[mid] > key) {
				high = mid - 1;
			} else if (is[mid] < key) {
				low = mid + 1;
			}
		}
		return -1;
	}

}

 

 

 

 

     Binary sorted tree:



 

     A binary sorted tree is either an empty tree, or a binary tree with the following properties:

     (1) If it has a left subtree, all nodes on the left subtree are smaller than the root node data.

     (2) If it has a right subtree, all nodes on the right subtree are greater than the root node data.

     (3) The left and right subtrees are each a binary sorted tree.

 

 Binary sorted tree delete node (with left and right subtrees)

 


delete node without right subtree




 Remove left and right subtrees

 

 

     Index lookup:

     When searching for a large amount of data, the index will take up more space. When updating, not only the main table but also the index table must be updated.

 

     1. Do a lookup in the index table.

     The search process is as follows:

     (1) First, according to the given keyword key, calculate the index value index1 according to the defined function, and find the index item whose index value is equal to index1 in the index table to determine the starting position and length of the corresponding sub-table in the main table .

     (2) Then, according to the start sequence number start obtained from the index table, the keyword key is sequentially searched at the position specified by the main table (that is, the beginning of the sub-table).

 

     2. Insert data into the main table.

     The algorithm for inserting and deleting operations on the index storage structure of the linear table is similar to the search algorithm. The specific process is as follows:

     (1) Look up the index table according to the value of the element to be inserted, and determine the corresponding sub-table.

     (2) Next, according to the keyword of the element to be inserted, the operation of inserting an element is performed in the sub-table.

     (2) After the insertion is completed, modify the length of the corresponding sub-table in the index table.

 

 

     Hash table:



 

     The basic idea of ​​the hash table is: take the keyword key of each element in the linear table as an independent variable, calculate the value of the function through a certain functional relationship h(key), use this value as the subscript of the array, and store the element in the array. into the corresponding array element. 

     The function h(key) is called the hash function, and the value of the function is called the hash address.

     Disadvantage 1: Potential waste of space

     Disadvantage 2: Conflict may occur

 

     Commonly used methods of constructing hash functions:

    >Direct addressing method (the keyword itself or the keyword plus a certain constant is directly used as the address, and the keyword is generally required to be consecutive and not repeated)

    >Remainder by division (generally choose odd or prime numbers for divisors)

    > Numerical analysis (analyzing certain parts of keywords)

    > Squared method (squared)

    >Folding method (split into segments of equal length with keywords and add them together)

    You can construct a suitable hash function according to the characteristics of the data

 

     Ways to handle conflicts:

    >Open address method.



 

    > Link method.



 

 

 

 

 

 

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326527197&siteId=291194637