Guest User

Untitled

a guest
Dec 3rd, 2025
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.57 KB | Source Code | 0 0
  1. function quickSortInPlace(arr, left = 0, right = arr.length - 1) {
  2. if (left < right) {
  3. const pivotIndex = partition(arr, left, right);
  4. quickSortInPlace(arr, left, pivotIndex - 1);
  5. quickSortInPlace(arr, pivotIndex + 1, right);
  6. }
  7. return arr;
  8. }
  9.  
  10. function partition(arr, left, right) {
  11. const pivot = arr[right];
  12. let i = left;
  13.  
  14. for (let j = left; j < right; j++) {
  15. if (arr[j] < pivot) {
  16. [arr[i], arr[j]] = [arr[j], arr[i]];
  17. i++;
  18. }
  19. }
  20.  
  21. [arr[i], arr[right]] = [arr[right], arr[i]]; // Move pivot into place
  22. return i;
  23. }
Advertisement
Add Comment
Please, Sign In to add comment