• 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}