Worst Case Time Complexity: O(n^2)
const quickSort = (arr, low, high) => {
if (low >= 0 && high >= 0 && low < high) {
const partition = getPartition(arr, low, high);
quickSort(arr, low, partition);
quickSort(arr, partition + 1, high);
}
};
const getPartition = (arr, low, high) => {
const pivot = arr[low + Math.floor((high - low) / 2)];
low--;
high++;
while (true) {
do {
low++;
} while (arr[low] < pivot)
do {
high--;
} while (arr[high] > pivot)
if (low >= high) {
return high;
}
swap(arr, low, high);
}
};
const swap = (arr, a, b) => {
const temp = arr[a];
arr[a] = arr[b];
arr[b] = temp;
};
const arr = [4, 3, -1, 4, 1, 0];
quickSort(arr, 0, arr.length - 1);
console.log(arr); // [-1, 0, 1, 3, 4, 4]