Last active
August 29, 2015 14:02
-
-
Save youxiachai/7b6c6888892854bb7a08 to your computer and use it in GitHub Desktop.
归并排序
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Array.prototype.mergeSort=function(){ | |
var merge=function(left,right){ | |
var final=[]; | |
while (left.length && right.length) { | |
final.push(left[0] <= right[0] ? left.shift() : right.shift() ); | |
} | |
return final.concat(left.concat(right)); | |
} | |
// 递归结束 | |
if (this.length < 2) { | |
return this; | |
} | |
var leftArray=this.slice(0,parseInt(this.length/2)); | |
var rightArray=this.slice(parseInt(this.length/2)); | |
return merge(leftArray.mergeSort(),rightArray.mergeSort()); | |
} | |
a=[234,55,667,778,234,664,2245,10239, 4321] ; | |
console.log(a.mergeSort()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
niubility