插值搜索
来源:互联网 发布:高性能mysql 编辑:程序博客网 时间:2024/05/14 12:09
插值搜索核心代码由二分搜索(区间左闭右开)m = l + 1.0/2*(r - 1 - l) = 1.0(l + r - 1)/2 更改为m = l +1.0*(key - a[l])/(a[r - 1] - a[l])*(r - 1 - l).
利用关键字所在位置的估值(key - a[l])/(a[r - 1] - a[l])代替1/2,对于随机分布为常规分布的关键字,平均时间复杂度lg lg N.即如果N为10亿,平均查找小于5次.
//// main.cpp// 插值搜索//// Created by Lance on 13-3-14.// Copyright (c) 2013年 Lance. All rights reserved.//#include <iostream>#include <cstdio>using std::cin;using std::cout;using std::endl;typedef int Item;#define key(A)(A)#define less(A, B)(key(A) < key(B))#define exch(A, B){ Item t = A; A = B; B = t; }#define compexch(A, B)if (less(B, A)) exch(A, B)void insertion (Item a[], int l, int r) {//插入排序inti;for (i = r - 1; i > 1; --i)//设置观察哨compexch(a[i - 1], a[i]);for (i = l + 2; i < r; ++i) {intj = i;Itemv = a[i];while (less(v, a[j - 1])) {//确定位置并移位数组a[j] = a[j - 1];--j;} // end of whilea[j] = v;}}Item search (Item a[], Item key, int l, int r) {//插值搜索核心代码intm = l + 1.0*(key - a[l])/(a[r - 1] - a[l])*(r - 1 - l);if (l > r)return -1;if (key == a[m])return m;if (key < a[m])return search(a, key, l, m);elsereturn search(a, key, m + 1, r);}int main(int argc, char *argv[]) { const int N(10); int i(0); Item *a = new Item[N]; for (i = 0; i < N; ++i) a[i] = 1000*(1.0*rand()/RAND_MAX);Itemcompare(a[N/2]); printf("old data:\n"); for (i = 0; i < N; ++i) printf("%3d ", a[i]); //便于格式控制 printf("\n"); insertion(a, 0, N); printf("sorted:\n"); for (i = 0; i < N; ++i) printf("%3d ", a[i]); printf("\n");int ans = search(a, compare, 0, N) + 1;cout << endl << endl;cout << "compare is " << compare << endl;cout << "ans is " << ans << endl; delete a;}
- 插值搜索
- 插值搜索
- Interpolation Search 插值搜索算法
- 插补搜索法
- 优化学习率 - 1 - 回溯线性搜索和二次插值线性搜索
- 插值
- 插值
- 插值
- 插值
- 插值
- 插值
- 插值
- 插值
- 插值
- newton插值 lagrange 插值
- 插值方法-牛顿插值
- Scala 字符串插值: s插值
- Matlab数据插值-内插、外插
- VMware中安装ubuntu server
- the algorithms episode one
- 八大排序算法总结
- Win7配置 Maven
- 详解div+css相对定位和绝对定位用法
- 插值搜索
- Jquery表单插件ajaxForm用法详解
- JAVA TreeSet
- 1198. Substring
- secureCRT The remote system refused the connection
- Linux API函数总结
- 一个24点的小游戏~
- 1200. Stick
- IOS笔记:获取当前时间,并转化为一定格式字符串