@xmruibi
2015-04-28T13:48:02.000000Z
字数 760
阅读 651
Algorithm
Search Problems can be divided as Sequential Search, Binary Search, Interpolation
Please Check:
Interpolation Equation:
On average the interpolation search makes about log(log(n)) comparisons (if the elements are uniformly distributed), where n is the number of elements to be searched. In the worst case (for instance where the numerical values of the keys increase exponentially) it can make up to O(n) comparisons.
public int interpolationSearch(int[] nums, int key) {int l = 0, r = nums.length-1;while(l<r){int m = l+(r-l)*(key - nums[l])/(nums[r]-nums[l]);if(nums[m]<key)l = m+1;else if(nums[m]>key)r = m;elsereturn m;}return -1;}