Implementation (Java) and explanation of block search - common data structure

Implementation (Java) and explanation of block search - common data structure

1. Algorithm overview

​Blocking Search is also called index sequential search method . In this search method, in addition to the original table itself, an " index table " needs to be established, that is, the original table is divided into pieces, and the largest one is selected for each piece. The record is used as a key item , and the starting subscript in the block is the pointer item of the block . The index table is ordered according to the keywords, that is, there is an order between blocks, and elements within a block are out of order. When searching, first determine which block the record to be searched is in, and then use the sequential search method to find its specific location in a specific block . Therefore, its performance is between the sequential search method and the binary search method.

Time complexity : no more than O(n)

Space complexity : no more than O(n)

Advantages :

  • When inserting and deleting elements in the table, you only need to find the corresponding block, and you can perform insertion and deletion operations in the block
  • Unordered within a block, easy insertion and deletion without extensive movement
  • It is suitable for scenarios where the linear table needs to be quickly searched and frequently changed dynamically

Disadvantages :

  • Need to add a memory space to store the index table
  • Need to sort the initial index table according to its maximum key (or minimum key)

2. Code implementation

package top.alibra.algorithm;
public class ChunkedLookup {
    
    
    private static int  ChunkedLookupSearch(int a[],BlockTable[] arr,int key){
    
    
        int left=0,right=arr.length-1;
        //利用折半查找法查找元素所在的块,就是索引块  right初使为3
        while(left<=right){
    
    
            int mid=(right-left)/2+left;//采用折半查找
            if(arr[mid].key>=key){
    
    
                right=mid-1;
            }else{
    
    
                left=mid+1;
            }
        }

        if(left> arr.length-1||right<0){
    
    
            //表示在两边,没有合适的
            return -1;
        }

        //循环结束,元素所在的块为right+1 取对应左区间下标作为循环的开始点
        //因为arr[mid].key>=key,所以真实所在块总是比right大一个,所以要+1
        int i=arr[right+1].low;
        //在块内进行顺序查找确定记录的最终位置
        while(i<=arr[right+1].high&&a[i]!=key){
    
    
            i++;
        }
        //如果下标在块的范围之内,说明查找成功,否则失败
        if(i<=arr[right+1].high){
    
    
            return i;
        }else{
    
    
            return -1;
        }
    }

    //测试
    public static void main(String[] args) {
    
    
        //原表
        int a[]={
    
    8,21,11,13,34,41,43,39,49,60,58,47,79,80,77,82};
        //分块获得对应的索引表,这里是一个以索引结点为元素的对象数组
        BlockTable [] arr={
    
    
                new BlockTable(21,0,3),//最大关键字为22 起始下标为0,3的块
                new BlockTable(43,4,7),
                new BlockTable(60,8,11),
                new BlockTable(82,12,15)
        };

        //待查关键字
//        int key=1;
//        int key=9;
//        int key=431;
        int key=43;

        //调用分块查找算法,并输出查找的结果
        int result=ChunkedLookupSearch(a,arr,key);
        System.out.print("数组下标:"+result);
    }

}
 
//索引表结点
class BlockTable{
    
    
    int key;
    int low;
    int high;
    BlockTable(int key,int low,int high){
    
    
        this.key=key;
        this.low=low;
        this.high=high;
    }
}

Guess you like

Origin blog.csdn.net/qq_46138492/article/details/129268960