Skip to content

Instantly share code, notes, and snippets.

@adhrinae
Last active May 3, 2018 01:06
Show Gist options
  • Save adhrinae/6174f356dc9f4f0de121af152c2fc744 to your computer and use it in GitHub Desktop.
Save adhrinae/6174f356dc9f4f0de121af152c2fc744 to your computer and use it in GitHub Desktop.
Quick Sort in Javascript
function quickSort(arr) {
if (arr.length <= 1) return arr;
const result = [];
const pivot = arr[Math.floor(arr.length / 2)];
const left = arr.filter(el => el < pivot);
const right = arr.filter(el => el > pivot);
return result.concat(quickSort(left), pivot, quickSort(right));
}
const unsorted = [7, 1, 5, 2, 9, 53, 22];
console.log(quickSort(unsorted));
@adhrinae
Copy link
Author

adhrinae commented May 3, 2018

Problem: Cannot sort duplicated values

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment