Skip to content

Instantly share code, notes, and snippets.

@nikuuchi
Created April 4, 2013 05:29
Show Gist options
  • Select an option

  • Save nikuuchi/5308053 to your computer and use it in GitHub Desktop.

Select an option

Save nikuuchi/5308053 to your computer and use it in GitHub Desktop.
quick sortを作ってみた in JavaScript
'use strict';
function quickSort(arr) {
if (arr.length === 0) {
return arr;
} else {
var p = arr[0],
xs = arr.slice(1);
var gts = [],
lts = [],
i = 0;
for(i = 0; i < xs.length; i++) {
if ( xs[i] < p ) {
lts.push(xs[i]);
} else {
gts.push(xs[i]);
}
}
return quickSort(lts).concat([p], quickSort(gts));
}
}
console.log([4, 6, 3, 5, 1, 2, 7]);
console.log(quickSort([4, 6, 3, 5, 1, 2, 7]));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment