8.2 sequential table search

Source: Internet
Author: User

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

Related Article

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.