为JS数组/Json/Xml添加折半查询方法
来源:互联网 发布:大数据小数据融合 编辑:程序博客网 时间:2024/05/22 06:43
对于顺序或逆序排序的数组/Json/Xml等,顺序查询如果数据量少的话倒看不出来,如果数据量庞大同时查询的值在比较后面/前面的时候,顺序/逆序查询极大影响效率,借用了数据结构的折半查询算法,为数组添加了折半查询功能,Json/Xml等原理同。
使用方法:arrayName.select(value [,minIndex][,maxIndex]),[]内为可选输入,输入则指定查询范围,不输入则默认全局查询,程序自动判断数组是顺/逆序排序(仅支持顺序/逆序排序的数组),返回值为查询值所在的位置,返回-1为数组内不存在查询的值。资源内有顺序排序和折半排序的比较,数组越大速度差别越大。
//查询传入的value值所在数组中的位置(仅针对按照规则排序有效)
//minIndex,maxIndex指定查询范围,可选输入,不输入则查询全局
//返回值:查询值所在数组的index,-1不存在
//coding by pippe pippe@163.com
Array.prototype.select = function(value, minIndex, maxIndex)
...{
minIndex = minIndex ? minIndex : 0;
maxIndex = maxIndex ? maxIndex : this.length - 1;
var minValue = this[minIndex];
var maxValue = this[maxIndex];
//排列顺序,0顺序,1逆序
var sortType = 0;
if (maxValue < minValue)
...{
//判断是否逆序
sortType = 1;
var temp = minValue;
minValue = maxValue;
maxValue = temp;
}
if (value == this[minIndex])
...{
return minIndex;
}
if (value == this[maxIndex])
...{
return maxIndex;
}
if (value < minValue || value > maxValue)
...{
//不在范围内
return -1;
}
return sortType == 0 ? this.halfSelectByOrdinal(value, minIndex, maxIndex) : this.halfSelectByReverse(value, minIndex, maxIndex);
}
//顺序折半查询
Array.prototype.halfSelectByOrdinal = function(value, minIndex, maxIndex)
...{
if (minIndex + 1 == maxIndex || minIndex == maxIndex)
...{
if (value == this[minIndex])
...{
return minIndex;
}
if (value == this[maxIndex])
...{
return maxIndex;
}
return -1;
}
var halfIndex = minIndex + parseInt((maxIndex - minIndex) / 2);
var index;
if (value == this[halfIndex])
...{
//命中
return halfIndex;
}
if (value < this[halfIndex])
...{
//在前半部分
return this.halfSelectByOrdinal(value, minIndex, halfIndex);
}
if (value > this[halfIndex])
...{
//在后半部分
return this.halfSelectByOrdinal(value, halfIndex, maxIndex);
}
return -1;
}
//逆序折半查询
Array.prototype.halfSelectByReverse = function(value, minIndex, maxIndex)
...{
if (minIndex + 1 == maxIndex || minIndex == maxIndex)
...{
if (value == this[minIndex])
...{
return minIndex;
}
if (value == this[maxIndex])
...{
return maxIndex;
}
return -1;
}
var halfIndex = minIndex + parseInt((maxIndex - minIndex) / 2);
var index;
if (value == this[halfIndex])
...{
//命中
return halfIndex;
}
if (value > this[halfIndex])
...{
//在前半部分
return this.halfSelectByReverse(value, minIndex, halfIndex);
}
if (value < this[halfIndex])
...{
//在后半部分
return this.halfSelectByReverse(value, halfIndex, maxIndex);
}
return -1;
}
//minIndex,maxIndex指定查询范围,可选输入,不输入则查询全局
//返回值:查询值所在数组的index,-1不存在
//coding by pippe pippe@163.com
Array.prototype.select = function(value, minIndex, maxIndex)
...{
minIndex = minIndex ? minIndex : 0;
maxIndex = maxIndex ? maxIndex : this.length - 1;
var minValue = this[minIndex];
var maxValue = this[maxIndex];
//排列顺序,0顺序,1逆序
var sortType = 0;
if (maxValue < minValue)
...{
//判断是否逆序
sortType = 1;
var temp = minValue;
minValue = maxValue;
maxValue = temp;
}
if (value == this[minIndex])
...{
return minIndex;
}
if (value == this[maxIndex])
...{
return maxIndex;
}
if (value < minValue || value > maxValue)
...{
//不在范围内
return -1;
}
return sortType == 0 ? this.halfSelectByOrdinal(value, minIndex, maxIndex) : this.halfSelectByReverse(value, minIndex, maxIndex);
}
//顺序折半查询
Array.prototype.halfSelectByOrdinal = function(value, minIndex, maxIndex)
...{
if (minIndex + 1 == maxIndex || minIndex == maxIndex)
...{
if (value == this[minIndex])
...{
return minIndex;
}
if (value == this[maxIndex])
...{
return maxIndex;
}
return -1;
}
var halfIndex = minIndex + parseInt((maxIndex - minIndex) / 2);
var index;
if (value == this[halfIndex])
...{
//命中
return halfIndex;
}
if (value < this[halfIndex])
...{
//在前半部分
return this.halfSelectByOrdinal(value, minIndex, halfIndex);
}
if (value > this[halfIndex])
...{
//在后半部分
return this.halfSelectByOrdinal(value, halfIndex, maxIndex);
}
return -1;
}
//逆序折半查询
Array.prototype.halfSelectByReverse = function(value, minIndex, maxIndex)
...{
if (minIndex + 1 == maxIndex || minIndex == maxIndex)
...{
if (value == this[minIndex])
...{
return minIndex;
}
if (value == this[maxIndex])
...{
return maxIndex;
}
return -1;
}
var halfIndex = minIndex + parseInt((maxIndex - minIndex) / 2);
var index;
if (value == this[halfIndex])
...{
//命中
return halfIndex;
}
if (value > this[halfIndex])
...{
//在前半部分
return this.halfSelectByReverse(value, minIndex, halfIndex);
}
if (value < this[halfIndex])
...{
//在后半部分
return this.halfSelectByReverse(value, halfIndex, maxIndex);
}
return -1;
}
- 为JS数组/Json/Xml添加折半查询方法
- 使用分类为数组添加快速排序方法和折半查找方法
- js 实现对JSON数组模糊查询的方法
- js中prototype用法实例(为数组添加contains方法)
- 为js的数组添加contains方法,方便使用
- JS解析json数据并将json字符串转化为数组的实现方法
- JS解析json数据并将json字符串转化为数组的实现方法
- js关联数组转化为json字符串
- JS中json字符串转换为数组
- JS 为数组添加新项
- JS 实现Json查询的方法实例
- 1.js动态的往json数据添加新属性和值 2.JSON 和 JS 对象互转 3.对象转化为数组
- Json字符串转换为JS对象方法
- Json字符串转换为JS对象方法
- js将数组对象转化为json格式的数组
- 为 Javascript 数组添加一个 inArray 方法
- 原生js为DOM添加方法
- 折半查询与反转数组元素
- 动态设置网页背景图片
- 连接字符串到底哪个有效?一个小的测试
- Returning ArrayList from a WebService
- 研究递归函数的堆栈表示
- 警告1无法应用项“c”的发布属性。Packing
- 为JS数组/Json/Xml添加折半查询方法
- Hashtable和HashMap的区别
- 《linux设备驱动》英文第二版学习笔记1
- ASP.NET2.0中将文件、图片上传到数据库(已试)
- Tab菜单(分离自迅雷)
- Iptables 指南
- 今天。。。3.24
- ADO.NET事务的使用
- Google排名优化-面向Google(Search Engine Friendly)的URL设计