插值搜索不适用于特定系列的10个数字 [英] Interpolation search not working on a particular series of 10 number

查看:74
本文介绍了插值搜索不适用于特定系列的10个数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include<stdio.h>
#include<stdlib.h>
int main()
{
        int lo,key,m,i,hi;
        int arr[10];
        printf("enter the elements of array\n");

        for(i=0; i<10; i++)
        {
                scanf("%d",&arr[i]);
        }
        printf("enter the key\n");
        scanf("%d",&key);
        lo = 0;
        hi = 9;
        while(hi >=lo)
        {
   
                m = lo+((hi-lo)/(arr[hi]-arr[lo]))*(key-arr[lo]);
                if(key == arr[m])
                {
                        printf("key found at location : %d",m);
                        return 0;
                }
                else if(key < arr[m])
                        hi = m-1;
                else
                        lo = m+1;

        }
                printf("key not found\n");
        return 0;
}





我的尝试:



它正常用于其他系列,但是当我运行程序然后放入这个系列时就出现了一些问题。

输入:1,3,5,7,9,11,15 ,16,17,18

键值:12

期望o / p =未找到密钥

实际o / p =分段失败



What I have tried:

it work normally for other series but when i run the program and put this series then there some problem.
input : 1,3,5,7,9,11,15,16,17,18
key value : 12
desired o/p = key not found
actual o/p = segmentation fail

推荐答案

问题是,在某些时候, m 超出范围0-9。

您需要在纸上查看您的配方并尝试5,12,17 ...



当您不明白您的代码是什么时做或为什么它做它做的,答案是调试器

使用调试器来查看你的代码在做什么。它允许你逐行执行第1行并在执行时检查变量,它是一个令人难以置信的学习工具。



调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]



调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做的比较。

调试器中没有魔法,它没有找到错误,它只是帮助你。当代码没有达到预期的效果时,你就会接近一个错误。
The problem is that at some point, m become out of range 0-9.
You need to review your formula on paper and try it with 5, 12, 17...

When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. It allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


这篇关于插值搜索不适用于特定系列的10个数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆