Data structure and algorithm series (3) Base lookup algorithm

Source: Internet
Author: User
Tags data structures numeric value

Objective

Data lookup is the basic computer programming work, and people have been studying it for many years. In this section you will see only one aspect of the lookup problem, which is to find it in a list (array) based on the given number.

There are two ways to find data in a list: Sequential lookup and two-in-place lookup. You can use sequential lookups when data items are randomly arranged in a list, and two-fork lookups are used when items are sorted in order in the list.

1. Sequential Lookup algorithm

The most prominent lookup type is to traverse each record sequentially from the beginning of the recordset until the desired record is found or the end of the dataset is reached. This is called the order lookup.

Sequential lookups (also called linear lookups) are very easy to implement. Starts at the beginning of the array and compares each array element that is accessed in turn to the numeric value you want to find. If a matching data item is found, the lookup operation is ended. If a match is still not generated at the end of the array, then the value is not in the array.

The following is a function that performs a sequential lookup operation:

Whether the order lookup exists

public static bool Seqsearch (int[] arr, int svalue)

{

for (int index = 0; index < arr. Length; index++)

{

if (arr[index] = = svalue)

Here you can also return the position of svalue in the array arr returns index;

return true;

}

return false;

}

Very simple base lookup.

1.1 Find minimum and maximum values

Computer programs are often asked to look for minimum and maximum values from arrays (or other data structures). In an ordered array, finding the minimum and maximum values is an easy task. However, in an unordered array, this has a small challenge.

The following starts with how to find the minimum value of an array: first the algorithm:

2. Start looping through the array and compare each successive array element to the minimum variable.

4. Continue the above operation until you have access to the last array element.

1, the number of guessing games is 82.

3, the 2nd time guess 75. The answer is too small.

5, the 4th time guess 81. The answer is too small.

7, the middle point is 82.5, which is approximate to 82.

<p cxsplast "style=" margin:0cm 0cm 0pt 60pt;line-height:17pt;; -ms-word-break:normal ">8, the 6th time guesses 82, the answer is correct."

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.