src/search/interpolation-search.js
/** 
Interpolation search is an improved variant of binary search. 
This search algorithm works on the probing position of the required value.
 For this algorithm to work properly, 
 the data collection should be in a sorted form and equally distributed.

 guess( mid ) = min + ( ( key - arr[min]) * ( max - min) / ( arr[max] - arr[min]) )
 */
Interpolation search is an improved variant of binary search.
This search algorithm works on the probing position of the required value.
For this algorithm to work properly,
the data collection should be in a sorted form and equally distributed.


Interpolation search is an improved variant of binary search . This search algorithm works on the probing position of the required value . For this algorithm to work properly the data collection should be in a sorted form and equally distributed .
function interpolationSearch(key, arr) {
  if (!Array.isArray(arr)) return -1;
  let min = 0;
  let max = arr.length - 1;
  while (min < max && key <= arr[max] && key >= arr[min]) {
    let guess =
      min +
      Math.round(((key - arr[min]) * (max - min)) / (arr[max] - arr[min]));
    if (key == arr[guess]) {
      return guess;
    } else if (key < arr[guess]) {
      max = guess - 1;
    } else {
      min = guess + 1;
    }
    `Max to be ${max} & Min to be${min}`);
  }
  return -1;
}
function interpolationSearch ( key , arr ) { if ( ! Array . isArray ( arr ) ) return - 1 ; let min = 0 ; let max = arr . length - 1 ; while ( min < max && key <= arr [ max ] && key >= arr [ min ] ) { let guess = min + Math . round ( ( ( key - arr [ min ] ) * ( max - min ) ) / ( arr [ max ] - arr [ min ] ) ) ; if ( key == arr [ guess ] ) { return guess ; } else if ( key < arr [ guess ] ) { max = guess - 1 ; } else { min = guess + 1 ; } } return - 1 ; }
Interpolation search is an improved variant of binary search.
Interpolation search is an improved variant of binary search .
// console.log(`---Interpolation Search-----`); // console.log(`Max is ${max} & Min is${min}`); // console.log( //   `guess is: ${guess} => ${arr[guess]} and search value is: ${key}` // ); // console.log(`Max to be ${max} & Min to be${min}`);