Imagine how troublesome it is to find the book you need in a pile of scattered books. If you put the book neatly, for example, put it on the shelf, so as to move from left to right or from right to left) it is easy to search for the books you need in sequence.
The scattered pile of books can be understood as a collection, and they are arranged neatly on the shelf, which is equivalent to building a sequential query table, which is easier to find. If the name of a book is sorted in alphabetical order on the basis of neatly arranged columns, an ordered search table is formed.
Sequential Table query: sequential query.
Ordered table search: semi-query, interpolation search, and Fibonacci search.
Now we will focus on sequential search. Sequential search, also known as linear search, is the most basic search technology. Its search process starts with the first or last record in the Table. Zhuge compares the record keywords with the given values, if the keyword of a record is equal to the given value, the query is successful and the queried record is found. If the last or first record is known, the keyword and the given value are not equal, no records are found in the table. The query fails.
# Include <stdio. h> # include <stdlib. h> int arr [10] = {9, 5, 8, 3, 2, 7, 4, 0, 6, 1}; int SequentialSearch (int * a, int n, int key) {int I; for (I = 0; I <n; I ++) {if (a [I] = key) return I ;} return-1 ;}int main () {int addr; int key = 2; addr = SequentialSearch (arr, 10, key); if (addr =-1) printf ("search failed \ n"); else {printf ("search successful \ n"); printf ("Location: % d \ n", addr );} return 0 ;}
This article is from "Li Haichuan" blog, please be sure to keep this source http://lihaichuan.blog.51cto.com/498079/1282314