Index search (index search, block search) C language implementation

Source: Internet
Author: User

1. Basic Concepts


Index search is also called hierarchical search.

The basic idea of index storage is: first, divide a set or linear table (which corresponds to the primary table) into several logical sub-tables according to certain functional relationships or conditions, create an index for each sub-table.

These index items constitute an index table of the master table. Then, you can store the index table and each sub-table in sequence or by link.

The index table type can be defined as follows:

Struct IndexItem {IndexKeyType index; // IndexKeyType is the pre-defined index value type int start; // The subscript position int length of the first element in the sub-table; // length field of the sub-Table}; typedef struct IndexItem indexlist [ILMSize]; // ILMSize is an integer constant defined in advance, which is greater than or equal to the number of index items m

The type of the primary table can be defined as follows:

Typedef struct ElemType mainlist [MaxSize]; // MaxSize is an integer constant defined in advance, greater than or equal to the number of elements in the master table n


Each index item in the index table corresponds to multiple records, which is called a sparse index. If each index item uniquely corresponds to one record, it is called a dense index.


2. Index Search Algorithm


Process:

First, based on the given index value K1, find the index item whose index value is equal to K1 on the index table to determine the start position and length of the corresponding sub-table in the master table, then, based on the given keyword K2, find the corresponding sub-table

Element with the keyword equal to K2.

Set array A to A master table with the mainlist type, array B to an index table created on master table A with the indexlist type, and m to the actual length of index table B, that is, the number of index items, K1 and K2 are given

The index value and keyword with the search, and assume that each sub-Table uses sequential storage, the index search algorithm is:

Int Indsch (mainlist A, indexlist B, int m, IndexKeyType K1, KeyType K2) {// use index B of the primary table A and the size of the primary table m to find the index value K1, record whose keyword is K2 // returns the subscript position of the record in the master table. If the search fails,-1 int I, j; for (I = 0; I <m; I ++) if (K1 = B [I]. index) break; if (I = m) return-1; // search failed j = B [I]. start; while (j <B [I]. start + B [I]. length) {if (K2 = A [j]. key) break; else j ++;} if (j <B [I]. start + B [I]. length) return j; // else return-1; // search failed} If IndexKeyType is defined as a string type, the corresponding condition in the algorithm is changed to strcmp (K1, B [I]. index) = 0; similarly, if the KeyType is defined as A string type, the corresponding condition in the algorithm should also be changed to strcmp (K2, A [j]. key) = 0 if each sub-Table uses link storage in master table A, you only need to modify the while loop in the above algorithm and the if statement after it as follows: while (j! =-1) // use-1 as A null pointer to mark {if (K2 = A [j]. key) break; else j = A [j]. next;} return j;

If index B is a dense index, it is simpler. You only need to search for index B and return B [I]. start directly when the index is successful.


Index search and analysis:

The number of index queries equals to the sum of the number of times the index table is compared in the algorithm and the number of times the corresponding sub-table is compared. Assume that the index table length is m and the sub-table length is s,

The average search length of the index query is:

ASL = (1 + m)/2 + (1 + s)/2 = 1 + (m + s)/2

Assuming that each sub-table has the same length, that is, s = n/m, ASL = 1 + (m + n/m)/2, when m = n/m, (that is, m = √ ▔ n, s is also equal to √ ▔ n), ASL = 1 + √ ▔ n is the minimum, and the time complexity is O (√ ▔ n)

It can be seen that index search is faster than sequential search, but less than binary search.

In index storage, it is not only easy to find a single element, but also more convenient to find all the elements in a sub-table. If each sub-table in the master table is reserved with a free location, the index storage is also easy to insert and delete operations.


3. multipart search


Block lookup is an index lookup, and the index table corresponding to it is a sparse index. Specifically, block lookup requires that each sub-table (also called a block) in the master table be incremented (or decreased) ordered. That is, the maximum keyword in the first block must be

Smaller than the minimum keyword in the last block, but the arrangement of elements in the block can be unordered. It also requires that the index value is the maximum keyword in each part.


Is an example of the main table and index table used for block search:



The block search algorithm is similar to the index search algorithm above, as shown in the following code: <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + PC9wPgo8cHJlIGNsYXNzPQ = "brush: java;"> int Blocksch (mainlist A, indexlist B, int m, KeyType K) {// use primary table A and index table B with the size of m to find records int I, j; for (I = 0; I <m; I ++) if (K <= B [I]. index) break; if (I = m) return-1; // search failed j = B [I]. start; while (j <B [I]. start + B [I]. length) {if (K = A [j]. key) break; else j ++;} if (j <B [I]. start + B [I]. length) return j; else return-1;} if the index table is not a sequential query, it is a binary query. You need to replace the for loop statement in the algorithm with the following program segments: int low = 0, high = m-1; while (low <= high) {int mid = (low + high)/2; if (K = B [mid]. index) {I = mid; break;} else if (K <B [mid]. index) high = mid-1; else low = mid + 1;} if (low> high) I = low; when the binary search fails, the low value should be assigned to I. In this case, B [I]. index is the index value just greater than K. Of course, if the low value is m, it indicates that the real search failed.

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.