Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2019
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.43 KB | None | 0 0
  1. function quickSort(arr) {
  2. if (arr.length <= 1) {
  3. return arr;
  4. }
  5.  
  6. const less = [];
  7. const greater = [];
  8. const pivot = arr[arr.length - 1];
  9. for (let i = 0; i < arr.length - 1; ++i) {
  10. const num = arr[i];
  11. if (num < pivot) {
  12. less.push(num);
  13. } else {
  14. greater.push(num);
  15. }
  16. }
  17.  
  18. return [...quickSort(less), pivot, ...quickSort(greater)];
  19. }
  20.  
  21. const arr = [9, 4, 1, 6, 7, 3, 8, 2, 5];
  22. quickSort(arr);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement