【数据结构与算法】【查找】插值查找的代码实现

本文介绍了一种基于折半查找优化的插值查找算法,详细解释了插值查找的计算公式及其应用场景。当查找表较长且关键字分布均匀时,插值查找能提供更佳的性能表现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

插值查找(Interpolation Search):是根据要查找的关键字key与查找表中最大最小记录的关键字比较后的查找方法,其核心在于插值的计算公式。
插值计算公式(折半查找为1/2):(key - a[low]) / (a[high] - a[low])
mid计算公式:mid = low + (high - low) *  (key - a[low]) / (a[high] - a[low]);

插值查找是基于折半查找进行了优化的查找方法。
优势在于:当表长较大,而关键字分布又比较均匀的查找表来说,插值查找算法的平均性能要比折半查找要好得多。

代码实现:

// Filename: interpolation_search.c

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include "public.h"

// 计数器,评估性能使用
extern int count;

// 插值查找
// a[n]为待查找数组,a[0]不使用,n为数组长度(不包含a[0])
// 查找成功,则返回key所在的地址;查找失败,则返回0
int InterpolationSearch(int a[], int n, int key)
{
    int low = 1, high = n, mid;
    count = 0;

    while (low <= high)
    {
        count++;
        mid = low + (high - low) * (key - a[low]) / (a[high] - a[low]);
        if (key < a[mid])
        {
            high = mid - 1;
        }
        else if (key > a[mid])
        {
            low = mid + 1;
        }
        else
        {
            return mid;
        }
    }
    return 0;
}

// 插值查找主函数
int InterpolationSearchMain()
{
    int value;
    int retval;
    int arr[12] = {0xFF, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, -1};

    printf("插值查找演示(a[0]未使用,不参与排序): \n");

    printf("静态查找表数据:\n");
    PrintArray(arr, sizeof(arr) / sizeof(int));
    printf("\n");

    while (1)
    {
        // 查找元素
        printf("请输入要查找的数据: \n");
        scanf("%d", &value);
        // 退出查找
        if (0xFF == value) break;

        retval = InterpolationSearch(arr, 10, value);

        if (0 == retval)
        {
            printf("查找失败! 总计比较次数: %d.\n", count);
        }
        else
        {
            printf("查找成功,元素所在位置: %d. 总计比较次数: %d.\n", retval, count);
        }
    }

    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值