美文网首页
js实现数组的基本排序与去重

js实现数组的基本排序与去重

作者: 程序员小布的养生之道 | 来源:发表于2017-10-20 14:46 被阅读0次

    //位置交换Array.prototype.wrap=function(i,j){    var temp;    temp=this[i];    this[i]=this[j];    this[j]=temp;    return this;}//数组去重Array.prototype.unique=function(){    var outArr=[];    for(var i=0;ithis[i + 1]) {                this.wrap(i, i + 1);                flag = true;            }        }    }    return this;};// 插入排序Array.prototype.insertp=function(){    for(var i=1;ithis[i]) {

    m = i;

    if(m!=j){

    this.wrap(j,m);

    }

    }

    }

    }

    return this;

    };

    //快排(一)

    Array.prototype.quickp=function(left,right){

    if(left < right) {

    var  s = this[left];

    var  i = left;

    var  j = right + 1;

    while(true) {

    // 向右找

    while(i + 1 < this.length && this[++i] < s) ;

    // 向左找

    while(j -1 > -1 && this[--j] > s) ;

    if(i >= j){

    break;

    }else{

    this.wrap( i, j);

    }

    }

    this[left] = this[j];

    this[j] = s;

    this.quickp(left, j-1);  // 对左边进行递回

    this.quickp(j+1, right);  // 对右边进行递回

    }

    return this;

    };排序

    相关文章

      网友评论

          本文标题:js实现数组的基本排序与去重

          本文链接:https://www.haomeiwen.com/subject/gelvuxtx.html