Java InterpolationSearch

/**
* <html>
* <body>
* <P> Copyright 1994-2018 JasonInternational </p>
* <p> All rights reserved.</p>
* <p> Created on 2018年4月10日 上午9:46:32</p>
* <p> Created by Jason</p>
* </body>
* </html>
*/
package cn.ucaner.algorithm.search; /**
* Interpolation search is an algorithm for searching for a given key in an indexed array that has been ordered by numerical values assigned to the keys (key values). It parallels how humans search
* through a telephone book for a particular name, the key value by which the book's entries are ordered.
* <p>
* Worst-case performance O(n)<br>
* Average performance O(log(log(n)))<br>
* <p>
* @see <a href="https://en.wikipedia.org/wiki/Interpolation_search">Interpolation Search (Wikipedia)</a>
* <br>
* @author Justin Wetherell <phishman3579@gmail.com>
*/
public class InterpolationSearch { private static int[] sorted = null; // Assuming the array is sorted
public static final int find(int value, int[] array) {
InterpolationSearch.sorted = array;
try {
return recursiveFind(value, 0, InterpolationSearch.sorted.length - 1);
} finally {
InterpolationSearch.sorted = null;
}
} private static int recursiveFind(int value, int start, int end) {
if (start == end) {
int lastValue = sorted[start]; // start==end
if (value == lastValue)
return start; // start==end
return Integer.MAX_VALUE;
} final int mid = start + ((value - sorted[start]) * (end - start)) / (sorted[end] - sorted[start]);
if (mid < 0 || mid > end)
return Integer.MAX_VALUE;
int midValue = sorted[mid];
if (value == midValue)
return mid;
if (value > midValue)
return recursiveFind(value, mid + 1, end);
return recursiveFind(value, start, mid - 1);
}
}

  

最新文章

  1. 关于SIGSEGV错误及处理方法(转)
  2. Atitit.复合文档的格式&#160;标准化格式
  3. linux笔记:RPM软件包管理-rpm命令管理
  4. NOIP2007 守望者的逃离-DP
  5. sea.js,spm学习
  6. mysql中lock tables与unlock tables
  7. silverlight 生成图表 WCF 解析XML代码.svc.cs 文件
  8. JParticles 2.0 发布,打造炫酷的粒子特效
  9. React Native 轻松集成分享功能(iOS 篇)
  10. border-radius 圆角
  11. Numpy数组数据文件的读写
  12. TopicsExtraction with NMF &amp; LDA
  13. docker镜像操作
  14. css解决td单元格内文字溢出
  15. Spark 常见问题集合
  16. (转) 开运算opening_circle和闭运算closing_circle的异同
  17. 求链表的倒数第m个元素
  18. Pyhon 中文编码问题(字符串前加‘U’)
  19. VAE(Variational Autoencoder)的原理
  20. 最小生成树 Prim(普里姆)算法和Kruskal(克鲁斯特尔)算法

热门文章

  1. 【Java】 HashMap
  2. Linux中split大文件分割和cat合并文件
  3. vue使用install函数把组件做成插件方便全局调用
  4. ISO/IEC 9899:2011 条款6.2.3——标识符的名字空间
  5. MauiMETA工具的使用(一)
  6. (五)UML之协作图
  7. (四)UML之顺序图(时序图)
  8. 第一个php文件运行
  9. idea自动下载依赖的源代码等信息
  10. linux下的进程通信之信号量semaphore