确定最快的方法,如果一个元素是一个排序的数组
问题描述:
我有排序的整数用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.