写一个js的快速排序算法
时间: 2023-04-07 10:00:39 浏览: 62
好的,我可以回答这个问题。下面是一个简单的 JavaScript 快速排序算法的实现:
```
function quickSort(arr) {
if (arr.length <= 1) {
return arr;
}
const pivotIndex = Math.floor(arr.length / 2);
const pivot = arr[pivotIndex];
const left = [];
const right = [];
for (let i = 0; i < arr.length; i++) {
if (i === pivotIndex) {
continue;
}
if (arr[i] < pivot) {
left.push(arr[i]);
} else {
right.push(arr[i]);
}
}
return [...quickSort(left), pivot, ...quickSort(right)];
}
const arr = [5, 3, 7, 2, 8, 4, 1, 6];
console.log(quickSort(arr)); // [1, 2, 3, 4, 5, 6, 7, 8]
```
这个算法的时间复杂度为 O(n log n),它通过递归将数组分成较小的部分,然后将它们合并在一起。
相关问题
帮我写一个js 快速排序算法
当然可以!下面是一个基于JavaScript实现的快速排序算法:
```javascript
function quickSort(arr) {
if (arr.length <= 1) {
return arr;
}
const pivot = arr[0];
const left = [];
const right = [];
for (let i = 1; i < arr.length; i++) {
arr[i] < pivot ? left.push(arr[i]) : right.push(arr[i]);
}
return [...quickSort(left), pivot, ...quickSort(right)];
}
```
这个算法的基本思想是:选择一个元素作为基准(一般选择第一个元素),然后将所有比基准小的元素放在左边,所有比基准大的元素放在右边,然后递归地对左右两个子数组进行排序,最终得到排序后的整个数组。
我希望这个算法可以帮助到你。如果你还有其他问题,可以继续问我哦!
用JS写一个快速排序算法
### 回答1:
好的,我可以为您提供一个用JavaScript编写的快速排序算法。代码如下:
```
function quickSort(arr) {
if (arr.length <= 1) {
return arr;
}
const pivotIndex = Math.floor(arr.length / 2);
const pivot = arr[pivotIndex];
const left = [];
const right = [];
for (let i = 0; i < arr.length; i++) {
if (i === pivotIndex) {
continue;
}
if (arr[i] < pivot) {
left.push(arr[i]);
} else {
right.push(arr[i]);
}
}
return [...quickSort(left), pivot, ...quickSort(right)];
}
```
这个算法接受一个数组作为输入,然后通过递归地将数组分成较小的子数组并进行排序,最终返回排序好的数组。
### 回答2:
快速排序算法是一种常见的排序算法,它基于分治的思想。下面是用JavaScript实现快速排序的代码:
```
function quickSort(arr) {
if (arr.length <= 1) {
return arr;
}
const pivot = arr[Math.floor(arr.length / 2)];
const left = [];
const right = [];
for (let i = 0; i < arr.length; i++) {
if (i === Math.floor(arr.length / 2)) {
continue;
}
if (arr[i] < pivot) {
left.push(arr[i]);
} else {
right.push(arr[i]);
}
}
return [...quickSort(left), pivot, ...quickSort(right)];
}
// 示例
const arr = [5, 2, 8, 4, 9, 1, 3];
const sortedArr = quickSort(arr);
console.log(sortedArr); // 输出 [1, 2, 3, 4, 5, 8, 9]
```
这段代码首先判断了输入的数组长度是否小于等于1,如果是的话直接返回原数组,作为递归的终止条件。否则,取数组中间位置的元素作为基准值(pivot),并创建两个空数组用来存放左右两侧的元素。
然后,通过遍历数组中除了中间元素的其他元素,将小于基准值的元素放入左数组,大于等于基准值的元素放入右数组。
最后,通过递归调用quickSort函数对左右数组进行排序,并以 `[...quickSort(left), pivot, ...quickSort(right)]` 的形式返回排序后的数组。
使用示例中的数组 `[5, 2, 8, 4, 9, 1, 3]` 经过快速排序后得到 `[1, 2, 3, 4, 5, 8, 9]`。
快速排序算法的时间复杂度为 O(nlogn),是一种高效的排序算法。
### 回答3:
快速排序是一种常用的排序算法,它使用分治法来对数据集合进行排序。下面是用JavaScript语言编写的快速排序算法:
```javascript
function quickSort(arr) {
if (arr.length <= 1) {
return arr;
}
var pivotIndex = Math.floor(arr.length / 2);
var pivot = arr.splice(pivotIndex, 1)[0];
var left = [];
var right = [];
for (var i = 0; i < arr.length; i++) {
if (arr[i] < pivot) {
left.push(arr[i]);
} else {
right.push(arr[i]);
}
}
return quickSort(left).concat([pivot], quickSort(right));
}
// 测试快速排序算法
var array = [5, 2, 6, 3, 1, 4];
console.log(quickSort(array)); // 输出 [1, 2, 3, 4, 5, 6]
```
上述代码中,`quickSort`函数是递归函数,它将待排序的数组分成两部分,然后再对这两部分分别进行快速排序,最后将左侧部分、基准值和右侧部分拼接起来,得到排序后的数组。
快速排序的时间复杂度取决于分割点的选择,一般情况下平均时间复杂度为O(nlogn),最坏情况下的时间复杂度为O(n^2)。快速排序是一种原地排序算法,不需要额外的存储空间,所以空间复杂度为O(logn)。