Created
November 25, 2016 06:09
-
-
Save brothertao/617bfbdeacb521d1a01fe532ffc8cfb6 to your computer and use it in GitHub Desktop.
js递归实现quick sort
This file contains 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
function quick(nums) { | |
if (nums.length===0) { | |
return []; | |
} | |
var pivot = nums.shift(); | |
var a = nums.filter(function(num) { | |
return num<=pivot; | |
}); | |
var b = nums.filter(function(num) { | |
return num>pivot; | |
}); | |
return quick(a).concat([pivot]).concat(quick(b)); | |
} | |
//test | |
var nums = [2,1,5,3,8,4,6] | |
quick(nums) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment