Search that programmers should understand (implemented in Java)

Source: Internet
Author: User

I wrote a few blog posts about sorting last week and received support from many friends. Thank you very much.

Compared with sorting, today's search is much simpler. Let's talk about it first today:

1,
Sequential search

2,
Half Lookup

 

I. 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.

Calculate the average search length.

For example, in the table above, search for 1 requires 1 time, search for 2 requires 2 times, push down in sequence, we can see that the search for 16 requires 16 times,

As you can see, we only need to sum the search times (for junior high school students, the upper base and the lower bottom multiplied by the height divided by 2), and then divide by the number of points, that is, the average search length.

Set n = number of nodes

Average search length = (n + 1)/2

 

Implemented in Java:

Import Java. util. gradient; public class sequentialsearch {int A [] = {49,38, 65,97, 53,51}; Public sequentialsearch () {system. out. println ("Enter the number to be queried:"); wrote input = new partition (system. in); int input1 = input. nextint (); For (INT I = 0; I <. length; I ++) {if (a [I] = input1) {system. out. println (input1 + ":" + I); break;} if (I =. length-1) system. out. println ("No R Esult! ");}}}

 

Ii. Basic Idea of Binary Search:

 

Prerequisites:

(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 + 1

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 low> high, the search fails.

 

 

Average search length = log2 (n + 1)-1

 

Note: although the binary search efficiency is high, 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.

Therefore, the sequence must be ordered when the semi-query is used!

 

Implemented in Java:

Import Java. util. arraylist; import Java. util. list; public class binarysearch {public binarysearch () {list <integer> List = new arraylist <integer> (); For (INT I = 0; I <10000; I + = 2) {// Add all the even numbers from 1 to the list. As an experiment array, it is obviously ordered! List. add (I); // The array is also available here.} int low = 0; int high = List. size (); int key = 3334; while (low <= high) {int mid = (low + high)/2; If (Key = List. get (MID) {system. out. println ("the position of this value in the list is:" + mid); break;} If (Key> list. get (MID) {LOW = Mid + 1; // when the value is smaller than, the low pointer moves backward, and the high pointer remains unchanged} If (Key <list. get (MID) {high = mid-1; // when the value is greater than, the high pointer moves forward, and the low pointer remains unchanged} If (low> high) {system. out. println ("no results found! ");}}}

 

 

I also learned from a previous blog post. I just use JavaTo achieve this, add the blog address of your predecessors:

Http://blog.csdn.net/shan9liang/article/details/7555811

Of course, you can also go to Yan Weimin's "Data Structure" to learn more about the data structure.

 

 

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.