Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function quickSort(arr){
- var pivot = arr[Math.floor(Math.random() * arr.length)];
- console.log("piv: ", pivot);
- if (arr.length < 2){
- return arr;
- }
- var smaller = [];
- var bigger = [];
- var pivots = [];
- arr.forEach(function(i){
- if (i < pivot) {
- smaller.push(i)
- } else if (i > pivot) {
- bigger.push(i)
- } else {
- pivots.push(i)
- }
- })
- return [
- ...quickSort(smaller),
- ...pivots,
- ...quickSort(bigger)
- ];
- }
- console.log(quickSort([1,5,3,7,8,2,9,3,5]));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement