Sequential Table Lookup algorithm

Source: Internet
Author: User

Sequential table lookup algorithm is the most basic searching technique in the algorithm.

Algorithm principle: In layman's terms, look up data from a table and return the current value index whenever you see the same data as the value you are taking.

On the code:

public  int  seqserch (int []a, int key) {
	int i,n=a.length;
	for (i = 1; i < n; i++) {
            if (a[i] = = key) {
	        return i;
	     }}
}

The code is simple, but when multiple threads or concurrent requests are encountered, the value of I may be out of bounds. So, the program community has a

Well-known Sentinel mode, here is the rewritten code:

public int Seq_serch (int []a, int key) {

	int i;
	A[0] = key;//Sentinel
	i = a.length//starts from the tail
	while (a[i]! = key) {
	     i--;
	}
	Return i;//if return 0 fails to find
}

Summary: Sequential lookup technology is extremely inefficient when it encounters a very large range of lookups. Sentinel mode is recommended when there is a small amount of data available.

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.