Skip to content

Instantly share code, notes, and snippets.

@brothertao
Created November 25, 2016 06:09
Show Gist options
  • Save brothertao/617bfbdeacb521d1a01fe532ffc8cfb6 to your computer and use it in GitHub Desktop.
Save brothertao/617bfbdeacb521d1a01fe532ffc8cfb6 to your computer and use it in GitHub Desktop.
js递归实现quick sort
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