标签:
数据结构中查找的知识点主要有以下三点
1、静态查找
1.1 顺序查找
1.2 有序表
1.2.1 二分查找
1.2.2 插值查找
2、动态查找
2.1 二叉排序树
2.2 平衡二叉树
2.3 B-和B+树
3、哈希查找
3.1 常用哈希函数
3.2 解决冲突的办法
1.2.1.1 非递归实现
实现思路:
1、low=0,high=len-1;
2、mid=(low+high)/2;
3、中间值小于目标值,high=mid-1
4、中间值大于目标值,low=mid+1
5、重复
public static int binarySearch(int[] data,int dest) { int high=data.length-1,low=0; int mid=0;//中间值 int index=-1;//位置 while(low<=high) { mid=(high+low)/2; if(dest>data[mid]) low=mid+1; else if(dest<data[mid]) high=mid-1; else {index=mid;break;} } return index; }
1.2.1.2 递归实现
实现思路:
将high,low作为参数传入即可
public static int binarySearch(int[] data,int dest,int high,int low) { int mid=0; if(low<=high) { mid=(high+low)/2; if(dest>data[mid]) return binarySearch(data, dest, high, mid+1); else if(dest<data[mid]) return binarySearch(data, dest, mid-1, low); else {return mid;} } else return -1; }
1.2.2 插值查找
对二分查找的改进
mid=low+(dest-data[low])/(data[high]-data[low])*(high-low);
public static int binarySearch(int[] data,int dest,int high,int low) { int mid=0; if(low<=high) { mid=low+(dest-data[low])/(data[high]-data[low])*(high-low); if(dest>data[mid]) return binarySearch(data, dest, high, mid+1); else if(dest<data[mid]) return binarySearch(data, dest, mid-1, low); else {return mid;} } else return -1; }
标签:
原文地址:http://www.cnblogs.com/maydow/p/4772999.html