# Search algorithm # [1] simple search: sequential and semi-sequential search

Source: Internet
Author: User
• Sequential search

Starting from the end of a linear table, the keywords of each record are compared with the given value. If the keywords of a record are equal to the given value, the query is successful and the record serial number is returned; if all records in the linear table are compared, and no records with the same keyword as the given value are found, the query fails and a failure value is returned.

• Half-Lookup

It is also called binary search. This search method requires that the data in the query table be saved in a linear structure, and that the data in the query table be sorted by keywords in ascending order.

 

Sequential search is relatively simple. You can compare each element in the array with the element to be searched. No code descriptions

Semi-query is a recursive process that reduces the search range by half once. When the search range is reduced to only one element, this element is not the element to be searched, the search fails.

Semi-query instructions are as follows:

  

 

The code for searching in half is as follows:

1 # include <stdio. h> 2 # define length 10 3 int source [length] = {10, 12, 28, 37, 54,65, 69,86, 90,98}; 4 5 // binary search, search for the position of the key in raw data a 6 int binarysearch (int A [], int N, int key) {7 int low, high, mid; 8 9 low = 0; 10 high = n-1; 11 12 while (low <= high) {13 mid = (low + high)/2; // obtain the minimum and maximum Center 14 if (a [Mid] = Key) for each intermediate number. // The searched element is exactly equal to the center value 15 return mid; 16 else if (Key <A [Mid]) // The searched element is smaller than the median value, and 17 High = mid-1 is found in the range of the minimum value and the last median value; 18 else19 low = Mid + 1; // The searched element is greater than the median value. The median value and the maximum value range are 20} 21 22 return-1; 23} 24 25 int main () {26 int I, key, Pos; 27 printf ("Raw data: \ n"); 28 29 for (I = 0; I <length; I ++) 30 printf ("% d", source [I]); 31 32 printf ("\ n enter the data to be searched:"); 33 scanf ("% d ", & Key); 34 35 Pos = binarysearch (source, length, key); 36 37 If (Pos>-1) 38 printf ("\ n search successful, the search element is located at % d ", POS); 39 else40 printf (" \ n search failed "); 41 42}

 

 

  

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.