K,V>在排序列表&LT的钥匙二进制搜索; [英] Binary Search on Keys of SortedList<K, V>

查看:219
本文介绍了K,V>在排序列表&LT的钥匙二进制搜索;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要编写的线性插值一些代码,我试图找出最有效的方式来搜索排序列表<的钥匙; K,V> 的。上,环绕着我的目标键下键

I need to write some code for linear interpolation and I am trying to figure out the most efficient way to search the Keys of a SortedList<K, V> for the upper and lower keys that surround my target key.

SortedList<int, double> xyTable = new SortedList<int, double>()
{
    {1, 10}, {2, 20}, {3, 30}, {4,40}
};

double targetX = 3.5;



这是对查询的列表,并确定该3.5是3和4之间的最有效的方法是什么?我有一个方法/作弊,对于整数工程(临时插入目标键进入列表,然后找到索引),但我想我会问的利弊,这样我就能生产出高质量的代码。

What is the most efficient way to search the list and determine that 3.5 is between 3 and 4? I have a method / cheat that works for integers (temporarily insert the target Key into the list then find the index) but I figured I'd ask the pros so I could produce quality code.

感谢。

推荐答案

一个二进制搜索给你一个列表上不俗的表现。不过在排序列表的键属性的类型的的IList ,而二分查找是在列表定义。幸运的是,你可以找到在此相关的问题二进制搜索为的IList 实施

A binary search gives you decent performance on a list. However the Keys property on SortedList is of type IList, whereas BinarySearch is defined on List. Fortunately, you can find an implementation of binary search for IList in this related question:

How上执行的IList℃的二进制搜索; T> ;?

这篇关于K,V&gt;在排序列表&LT的钥匙二进制搜索;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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