为JS数组/Json/Xml添加折半查询方法
2008-03-24 19:56
911 查看
对于顺序或逆序排序的数组/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;
}
使用方法: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;
}
相关文章推荐
- js 实现对JSON数组模糊查询的方法
- js数组合并(一个数组添加到另一个数组里面)方法
- JS实现去除数组中重复json的方法示例
- js便利json 数组的方法
- json对象与数组以及转换成js对象的简单实现方法
- JS 实现Json查询方法
- js/jquery解析json和数组格式的方法详解
- JS 实现Json查询的方法实例
- json对象与数组以及转换成js对象的简单实现方法
- 以JSON形式将JS中Array对象数组传至后台的方法
- JS数组方法汇总 array数组元素的添加和删除
- JS数组方法汇总 array数组元素的添加和删除
- js 无参数、有参数的webservice通信方法 XMLHttpRequest和 $.ajax方式汇总及返回值为json问题
- JS解析json数据并将json字符串转化为数组的实现方法
- js动态添加行hiddenfield保存xml或json 数据 提交给后台处理
- JS数组方法汇总 array数组元素的添加和删除
- js/jquery解析json和数组格式的方法详解
- JS解析json数据并将json字符串转化为数组的实现方法
- js中prototype用法实例(为数组添加contains方法)