Skip to content

Instantly share code, notes, and snippets.

@vladanyes
Created May 5, 2019 04:25
Show Gist options
  • Save vladanyes/83a07193bfb91846ddb73b49399ff842 to your computer and use it in GitHub Desktop.
Save vladanyes/83a07193bfb91846ddb73b49399ff842 to your computer and use it in GitHub Desktop.
Quicksort in JavaScript
const arrayToSort = [1,9,2,8,3,7,4,6,5];
const sort = array => {
if (array.length < 2) return array;
const [pivot, ...restItems] = array;
const less = [...restItems].filter(item => item < pivot);
const greater = [...restItems].filter(item => item > pivot);
console.log('test');
return sort(less).concat(pivot).concat(sort(greater));
};
console.log(sort(arrayToSort));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment