Skip to content

Instantly share code, notes, and snippets.

@okaprinarjaya
Last active September 6, 2019 07:47
Show Gist options
  • Save okaprinarjaya/dc724e029f6bb6995f0ef481d916e260 to your computer and use it in GitHub Desktop.
Save okaprinarjaya/dc724e029f6bb6995f0ef481d916e260 to your computer and use it in GitHub Desktop.
function quicksort(list) {
if (list.length > 0) {
const pivot = list.splice(0, 1)[0];
const smaller = quicksort(list.filter(item => item < pivot) || []);
const bigger = quicksort(list.filter(item => item >= pivot) || []);
return smaller.concat([pivot]).concat(bigger);
} else {
return [];
}
}
let unsorted_list = [7,1,3,4,2,5,0,6,2,2,2,2,2100,11,10,21,15];
console.log("UNSORTED LIST LENGTH = ", unsorted_list.length);
let sorted_list = quicksort(unsorted_list);
console.log("SORTED LIST LENGTH = ", sorted_list.length);
console.log(sorted_list);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment