1 Array.prototype.quickSort = function() { 2 var len = this.length; 3 if(len < 2) return this; 4 var left = [], right = [], base = this[0]; 5 for(var i = 1; i < len; i ++) { 6 var iv = this[i]; 7 iv >= base && right.push(iv); 8 iv < base && left.push(iv); 9 } 10 return left.quickSort().concat(base, right.quickSort()) 11 } 12 13 var arr = [5, 3, 7, 4, 1, 9, 8, 6, 2]; 14 console.log(arr.quickSort())
抄wiki的,代码质量高,实现简单
https://zh.wikipedia.org/wiki/%E5%BF%AB%E9%80%9F%E6%8E%92%E5%BA%8F#JavaScript