Skip to content

Instantly share code, notes, and snippets.

@ncou
Forked from jasondscott/quickSort.js
Created August 23, 2016 09:51
Show Gist options
  • Save ncou/7f0668c98bdc556f0ab0e8dbec6d0a9c to your computer and use it in GitHub Desktop.
Save ncou/7f0668c98bdc556f0ab0e8dbec6d0a9c to your computer and use it in GitHub Desktop.
//JS QuickSort
Array.prototype.quickSort = function() {
var r = this;
if(this.length <= 1) {
return this;
}
var less = [], greater = [];
var pivot = r.splice(Math.floor(r.length / 2),1);
for (var i = r.length - 1 ; i >= 0; i--){
if ( r[i] <= pivot) {
less.push(r[i]);
} else {
greater.push(r[i]);
}
}
var c = [];
return c.concat(less.quickSort(), pivot, greater.quickSort());
};
var a = [3,1,43,5,123,6,231,0];
console.log(a.quickSort());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment