Array 原型扩展(快速排序,搅乱顺序)

 


/// 快速快速排序算法
Array.prototype.quickSort = function (left, right) {
// left = left || 0;
// right = right || this.length - 1;
if (left < right) {
var x = this[right],
i = left - 1,
temp;

for (var j = left; j <= right; j++) {
if (this[j] <= x) {
i++;
temp = this[i];
this[i] = this[j];
this[j] = temp;
}
}
this.quickSort(left, i - 1);
this.quickSort(i + 1, right);
};
}


// 搅乱当前的数组(洗牌)shuffle an array Fisher-Yates style
Array.prototype.shuffle = function () {
var i = this.length;
if (i !== 0) {
while (--i) {
var j = Math.floor(Math.random() * (i + 1));
var temp = this[i];
this[i] = this[j];
this[j] = temp;
}
}
};

posted @ 2016-04-25 23:57  Angkor--:--  阅读(240)  评论(0编辑  收藏  举报