分块查找

分块查找_哔哩哔哩_bilibili

 

/* ============================================================================
 * 分块查找(索引顺序查找)
 *
 * 适用条件:查找表"分块有序"
 *   ① 块内无序:每一块内部记录的关键字可任意排列;
 *   ② 块间有序:后一块所有记录的关键字均大于前一块的最大关键字;
 *   ③ 索引表按各块最大关键字 maxkey 递增有序。
 *
 * 基本思想:
 *   先查索引表确定记录所在块(索引表可顺序查找,也可折半查找),
 *   再在块内做顺序查找。
 *
 * 性能:
 *   设表长 n = b*s(b 为块数,s 为每块长度)。
 *   索引表顺序查找:ASL =(b+1)/2 +(s+1)/2 = n/(2s)+ s/2 + 1
 *   索引表折半查找:ASL ≈ log2(b+1)- 1 +(s+1)/2
 *   性能介于顺序查找 O(n)与折半查找 O(logn)之间。
 * ==========================================================================*/
#include <stdio.h>

#define MAXBLOCK  20    // 最大块数
#define MAXSIZE   100   // 查找表最大长度

// ---- 索引表元素类型 ----
typedef struct {
    int maxkey;      // 该块中最大的关键字
    int stadr;       // 该块第一个记录在表中的下标
    int len;         // 该块的长度
} IndexType;

// ---- 记录类型(查找表元素)----
typedef struct {
    int key;         // 关键字域
    int info;        // 元素下标
} ElemType;

// ---- 静态查找表(顺序表):下标从 1 开始 ----
typedef struct {
    ElemType elem[MAXSIZE + 1];
    int length;   // 表长
} SSTable;

/* --------------------------------------------------------------------------
 * ① 索引表用顺序查找确定所在块
 *    ST[] : 索引表(按 maxkey 递增有序)
 *    b    : 块数
 *    R    : 查找表(分块有序)
 *    k    : 待查关键字
 *    返回 : 成功返回记录下标(1 ~ length),失败返回 0
 * ------------------------------------------------------------------------*/
int Search_Idx(IndexType ST[], int b, SSTable R, int k){
    int i = 0, j;
    
    // 第一步:顺序查找索引表,确定所在块
    while(i < b && ST[i].maxkey < k)
        i++;
    if(i >= b)    // k 大于所有块的最大关键字,失败
        return 0;
        
    // 第二步:在块内顺序查找
    j = ST[i].stadr;
    while(j < ST[i].stadr + ST[i].len && R.elem[j].key != k)
        j++;
    if(j < ST[i].stadr + ST[i].len)
        return j;   // 查找成功,返回记录下标
        
    return 0;       // 块内未找到,失败
}

/* --------------------------------------------------------------------------
 * ② 索引表用折半查找确定所在块(块内仍顺序查找)
 *   循环结束后 low 指向满足 ST[low].maxkey >= k 的块
 * ------------------------------------------------------------------------*/
int Search_Idx_Bin(IndexType ST[], int b, SSTable R, int k){
    int low = 0, high = b - 1, mid, j;

    // 第一步:折半查找索引表
    while(low <= high){
        mid =(low + high)/ 2;
        if(k > ST[mid].maxkey)
            low  = mid + 1;     // 目标块在右半区
        else
            high = mid - 1;     // 目标块在左半区
    }
    if(low >= b)                // k 大于所有块的最大关键字,失败
        return 0;

    // 第二步:在块 ST[low] 内顺序查找
    j = ST[low].stadr;
    while(j < ST[low].stadr + ST[low].len && R.elem[j].key != k)
        j++;
    if(j < ST[low].stadr + ST[low].len)
        return j;
    
    return 0;
}

// 打印查找表和索引表
void PrintTables(SSTable R, IndexType ST[], int b){
    int i;
    printf("查找表 R(关键字,下标从 1 开始,每 6 个为一块):\n  ");
    for(i = 1; i <= R.length; i++){
        printf("%3d", R.elem[i].key);
        if(i % 6 == 0 && i != R.length)
            printf("\n  ");
    }
    printf("\n\n索引表 ST:\n");
    printf("  块号 | 最大关键字 | 起始下标 | 块长\n");
    printf("  -----+------------+----------+----\n");
    for(i = 0; i < b; i++)
        printf("   %d   |     %2d     |    %2d    | %2d\n",
               i + 1, ST[i].maxkey, ST[i].stadr, ST[i].len);
    printf("\n");
}

int main(){
    const int keys[18] = {
        22, 12, 13,  8,  9, 20, //maxkey=22
        33, 42, 44, 38, 24, 48, //maxkey=48
        60, 58, 74, 49, 86, 53  //maxkey=86
    };
    const int n = 18, b = 3;

    // ---- 构造查找表(下标从 1 开始)----
    SSTable  R;
    R.length = n;
    for(int i = 1; i <= n; i++){
        R.elem[i].key  = keys[i-1]; //关键字
        R.elem[i].info = i; //下标
    }

    // ---- 构造索引表(每块长度均为 6)----
    IndexType ST[MAXBLOCK];
    ST[0].maxkey = 22;  ST[0].stadr = 1;  ST[0].len = 6;
    ST[1].maxkey = 48;  ST[1].stadr = 7;  ST[1].len = 6;
    ST[2].maxkey = 86;  ST[2].stadr = 13; ST[2].len = 6;

    PrintTables(R, ST, b); // 打印查找表和索引表

    // ---- 查找测试 ----
    const int test[] = {22, 24, 49, 86, 53, 23, 100};
    const int nt = 7;

    printf("============== 查找测试 ==============\n");
    for(int i = 0; i < nt; i++){
        int pos = Search_Idx(ST, b, R, test[i]);

        if(pos > 0)
            printf("Search_Idx     查找 %3d:成功,位于下标 %2d\n",
                   test[i], pos);
        else
            printf("Search_Idx     查找 %3d:失败(表中无此关键字)\n", test[i]);

        pos = Search_Idx_Bin(ST, b, R, test[i]);
        if(pos > 0)
            printf("Search_Idx_Bin 查找 %3d:成功,位于下标 %2d\n", test[i], pos);
        else
            printf("Search_Idx_Bin 查找 %3d:失败(表中无此关键字)\n", test[i]);
    }
    return 0;
}

 

posted @ 2026-10-30 07:33  董晓  阅读(14)  评论(0)    收藏  举报