Searching: Shar's Method
Comparison tree
Java program
In this program, R1,…,RN were simplified to K1,…,KN.
/*** Created with IntelliJ IDEA.* User: 1O1O* Date: 12/11/13* Time: 6:52 PM* :)~* Shar's Method:Searching*/
public class Main {public static void main(String[] args) {int N = 16;int[] K = new int[18];/*Prepare the ordered data table*/K[1] = 61;K[2] = 87;K[3] = 154;K[4] = 170;K[5] = 275;K[6] = 426;K[7] = 503;K[8] = 509;K[9] = 512;K[10] = 612;K[11] = 653;K[12] = 677;K[13] = 703;K[14] = 765;K[15] = 897;K[16] = 908;/*Output sorted Ks*/System.out.println("Sorted Ks:");for(int i=1; i<=N; i++){System.out.println(i+":"+K[i]);}System.out.println();/*Kernel of the Algorithm!*/int Key = 653; /*Key to be found*/K[0] = -10000; /*Negative Infinite*/K[17] = 10000; /*Positive Infinite*/int k = (int)Math.floor(Math.log(N)/Math.log(2));int i = (int)Math.pow(2, k);int delta = (int)Math.pow(2, k);int l = (int)Math.ceil(Math.log(N-Math.pow(2, k)+1)/Math.log(2));if(Key > K[i]){delta = (int)Math.pow(2, l);i = N+1-(int)Math.pow(2, l);i = i+(int)Math.ceil((double)delta/2);delta = (int)Math.floor((double)delta/2);}do{if(Key < K[i]){if(delta == 0){System.out.println("Outputs: "+Key+" not found.");break;}else {i = i-(int)Math.ceil((double)delta/2);delta = (int)Math.floor((double)delta/2);}}else if(Key > K[i]){if(delta == 0){System.out.println("Outputs: "+Key+" not found.");break;}else {i = i+(int)Math.ceil((double)delta/2);delta = (int)Math.floor((double)delta/2);}}else {System.out.println("Outputs: "+Key+" in K["+i+"].");break;}}while (true);}
}
Outputs
Sorted Ks:
1:61
2:87
3:154
4:170
5:275
6:426
7:503
8:509
9:512
10:612
11:653
12:677
13:703
14:765
15:897
16:908Outputs: 653 in K[11].
Reference
<< The art of computer programming: Sorting and Searching >> VOLUME 3, DONALD E. KNUTH
Searching: Shar's Method相关推荐
- 线性插值插值_揭秘插值搜索
线性插值插值 搜索算法指南 (Searching Algorithm Guide) Prior to this article, I have written about Binary Search. ...
- 制作仅在xy方向有周期性的夹层结构,且不使用约束平板
转载请注明出处!!! 本篇是对这篇文章的改进,解决了xy方向的周期问题,具体原理请见上面的文章,本篇只简述过程.仿真过程用的是alpha-tubulin和beta-tubulin 制作蛋白夹水层 这里 ...
- 论文复现丨基于ModelArts进行图像风格化绘画
摘要:这个 notebook 基于论文「Stylized Neural Painting, arXiv:2011.08114.」提供了最基本的「图片生成绘画」变换的可复现例子. 本文分享自华为云社区& ...
- iOS runtime 底层详解、内部原理、场景应用
前言学:位域和共用体 一:isa指针--runtime之前的学习 1.1:苹果应用的按位或.按位与 二:类对象信息 2.1:类对象信息:rw_t 2.2:类对象信息:方法缓存(很关键) 2.2:类对象 ...
- 通过Gromacs做Lysozyme在水中的模拟
文章目录 1. 问题描述 2. 环境 3. 模拟过程 3.1 PDB文件下载 3.2 准备拓扑结构 3.2.1 清除结构中的水分子 3.2.2 pdb to gmx 3.3 Define box an ...
- ElasticSearch—Basic Operations
目录 Creating an index Deleting an index Opening or closing an index Putting a mapping in an index Get ...
- An error occurred while searching for implementations of method
1:在我安装完scala的插件后,在打开方法的实现类(open implementactions)的时候,抛出这个异常,后来发现这个异常是因为我的scala的插件跟我eclipse版本不兼容导致的. ...
- Objective-C method及相关方法分析
## Objective-C method及相关方法分析 转载请注名出处 [http://blog.csdn.net/uxyheaven](http://blog.csdn.net/uxyheaven ...
- [译]How to make searching faster
Here are some things to try to speed up the seaching speed of your Lucene application. Please see Im ...
最新文章
- python适合做后端开发吗-想从事Python 后端开发?
- 个人博客mysql 建表与数据测试
- OpenCV使用Laplacian filtering和距离变换以及Laplacian滤波对重叠对象进行分段的实例(附完整代码)
- 10.31NOIP模拟赛解题报告
- P3733 [HAOI2017]八纵八横(线性基/线段树分治)
- 降级安装_如何升级iOS13测试版,还有降级
- leetcode 692. 前K个高频单词
- Linux sys_call_table变动检测
- EBS系统请求表定时清除
- mysql可靠性优先策略
- 利用express启一个server服务
- 音频处理-2 WAV格式
- 对数学期望、方差、协方差、协方差矩阵的理解
- Linux makefile 教程 非常详细,且易懂
- 管仲(约前723年-前645年)
- 字符串模式匹配——BF算法
- 卡内基梅陇大学计算机学院,卡内基梅隆大学计算机学院
- SLAM【十一】建图
- 【弄nèng - Elasticsearch】运维篇 —— ES分片unassigned解决方案(ALLOCATION_FAILED,REPLICA_ADDED等
- 漫画:什么是哥德巴赫猜想?