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.
Thanks.
If we know nothing about the distribution of key values, then we have just proved that binary search is the best algorithm available for searching a sorted array.
The C++ function std :: is_sorted checks if the elements in range [first, last] are sorted in ascending order.
Knowing that the array is sorted a binary search would be the best approach.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With