你能多快可以使线性搜索? [英] How fast can you make linear search?

查看:129
本文介绍了你能多快可以使线性搜索?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在找这个线性搜索优化:

 静态INT
线性(const int的*改编,诠释N,INT键)
{
        INT I = 0;
        而(I< N){
                如果(ARR [I]> =键)
                        打破;
                ++我;
        }
        返回我;
}

数组进行排序,并在功能应该返回一个大于或等于所述键的第一个元素的索引。他们阵不是很大(低于200元),并会进行大量的搜索ppared一旦$ P $。第n次后,数组元素可以根据需要被初始化为合适的东西,如果加快搜索。

没有,二进制搜索是不允许的,只能线性搜索。

修改:我对这个话题的所有知识,现在总结的在这个博客帖子


解决方案

  1. 告诉你的老板,你可以把它50%的速度,但需要6个月,和一些钱。

  2. 等待六个月。

  3. 购买新的硬件。

好了,它使大约多大意义通过一个排序的数组的线性搜索!

(更严重的是,你能不能给我们介绍一下一些线索,为什么没有二进制搜索?)

I'm looking to optimize this linear search:

static int
linear (const int *arr, int n, int key)
{
        int i = 0;
        while (i < n) {
                if (arr [i] >= key)
                        break;
                ++i;
        }
        return i;
}

The array is sorted and the function is supposed to return the index of the first element that is greater or equal to the key. They array is not large (below 200 elements) and will be prepared once for a large number of searches. Array elements after the n-th can if necessary be initialized to something appropriate, if that speeds up the search.

No, binary search is not allowed, only linear search.

Edit: All my knowledge about this topic is now summarized in this blog post.

解决方案

  1. Tell your boss you can make it 50% faster, but it will take 6 months, and some money.
  2. Wait six months.
  3. Buy new hardware.

Well, it makes about as much sense as a linear search through a sorted array!

(More seriously, can you give us some clues about why no binary search?)

这篇关于你能多快可以使线性搜索?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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