Java-preliminary Understanding-fifth-arrays-common operations-find & Binary find

Source: Internet
Author: User

One. Find

You want to know where a certain data is located in the array. (Find a data from an array, the data exists, and return the angular label, no corresponding data, return-1.)

Describes the common operation of an array, which is actually a functional function (for the most value, sort, find). Once a function is mentioned, it involves two explicit, return values and inputs. Here you get the position, which is the index number. We need to know the position of a certain data in an array, and the data and arrays are indeterminate, which is a general solution . Programming is to use a common method to solve the specific problem, the main function is a deterministic problem, functional function is a universal solution. Therefore, there are two parameters in the input. When you write a parameter, you label the type and the variable name.

Always use dialectical thinking to look at the problem, find two kinds of results, find and can not find. When it is found, it returns the value of the corner mark directly, and returns 1 when it is not found. When the array pairs the data, it is compiled from 0, and returns 1, which means the array does not have the data, and there is no corresponding corner label.

One question to note here is whether there is only one corner label for the corresponding data. The reason is the uncertainty of the array, the kind of.

It's strange that return-1 is written outside of the for statement. Let's analyze, (in fact, this involves returning a problem that returns multiple values ), and after defining the method, enter the for statement. For loop inside, if the if is set, it returns one. There are three cases in the for, there are multiple return values, there is a return value, no return value. Regardless of what is in the for, the value of 1 will be returned. But when there is data in the array, the return value is the first corner of the data, the return value is no matter, here is worth pondering . In any case, the end will return a value of 1, if there is a corresponding data, there is a return of the value of the angle, then the 1 does not matter; if there is no return value, then the -1 here is the only return value, it means that there is no corresponding data in the array.

Two.

Given a deterministic array of {4,1,8,7,3,8,2}, if we use the source code above to find the location of data 8, the result shows the first 8 where the corner Mark 2.

Here I have a question, why the output statement output value is not the angle of 5, is 2 after the return, the last value should be 5. I doubt that I understand the FOR Loop statement, is the for loop structure executed for every x? How to quit, is all executed out of the exit, or meet the execution of an X to exit the statement? → The end of the Loop statement is not the end of the loop condition to exit, and the execution of the statement is not related . So, for the two return values here (in fact, three return values, 1 is always to return), the output returned first value , it is worth pondering.

The method named GetIndex in int index=getindex (arr, 8) is not the same as the naming rules for the name of the function and the class name, and the first letter of the class name begins to be capitalized, and the name of the functions waits for the second word.

Three. Binary search

From the perspective of the video, the operation of the data first introduced the search, and also introduced the binary lookup. Why do you introduce two kinds of lookups, that is, the amount of data is different, looking for the program is less data for the situation. Binary lookup is the source code for a lot of data when it is set.

Binary lookups can improve the efficiency of lookups. The ability to perform binary lookup data has a premise and must be orderly.

Examples Show

Scenario 1:

KEY=45, determine the corner label for Data 45.

At the beginning, we want to determine the size of the middle angle, the middle angle Mark = (the first corner mark + tail angle mark)/2, because this is the computation in the Java computer language, so the result of the intermediate corner is definitely not floating point type, is the int type. This series of data is in order of size, but there is no rule, the difference is 1, thinking can not be stiff.

Scenario 2:

KEY=50,

Thinking: Binary find in the design of a few content, first need three variables, need to do the intermediate angle is not to find the element, is large or small, and then continue to carry out binary action, these actions are repeated execution, which is used in the loop.

Java-preliminary Understanding-fifth-arrays-common operations-find & Binary find

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.