package com;
//27,22,30,40,36,13,19,16,20,7,10,43,50,48
public class Demo {
public static void main(String[] args) {
int[] arr={27,22,30,40,36,
13,19,16,20,
7,10,
43,50,48};
Block b1 = new Block(22,40,0,4);
Block b2 = new Block(13,20,5,8);
Block b3 = new Block(7,10,9,10);
Block b4 = new Block(43,50,11,13);
Block[] blocks={b1,b2,b3,b4};
int key=48;
int index = findIndex(blocks,arr,key);
System.out.println(index);
}
private static int findIndex(Block[] blocks, int[] arr,int key){
int blockIndex = findIndexBlock(blocks,key);
if(blockIndex==-1) return -1;
int starIndex=blocks[blockIndex].getStarIndex();
int endIndex=blocks[blockIndex].getEndIndex();
while(true){
for (int i = starIndex; i <= endIndex; i++) {
if(arr[i]==key) return i;
}
}
}
private static int findIndexBlock(Block[] blocks,int key){
for (int i = 0; i < blocks.length; i++) {
if(key>=blocks[i].getMin() && key<=blocks[i].getMax()) return i;
}
return -1;
}
}
class Block{
private int min;
private int max;
private int starIndex;
private int endIndex;
public Block() {
}
public Block(int min, int max, int starIndex, int endIndex) {
this.min = min;
this.max = max;
this.starIndex = starIndex;
this.endIndex = endIndex;
}
public int getMin() {
return min;
}
public void setMin(int min) {
this.min = min;
}
public int getMax() {
return max;
}
public void setMax(int max) {
this.max = max;
}
public int getStarIndex() {
return starIndex;
}
public void setStarIndex(int starIndex) {
this.starIndex = starIndex;
}
public int getEndIndex() {
return endIndex;
}
public void setEndIndex(int endIndex) {
this.endIndex = endIndex;
}
}
JAVA插值查找
最新推荐文章于 2023-02-04 21:15:37 发布