向Array中添加快速排序

快速排序思路

1) 假设第一个元素为基准元素 

2) 把所有比基准元素小的记录放置在前一部分,把所有比基准元素大的记录放置在后一部分,并把基准元素放在这两部分的中间(i=j的位置)

快速排序实现

Function.prototype.method = function(name, func){
    this.prototype[name] = func;
    return this;
};

Array.method('quickSort', function(s, t){
    var i=s, j=t,
        tmp;
    if(s < t){
        tmp = this[s];
        while(i!=j){
            while(j>i && this[j]>tmp) j--;
            R[i] = R[j];
            while(i<j && this[j]<tmp) i++;
            R[j] = R[i]; 
        }
        R[i] = tmp;
        this.quickSort(s, i-1);
        this.quickSort(i+1, t);
    }
    return this;
});

 

posted @ 2013-10-09 13:37  JChen___  阅读(153)  评论(0编辑  收藏  举报