8.3 sequential table search

Source: Internet
Author: User

If we just put the books on the shelf, it is still difficult to find a book, that is, to search for the table in the order of section 8.2, but if we are sorting out the shelves, it is easier to find a book by sorting the books by the name in pinyin. To put it simply, books are ordered. When a linear table is ordered, it is always helpful for searching.

8.3.1 half Lookup

Semi-query: binary search ). The premise is that the records in a linear table must be key code orders usually grow from small to large), and linear tables must be stored in order. The basic idea of semi-query: in an ordered table, select 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, search continues in the left half area of the intermediate record. If the given value is greater than the keyword of the intermediate record, search continues in the right half area of the intermediate record. Repeat the above process until the search is successful, or no records are found in all the search areas.

 

# Include <stdio. h> # include <stdlib. h> int arr [10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; int BinarySearch (int * a, int n, int key) {int low; int high; int mid; low = 0; high = n-1; while (low <= high) {mid = (low + high) /2; if (key <a [mid]) high = mid-1; else if (key> a [mid]) low = mid + 1; elsereturn mid ;} return-1 ;}int main () {int addr; int key = 2; addr = BinarySearch (arr, 10, key); if (addr =-1) printf ("search failed \ n"); else {printf ("search successful \ n"); printf ("Location: % d \ n", addr );} return 0 ;}

8.3.2 interpolation Lookup

Our new question is, why do we have to fold half, instead of 1/4 or more.

For example, if you look for "apple" in an English dictionary, you will subconsciously go to the top pages of the book instead of starting from the middle of the dictionary.

Interpolation Search: This method compares the keyword of the maximum and minimum records in a table based on the keyword key.

Mid = (low + high)/2 = low + 1/2 (high-low );

Interpolation Search: mid = (key-a [low])/(a [high]-a [low]) * (high-low );

8.3.3 Fibonacci search

The golden splitting principle is used.

Fibonacci series: 0 1 1 2 3 5 8 13 21 34 ......

# Include <stdio. h> # include <stdlib. h> int arr [10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; int F [10] = {0, 1, 1, 2, 3, 5, 8, 13, 21, 34}; int maid (int * a, int n, int key) {int low; int high; int mid; int I; int k; low = 0; k = 0; high = n-1; while (n> F [k]) k ++; for (I = n; I <F [k]-1; I ++) a [I] = a [n-1]; while (low <= high) {mid = low + F [k-1]-1; if (key <a [mid]) {high = mid-1; k = k-1 ;} else if (key> a [mid]) {low = mid + 1; k = k-2;} else {if (mid <n-1) return mid; elsereturn n-1 ;}} return-1 ;}int main () {int addr; int key = 2; addr = maid (arr, 10, key); if (addr =-1) printf ("search failed \ n"); else {printf ("search successful \ n"); printf ("Location: % d \ n", addr );} return 0 ;}

Comparison of the three algorithms

Half-fold search: mid = (low + high)/2 = low + 1/2 (high-low); // addition, multiplication

Interpolation Search: mid = low + (key-a [low])/(a [high]-a [low]) * (high-low ); // multiplication and division.

Fibonacci search: mid = low + F [k]-1; // Addition

The Fibonacci algorithm is highly efficient.

 

This article is from "Li Haichuan" blog, please be sure to keep this source http://lihaichuan.blog.51cto.com/498079/1282331

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.