Skip to content

Instantly share code, notes, and snippets.

@nealnote
Created July 24, 2014 07:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nealnote/45a98f72d6c38bb0e980 to your computer and use it in GitHub Desktop.
Save nealnote/45a98f72d6c38bb0e980 to your computer and use it in GitHub Desktop.
var quickSort = function(arr){
if ( arr.length <= 1 ) {
return arr;
}
var sampleIndex = Math.floor(arr.length/2),
sample = arr.splice(sampleIndex, 1)[0],
left = [],
right = [];
for (var i = 0; i < arr.length ; i++){
if ( arr[i] < sample ) {
left.push(arr[i])
} else {
right.push(arr[i])
}
}
return quickSort(left).concat([sample], quickSort(right))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment