确定最快的方法,如果一个元素是一个排序的数组 [英] fastest way to determine if an element is in a sorted array

查看:101
本文介绍了确定最快的方法,如果一个元素是一个排序的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有排序的整数用1000以上的值的数组(可能高达5000+)。我需要编写收到int和返回基于元素数组中是一个布尔函数。我知道我可以写一个for循环休息后,我知道我可以使用jQuery .InArray。

I have an array of sorted ints with a 1,000 or more values (could be up to 5000+). I need to write a function that receives an int and returns a bool based on the element being in the array. I know I can write a for loop with a break, I know I can use jquery .InArray.

什么是要实现这一点,明知数组进行排序的最佳方式。

What would be the best way to implement this, KNOWING that the array is sorted.

感谢。

推荐答案

知道该数组排序二进制搜索将是最好的办法。

Knowing that the array is sorted a binary search would be the best approach.

这篇关于确定最快的方法,如果一个元素是一个排序的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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