Faster solutions exist that average O (log n) or, in some cases, O (log log n) instead of O (n). For a “binary search and search, ” you will probably find very good explanations.
If the array is unsorted, then yes, the element is located anywhere, and you cannot access O (n), but this does not apply to sorted arrays.
-
Some explanation of the search interpolation query:
While a binary search only deals with comparing two elements in terms of more / no more, the interpolation search also tries to use numerical values . The fact is that you have a sorted range of values from 0 to, say, 20,000. You are looking for 300 - the binary search will begin in half the range, at 10,000. An interpolation search assumes that 300 is likely to be somewhere closer to 0 than 20,000, so he will check element 6000 first instead of 10000. Then again - if it is too high, write to the lower subband, and it is too low - write to the upper subband.
For a large array with + - even distribution of values, the interpolation search should behave much faster than the binary search - see this code yourself. Also, it works best if you first use one interpolation search step, then one binary search step, etc.
Note that this is what a person does intuitively when looking for something in a dictionary.
Kos Nov 13 '10 at 12:55 2010-11-13 12:55
source share