Problem
https://leetcode.com/problems/sort-an-array/
Given an array of integers nums
, sort the array in ascending order.
Solution — Quick Sort
var sortArray = function(nums) {
function _pivot(arr, start, end) {
const pivot = arr[start];
let pivotIdx = start;
for (let i = start; i <= end; i++) {
if (arr[i] < pivot) {
pivotIdx++;
[arr[pivotIdx], arr[i]] = [arr[i], arr[pivotIdx]];
}
}
[arr[pivotIdx], arr[start]] = [arr[start], arr[pivotIdx]];
return pivotIdx;
}
function _quickSort(arr, left = 0, right = arr.length - 1) {
if (left < right) {
const pivotIdx = _pivot(arr, left, right);
_quickSort(arr, left, pivotIdx - 1);
_quickSort(arr, pivotIdx + 1, right);
}
}
_quickSort(nums);
return nums;
};
This is the article explaining Quick Sort algorithm