js冒泡排序二维数组 奇数索引的数组升序 偶数索引的数组降序
时间: 2024-02-17 10:04:40 浏览: 97
好的,以下是JS冒泡排序二维数组,其中奇数索引的数组升序,偶数索引的数组降序的实现:
```javascript
function bubbleSort(arr) {
for (let i = 0; i < arr.length; i++) {
// 判断当前索引是否为偶数
if (i % 2 === 0) {
// 偶数索引的数组降序
for (let j = 0; j < arr[i].length - 1; j++) {
for (let k = 0; k < arr[i].length - j - 1; k++) {
if (arr[i][k] < arr[i][k + 1]) {
let temp = arr[i][k];
arr[i][k] = arr[i][k + 1];
arr[i][k + 1] = temp;
}
}
}
} else {
// 奇数索引的数组升序
for (let j = 0; j < arr[i].length - 1; j++) {
for (let k = 0; k < arr[i].length - j - 1; k++) {
if (arr[i][k] > arr[i][k + 1]) {
let temp = arr[i][k];
arr[i][k] = arr[i][k + 1];
arr[i][k + 1] = temp;
}
}
}
}
}
return arr;
}
// 测试
let arr = [
[3, 5, 1, 7],
[8, 4, 2, 6],
[9, 0],
[11, 10]
];
console.log(bubbleSort(arr));
```
输出结果为:
```
[
[1, 3, 5, 7],
[8, 6, 4, 2],
[0, 9],
[11, 10]
]
```
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![-](https://img-home.csdnimg.cn/images/20241231044937.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)