Table of contents
Search in chunks
Blocking Search, also known as index sequential search, is a search method whose performance is between sequential search and binary search. In this search method, in addition to the table itself, an "index table" needs to be established.
Features: Data is stored in blocks, disordered within blocks, and ordered between blocks.
Algorithmic thinking
Calculate the process:
① Determine the block to which the record to be searched belongs in the index table (can be sequential or half-fold)
② Search sequentially within the block
Case 1-Find successfully (sequence)
In the lookup table, the process of searching for key=22 is
the first index table 10<22, continue to search for the next block,
index table 20<22, continue to search for the next block,
index table 30>=22, if 22 exists If so, in the block pointed to by index table 30, the
sequential search starts from the starting position of the block in index table 30 and the position of array subscript 6. If
List[6] is not satisfied, continue to search for the next bit. ,
List[7] = key, search successful.
Case 2 - Lookup failed (sequential)
In the lookup table, the process of searching for key=29 is
the first index table 10<29, continue to search for the next block,
index table 20<29, continue to search for the next block,
index table 30>=29, if 29 exists If so, in the block pointed to by index table 30, the
sequential search starts from the starting position of the block in index table 30 and the position of array subscript 6. If
List[6] is not satisfied, continue to search for the next bit. ,
List[7] is not satisfied, continue to search for the next digit,
List[8] is not satisfied, continue to search for the next digit,
List[9] , exceeds the block range, and the search fails.
Case 3 - Search successful (half price)
Use half to search the index and find the record with key=30:
use half to search the index, mid=(low+high)/2;
①mid=2, key=maxValue; key=30, must be in the block;
②From the index table 30 The starting position of this block, the position with array subscript 6, starts to search sequentially,
③List[8] = 30, the search is successful.
Case 4-Find successfully (halved)
Use half to search the index and find the record with key=19:
Use half to search the index, mid=(low+high)/2;
① mid=2, key=19<30, high = mid -1=1;
② mid= 0, key=19>10, low=mid+1=1;
③ mid =1, key=19<20, high=mid-1=0; ④ When low>high
occurs , in the block pointed to by low Find records in . ⑤ Search sequentially from the starting position of the block 20 in the index table and the position of the array subscript 2, ③List[4] = 19, the search is successful.
Case 5 - Search failed (halved)
Use half to search the index and find the record with key=54:
Use half to search the index, mid=(low+high)/2;
① mid=2, key=54>30, low=mid+1=3; ②
mid= 3, key=54>40, low=mid+1=4;
③ mid=4, key=54>50, low=mid+1=5;
⑤ low>high, and beyond the range of the index table, the search fails
Algorithm implementation
typedef int ElemType;
//索引表
typedef struct{
ElemType maxValue;
int low;
int hiht;
}Index;
//顺序存储实际元素
ElemType List[100];
Block search efficiency analysis (ASL)
In the description of the algorithm idea: Case 1 and Case 2 use the sequential search index table.
In the description of the algorithm idea: Case 3, Case 4 and Case 5 use the half search index table.
ASL = average search length of index table + average search length of block search
example
Suppose that a lookup table of length n is evenly divided into b blocks, each block has s elements. Assume that the average search lengths of index search and intra-block search are L 1 and L s
respectively , then the average search length of block search is ASL=L 1 +L s
Sequential search index table
Search the index table sequentially, then
L 1 = 1 + 2 + . . . + bb \frac{1+2+...+b}{b}b1+2+...+b = b + 1 2 \frac{b+1}{2} 2b+1
Ls = 1 + 2 + . . . + s s \frac{1+2+...+s}{s} s1+2+...+s = s + 1 2 \frac{s+1}{2} 2s+1
ASL = b + 1 2 \frac{b+1}{2} 2b+1 + s + 1 2 \frac{s+1}{2} 2s+1 = s 2 + 2 s + n 2 s \frac{s^2+2s+n}{2s} 2ss2+2s+n,when s= n \sqrt nnWhen, ASL min = n \sqrt nn+1
half search index table
Use half search index table, then
ASL min =⌈log 2 (b+1)⌉+ s + 1 2 \frac{s+1}{2}2s+1
ASLmin≈log2( n s \frac{n}{s} sn+1)+ s 2 \frac{s}{2} 2s
Advantages of block search:
- Insertion and deletion are easier without having to do a lot of moving.
When inserting and deleting data elements in the table, as long as the block corresponding to the element is found, the insertion and deletion operations can be performed within the block.Disadvantages of block search:
- To increase the storage space of an index table and perform sorting operations on the initial index table.
Easy to make mistakes
When performing a halved search on the index table, if the index table does not contain the target keyword, the halved search will eventually stop at low>high, and the search will be performed in the block pointed to by low.