感觉用不到 我就随便敲了敲代码。
package com.ldy.arithmetic.search;import org.apache.tomcat.jni.Error;import java.util.ArrayList;import java.util.Arrays;public class BlockBinarySearch {private int[] ints;private ArrayList[] lists;public BlockBinarySearch(){}public BlockBinarySearch(int[] ints) throws Exception {if(ints !=null && ints.length>0){this.ints = ints;lists = new ArrayList[ints.length];for (int i=0;i<lists.length;i++) {lists[i]=new ArrayList();}}else{throw new Exception("index can't be null or empty~");}}public void insert(int data) throws Exception {int index=binarySearch(data);lists[index].add(data);}public void search(int data) throws Exception {int i=binarySearch(data);for(int j=0;j<lists[i].size();j++){if(data ==(int)lists[i].get(j)){System.out.println(String.format("'%d' Position: [%d,%d]", data, i, j));return;}}System.out.println(String.format("'%d' Position: Not found", data));}private int binarySearch(int data) throws Exception {// if(data > ints[ints.length-1]){// throw new Exception("out of block range");// }int start = 0;int end = ints.length -1 ;int mid;while(start<end){mid=(start+end)>>>1;if(ints[mid]>data) end = mid - 1 ;else start=mid + 1;}return start;}public static void main(String[] args) throws Exception {int[] ints=new int[]{10,20,30,40,50,60};BlockBinarySearch blockBinarySearch=new BlockBinarySearch(ints);// System.out.println(Arrays.toString(ints));blockBinarySearch.insert(6);blockBinarySearch.insert(7);blockBinarySearch.insert(50);blockBinarySearch.insert(19);blockBinarySearch.insert(20);blockBinarySearch.insert(25);blockBinarySearch.insert(500);// blockBinarySearch.search(1);// blockBinarySearch.search(2);// blockBinarySearch.search(3);// blockBinarySearch.search(5);// blockBinarySearch.search(6);// blockBinarySearch.search(7);System.out.println(Arrays.toString(blockBinarySearch.lists));}}
如果觉得《分块查找 转载自https://www.cnblogs.com/Ming8006/p/8330481.html》对你有帮助,请点赞、收藏,并留下你的观点哦!