A search table is a data set Composed of data elements (or records) of the same type. A key is the value of a data item in a data element. It is also called a key value. It can be used to represent a data element or to identify a data item (field) of a record ), it is called a key code. If this keyword can uniquely identify a record, it is called the primary key ). For keywords that can recognize multiple data elements (or records), they are called secondary keys ), secondary keywords can also be understood as keywords that do not uniquely identify a data element (or record). The corresponding data item is the secondary key code.
Searching is a data element (or record) that determines in the query table that the keyword is equal to the given value based on a given value ).
There are two ways to search for a table: static table search and dynamic table search.
Static search table:
(1) query whether a "specific" data element is in the query table.
(2) retrieve a "specific" Data Element and various attributes.
Dynamic search table: inserts data elements that do not exist in the search table during the search process, or deletes an existing data element from the search table.
(1) Insert data elements when searching.
(2) Delete data elements when searching.
This document describes how to search tables in static mode.
I. Sequential table search
Sequential search, also called linear search, is the most basic search technology. Its search process starts with one (or the last) record in the table, compare the record keywords with the given values one by one. If the keywords of a record are the same as the given values, the query is successful and the queried records are found. If the record is not found until the last (or first) record, when the keyword and the given value are not equal, no records are found in the table and the query fails.
Ii. Search an ordered table
1. Half-Lookup
Binary Search is also called binary search. The premise is that records in a linear table must be ordered by key codes (usually from small to large), and linear tables must be stored in sequence. The basic idea of semi-query is: in an ordered table, take the intermediate record as the comparison object. If the given value is the same as the keyword of the intermediate record, the query is successful; if the given value is less than the keyword of the intermediate record, you can continue searching in the left half of the intermediate record. If the given value is greater than the keyword of the intermediate record, you can continue searching in the right half of the intermediate record. Repeat the above process until the search is successful, or no records are found in all the search areas.
2. Interpolation Search
Interpolation Search is a search method after comparing the key of the keyword to be searched with the keyword of the maximum and minimum record in the search table, its core lies in the interpolation formula (key-A [low])/(A [High]-A [low]).
3. Fibonacci search
The core of the Fibonacci search algorithm is
1) when key = A [Mid], the search is successful;
2) When key <A [Mid], the new range is the low to the mid-1, the number of the range is f [k-1]-1.
3) when key> A [Mid], the new range is the m + 1 to the high, the number of the range is f [K-2]-1.
See Figure 8-4-13.
The sample code is as follows: (adapted from big talk data structure)
C ++ code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
|
# Include <iostream> Using namespace STD; # Define Infinitly 65535 # Define Max size 100 Int f [100];/* Fibonacci sequence */ /* Search in an ordered manner without a guard, where arr is an array, n is the number of arrays to be searched, and key is the keyword to be searched */ /* Returns the position of the element pos (subscript + 1 )*/ Int sequential_search (int * arr, int N, int key) { For (INT I = 0; I <n; I ++) If (ARR [I] = key) Return I + 1; Return Infinitly; // An error occurred while returning infinity. } /* Search in the order of the guard */ /* Returns the position of the element pos (subscript + 1 )*/ Int sequential_search2 (int * arr, int N, int key) { Arr [N] = key; Int I = 0; While (ARR [I]! = Key) I ++; Return I + 1; // if n + 1 is returned, the operation fails. } /* Half-lookup */ /* Returns the subscript of the element */ Int binary_search (int * arr, int N, int key) { Int low = 0;/* defines the lowest subscript as the first record */ Int high = n-1;/* defines the highest subscript as the last record */ Int mid; While (low <= high) { Mid = (low + high)/2;/* half */ If (Key <arr [Mid])/* If the search value is smaller than the median value */ High = mid-1;/* The maximum subscript is adjusted to a smaller subscript */ Else if (Key> arr [Mid])/* If the search value is greater than the median value */ Low = Mid + 1;/* the lowest subscript is adjusted to the first digit of the middle subscript */ Else Return mid;/* if they are equal, the mid is the location found */ } Return Infinitly; } /* Interpolation Search */ Int interpolation_search (int * arr, int N, int key) { Int low = 0; Int high = n-1; Int mid; While (low <= high) { /* Interpolation formula */ Mid = low + (high-low) * (key-Arr [low])/(ARR [High]-Arr [low]); If (Key <arr [Mid]) High = mid-1; Else if (Key> arr [Mid]) Low = Mid + 1; Else Return mid; } Return Infinitly; } /* Fibonacci search */ Int fibonacci_search (int * arr, int N, int key) { Int low = 0;/* defines the lowest subscript as the first record */ Int high = n-1;/* defines the highest subscript as the last record */ Int I, K = 0; Int mid; While (n> F [k]-1) K ++; For (I = n-1; I <F [k]-1; I ++) Arr [I] = arr [n-1]; While (low <= high) { Mid = low + F [k-1]-1; If (Key <arr [Mid]) { High = mid-1; K = k-1; } Else if (Key> arr [Mid]) { Low = Mid + 1; K = k-2; } Else { If (mid <= n-1) Return mid; Else Return Infinitly; } } Return Infinitly; } Int main (void) { Int arr [maxsize] = {1, 16, 24, 35, 47, 59, 62, 73, 88, 99 }; Int result = sequential_search (ARR, 10, 24 ); If (result! = Infinitly) Cout <"24's pos:" <result <Endl; Result = sequential_search2 (ARR, 10, 59 ); If (result! = Sizeof (ARR)/sizeof (ARR [0]) Cout <"59's pos:" <result <Endl; Result = binary_search (ARR, 10, 73 ); If (result! = Infinitly) Cout <"73's pos:" <result + 1 <Endl; Result = interpolation_search (ARR, 10, 16 ); If (result! = Infinitly) Cout <"16's pos:" <result + 1 <Endl; F [0] = 0; F [1] = 1; For (INT I = 2; I <100; I ++) { F [I] = f [I-1] + F [I-2]; } Result = maid (ARR, 10, 88 ); If (result! = Infinitly) Cout <"88's pos:" <result + 1 <Endl; Return 0; } |
Output: