Skip to content

Instantly share code, notes, and snippets.

@alpgul
Last active March 23, 2018 07:40
Show Gist options
  • Save alpgul/c3408faac554d4876ff9d868041dcc4a to your computer and use it in GitHub Desktop.
Save alpgul/c3408faac554d4876ff9d868041dcc4a to your computer and use it in GitHub Desktop.
Interpolation Search with Javascript
/*
----
Best Case:1
Worst Case:√n(karekök n)
Average Case:log(logn)
Unsuccessful:√n
-----
mid = Lo + ((Hi - Lo) / (A[Hi] - A[Lo])) * (X - A[Lo])
where −
A = list
Lo = Lowest index of the list
Hi = Highest index of the list
A[n] = Value stored at index n in the list
*/
(function () {
let arr=[1,12,13,14,15],val=14;
function interpolation_search(array,value){
let low=0,high=array.length-1,index;
while(low<=high){
let mid=low+Math.floor(((high-low)/(array[high]-array[low]))*(value-array[low]));
if(array[mid]==value){
index=mid;
break;
}
else{
if(array[mid]<value){
low=mid+1;
}
else{
high=mid-1;
}
}
}
if(typeof index!=="undefined"){
return "bulunan değer indexi:"+index;
}
return "değer bulunamadi";
}
console.log(interpolation_search(arr,val));
})();
@alpgul
Copy link
Author

alpgul commented Mar 22, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment