Common search algorithms

Source: Internet
Author: User

Common search algorithms
Basic Idea of sequential search:

 

From the end of the table, scan the table sequentially, and compare the node keywords scanned with the given value (assumed as a). If the current node keyword is a, the search is successful; if the node with the keyword a is not found after the scan, the search fails.

To put it bluntly, from the start to the end, compare one by one. If you find the same one, it will succeed. If you cannot find the same one, it will fail. The obvious disadvantage is that the search efficiency is low.

It is applicable to the sequential and chained storage structures of linear tables.

 

Int [] aa = {3, 6, 8, 7, 10, 15, 12, 14, 16, 17, 20, 21}; for (int I = 0; I <aa. length; I ++) {if (8 = aa [I]) {
Console. WriteLine ("Find target element ");}}The basic idea of Binary Search:

 

(1) determine the midpoint of the interval: mid = (low + high)/2

Min represents the node location in the middle of the interval, low represents the leftmost node location in the interval, and high represents the rightmost node location in the interval

(2) compare the value of a To be queried with the keyword of the node mid (R [mid]. key is used below). If the value is equal, the search is successful; otherwise, a new search interval is determined:

If R [mid]. key> a, the order of the table is known, R [mid]. the values on the right of the key are greater than a, so if a keyword equal to a exists, it must be in R [mid]. the table on the left of the key. In this case, high = mid-1

If R [mid]. key <a, it is equal to the key of a. If it exists, it must be in the table on the right of R [mid]. key. At this time, low = mid

If R [mid]. key = a, the search is successful.

(3) For the next search interval, Repeat steps (1) and (2)

(4) During the search process, low increases gradually and high decreases gradually. If high is less than low, the search fails.

 

However, you need to sort the table by keywords. Sorting itself is a very time-consuming operation, so the bipartite method is more suitable for sequential storage structures. To keep the table in order, a large number of nodes must be moved in the sequence structure. Therefore, binary search is especially suitable for linear tables that are rarely changed once created and frequently needed to be searched.

Code implementation:

/// <Summary> /// Binary Search /// </summary> /// <param name = "array"> target array (sorted) </param> /// <param name = "a"> Number of queries </param> /// <returns> index of the target number </returns> public int BinarySearch (int [] array, int T) {int low, high, mid; low = 0; high = array. length-1; while (low <= high) {mid = (low + high)/2; if (array [mid] <T) {low = mid + 1 ;} else if (array [mid]> T) {high = mid-1 ;}else {return mid ;}} return-1 ;}
The basic idea of multipart search:  

Blocking Search is also called index sequential Search. It is a search method with performance between sequential search and binary search.

Prerequisites: unordered blocks in a block are ordered.
Method: Perform Two-byte searches for sequential intra-block searches.

A binary search table consists of a linear table with "Segmented Order" and an index table.
1.
Linear table with "Segmented Order"
Table R [1 .. n] are divided into B blocks, the number of nodes in the first B-1 block is s, the number of nodes in the B block is less than or equal to s; the keywords in each block are not necessarily ordered, however, the maximum keyword in the previous section must be smaller than the minimum keyword in the next section, that is, the table is "segmented and ordered.
2.
Index Table
Extracts the largest keyword in each block and its starting position to form an index table ID [l .. b], that is, the maximum keyword for storing block I in ID [I] (1 ≤ I ≤ B) and the starting position of the block in Table R. Since table R is segmented and ordered, the index table is an incremental and ordered table.

Therefore, the index table is searched in sequence or in binary order to determine which node is to be queried. the unordered block can only be searched in sequence.
Using System; using System. collections. generic; using System. text; namespace multipart lookup {class Program {static void Main (string [] args) {int [] aa = {3, 6, 8, 7, 10, 15, 12, 14, 16, 17, 20, 21}; for (int I = 0; I <aa. length; I ++) {if (8 = aa [I]) {Console. writeLine ("Find target element") ;}} int ysnum, zu, ww = 0; for (int aa1 = 0; aa1 <aa. length; aa1 ++) Console. write (aa [aa1] + ""); Console. writeLine (); Console. writeLine ("number of elements: {0}", aa. length); ysnum = aa. length; # region multipart Console. write ("Enter the number of groups:"); zu = Convert. toInt32 (Console. readLine (); int d = aa. length/zu; int [,] fenkuai = new int [zu, d]; Console. writeLine ("multipart:"); for (int z = 0; z <zu; z ++) // perform multipart for (int y = 0; y <d; y ++) {fenkuai [z, y] = aa [ww]; ww ++;} for (int z = 0; z <zu; z ++) // output multipart {for (int y = 0; y <d; y ++) {Console. write ("{0}", fenkuai [z, y]);} Console. writeLine () ;}# endregion # region determines the index table Console of each partition. writeLine ("Keyword:"); int [] suoyin = new int [zu]; int q = 0; int index = 0; for (int z = 0; z <zu; z ++) {int y; for (y = 0; y <d; y ++) {if (q <fenkuai [z, y]) {q = fenkuai [z, y] ;}// Console. write ("{0}", q); // output index table // create index table if (index <zu) {suoyin [index] = q ;} index ++ ;}for (int n = 0; n <zu; n ++) {Console. write (suoyin [n] + "") ;}# endregion # query the Console in sequence in the region block. writeLine ("Enter the value to be searched:"); int kk = Convert. toInt32 (Console. readLine (); int pp = shunxu (suoyin, kk); for (int n = 0; n <d; n ++) {if (kk = fenkuai [pp, n]) {Console. writeLine ("this value is in the element") ;}# endregion Console. readLine ();} public static int shunxu (int [] aa, int k) {for (int a = 0; a <aa. length-1; a ++) {if (k <= aa [a]) {return a ;}} return-1 ;}}}View Code

 

Hash table)  

Hash is one of the best ways to search and retrieve information related to the unique identifier key. The basic principle of a hash table is to convert the given key value to an offset address to retrieve records.

A hash function is used to convert a key to an address. The hash function performs operations on the key to specify a hash value, which indicates the location where the record can be found.

The basic idea of the hash method is: set a table T with a length of m, and use a function to convert the keywords of n records in the dataset into 0 ~ Value in the S-1 range

Hash Table conflicts

Two different keywords are mapped to the same location because the hash function has the same value, which is a conflict.

Resolve hash conflicts

Linked List Method: links all nodes with synonyms in a single linked list. That is, records with the same key and different values are stored in a single-chain table at the same location.

/// <Summary> /// insert records into the hash table, use the linked list method to resolve conflicts // </summary> /// <param name = "a"> </param> /// <param name = "key"> </ param> /// <param name = "Mod"> </param> // <returns> </returns> public bool HashInsert (chaintype [], int key, int Mod) {int I; I = Hash (key, Mod); chaintype pre; chaintype cur; pre = a [I]; cur = a [I]; while (cur! = Null & cur. Key! = Key) {pre = cur; cur = cur. next;}/* Insert the record at the end of the corresponding linked list when it is not found */if (cur = null) {cur = new chaintype (); cur. key = key; cur. next = null;/* Insert the first record in the chain */if (a [I] = null) a [I] = cur; else pre. next = cur; return true;} return false;} public chaintype HashSearch (chaintype [] a, int key, int Mod) {chaintype p; int I = Hash (key, mod); p = a [I]; while (p! = Null & p. Key! = Key) {p = p. Next;} if (p = null) return null; else return p ;}

 

Code call:

SearchA. hashInsert (a, 70, 13); searchA. hashInsert (a, 30, 13); searchA. hashInsert (a, 40, 13); searchA. hashInsert (a, 10, 13); searchA. hashInsert (a, 80, 13); searchA. hashInsert (a, 20, 13); searchA. hashInsert (a, 90, 13); searchA. hashInsert (a, 100, 13); searchA. hashInsert (a, 75, 13); searchA. hashInsert (a, 60, 13); searchA. hashInsert (a, 45, 13); Console. writeLine ("Enter the number to be searched:"); long time1 = System. dateTime. now. ticks; int num = Convert. toInt32 (Console. readLine (); chaintype p = searchA. hashSearch (a, num, 13); if (p = null) Console. writeLine ("{0} does not exist in the data list", num); else Console. writeLine ("the keyword you are looking for is: {0}", p. key); long time2 = System. dateTime. now. ticks; Console. writeLine ("Hash Table query time {0}", time2-time1 );

 

Reference

 
Comparison of several search algorithms

Abstr: searching is to find a specific information element in a large amount of information. In computer applications, searching is a common basic operation. This paper introduces four search algorithms, they are sequential search, binary search, binary sorting tree search, and hash search. The corresponding program code is written in JAVA, and the time complexity and space complexity of searching the same data are compared.

Comparison of various search algorithms?

Binary: the sequence to be searched must be sorted, that is, an ordered sequence.
Hash: Hash search is fast when location conflicts are well resolved. It mainly refers to the selection of hash functions.
Binary sorting tree: If the fruit tree is relatively balanced, the complexity of this query is log (n), but if it is very biased, for example, if you have always inserted only the left son node (this is the same as the linked list), it is worse.

Related Article

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.