为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;
    }
 
原创粉丝点击