Advertisement
Guest User

Untitled

a guest
Aug 24th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.51 KB | None | 0 0
  1. module.exports = mergeSort;
  2.  
  3. function quickSort(arr) {
  4. if(arr.length < 2)
  5. return arr;
  6. var pivotIndex = arr.length - 1;
  7. for (var i = 0; i < arr.length - 1; i++) {
  8. if(i >= pivotIndex)
  9. continue;
  10. while (arr[i] > arr[pivotIndex]) {
  11. var tmp = arr[pivotIndex - 1];
  12. arr[pivotIndex - 1] = arr[pivotIndex];
  13. arr[pivotIndex] = arr[i];
  14. pivotIndex -= 1;
  15. arr[i] = tmp;
  16. }
  17.  
  18. }
  19. return [...quickSort(arr.slice(0, arr.length / 2)), arr[pivotIndex], ...quickSort(arr.slice(arr.length + 1))];
  20. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement