xxxxxxxxxx
function quickSort(arr) {
if (arr.length <= 1) {
return arr;
}
const pivot = arr[arr.length - 1];
const left = [];
const right = [];
for (let i = 0; i < arr.length - 1; i++) {
if (arr[i] < pivot) {
left.push(arr[i]);
} else {
right.push(arr[i]);
}
}
return [quickSort(left), pivot, quickSort(right)];
}
// Example usage:
const array = [35, 2, 12, 6, 21, 14];
console.log(quickSort(array)); // Output: [2, 6, 12, 14, 21, 35]
xxxxxxxxxx
const quicksort = arr =>
arr.length <= 1
? arr
: [
quicksort(arr.slice(1).filter((el) => el < arr[0])),
arr[0],
quicksort(arr.slice(1).filter((el) => el >= arr[0])),
];
xxxxxxxxxx
function quickSort(arr) {
if (arr.length <= 1) {
return arr;
}
const pivot = arr[arr.length - 1];
const left = [];
const right = [];
for (let i = 0; i < arr.length - 1; i++) {
if (arr[i] < pivot) {
left.push(arr[i]);
} else {
right.push(arr[i]);
}
}
return [quickSort(left), pivot, quickSort(right)];
}
// Test the quickSort function
const arr = [5, 2, 9, 1, 7, 6, 4, 8, 3];
const sortedArr = quickSort(arr);
console.log(sortedArr);