Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function quickSortInPlace(arr, left = 0, right = arr.length - 1) {
- if (left < right) {
- const pivotIndex = partition(arr, left, right);
- quickSortInPlace(arr, left, pivotIndex - 1);
- quickSortInPlace(arr, pivotIndex + 1, right);
- }
- return arr;
- }
- function partition(arr, left, right) {
- const pivot = arr[right];
- let i = left;
- for (let j = left; j < right; j++) {
- if (arr[j] < pivot) {
- [arr[i], arr[j]] = [arr[j], arr[i]];
- i++;
- }
- }
- [arr[i], arr[right]] = [arr[right], arr[i]]; // Move pivot into place
- return i;
- }
Advertisement
Add Comment
Please, Sign In to add comment