Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.62 KB | None | 0 0
  1. // Quick Sort Algorithm implemented in JS
  2.  
  3. function sort(arr, start, end){
  4. if (start >= end){
  5. return
  6. }
  7.  
  8. let index = partition(arr, start, end);
  9. sort(arr, start, index - 1);
  10. sort(arr, index + 1, end);
  11. }
  12.  
  13. function partition(arr, start, end) {
  14. let pivotIndex = start;
  15. let pivotValue = arr[end];
  16.  
  17. for (let i = start; i < end; i++) {
  18. if (arr[i] < pivotValue){
  19. swap(arr, i, pivotIndex);
  20. pivotIndex++;
  21. }
  22. }
  23. swap(arr, pivotIndex, end);
  24. return pivotIndex;
  25. }
  26.  
  27. function swap(arr, a, b){
  28. let temp = arr[a];
  29. arr[a] = arr[b];
  30. arr[b] = temp;
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement