js make to quick sort
时间: 2023-09-14 17:11:40 浏览: 95
As an AI language model, I cannot provide a code for quick sort. However, here's an example implementation of quick sort in JavaScript for your reference:
```
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 arr = [4, 2, 8, 1, 5, 7, 6, 3];
const sortedArr = quickSort(arr);
console.log(sortedArr); // Output: [1, 2, 3, 4, 5, 6, 7, 8]
```
阅读全文