algorithm - Binary search on unknown size array -
suppose array has been given , want find element in array , how can search element in array using binary search , given array sorted , size of array unknown. linear search can applied trying figure out faster search linear algorithm.
if can test whether have fallen out of array range, use modified binary search (assume 1-based array):
- lower = 1, upper = 1;
- while (a[upper] < element) upper *= 2;
- normal binary search on (lower, upper).
otherwise, there no real way it: assume find somewhere equals element need, cannot know if falls out of array.
Comments
Post a Comment