Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.50 KB | None | 0 0
  1. function quickSort(arr){
  2. var pivot = arr[Math.floor(Math.random() * arr.length)];
  3. console.log("piv: ", pivot);
  4. if (arr.length < 2){
  5. return arr;
  6. }
  7. var smaller = [];
  8. var bigger = [];
  9. var pivots = [];
  10. arr.forEach(function(i){
  11. if (i < pivot) {
  12. smaller.push(i)
  13. } else if (i > pivot) {
  14. bigger.push(i)
  15. } else {
  16. pivots.push(i)
  17. }
  18. })
  19. return [
  20. ...quickSort(smaller),
  21. ...pivots,
  22. ...quickSort(bigger)
  23. ];
  24. }
  25.  
  26. console.log(quickSort([1,5,3,7,8,2,9,3,5]));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement