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):

  1. lower = 1, upper = 1;
  2. while (a[upper] < element) upper *= 2;
  3. 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

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -