Algorithm 1: Sequential search

Function: Checks whether the specified data key exists in array A

Arguments: a is an array; N is the number of arrays a; Key is the keyword being searched

Return value: If the search is successful, the subscript of the key is returned. Unsuccessful, return -1.

Int find_seq(int a[], int n, int key) {int I; for(i = 0; i < n; i++) if(key == a[i]) return i; return -1; }Copy the code

Algorithm 2: binary search

Low lower; High upper bound; Mid = (low + high) / 2

K = a[mid] : The search succeeds. Return mid

K < a[mid] : high = mid-1 continue

K > a[mid] : low = mid + 1

Low > high: The search fails

Int find_bin(int a[], int n, int key) {int low = 0, high = n-1, mid; While (low <= high) {mid = (high + low) / 2; if(key == a[mid]) return mid; if(key < a[mid]) high = mid - 1; else low = mid + 1; } return -1; }Copy the code

The main function

int main() { int f; 15,26,37,45,48,52,60,66,73,90 int a [10] = {}; f = find_bin(a, 10, 37); if(f ! = 1) printf(" %d\n", f); Else printf(" Search failed!" ); return 0; }Copy the code