js求数组的中位数
时间: 2023-08-05 17:05:53 浏览: 74
计算数组的中位数,可以按照以下步骤进行:
1. 将数组从小到大排序
2. 如果数组长度是奇数,则中位数为排序后数组的中间数
3. 如果数组长度是偶数,则中位数为排序后数组中间两个数的平均值
下面是一个 JavaScript 的代码示例:
```javascript
function median(arr) {
// 数组从小到大排序
arr.sort(function(a, b) {
return a - b;
});
var len = arr.length;
// 判断数组长度的奇偶性
if (len % 2 === 0) {
// 如果数组长度是偶数,返回中间两个数的平均值
return (arr[len / 2 - 1] + arr[len / 2]) / 2;
} else {
// 如果数组长度是奇数,返回中间数
return arr[(len - 1) / 2];
}
}
// 示例
var arr = [1, 2, 3, 4, 5];
console.log(median(arr)); // 输出 3
```
相关问题
js如何获取一个json数组的中位数
要获取一个 JSON 数组的中位数,你可以按照以下步骤进行操作:
1. 解析 JSON 字符串,将其转换为 JavaScript 对象或数组。
2. 对数组进行排序,以便以升序或降序的方式排列元素。
3. 确定数组的长度。
4. 根据数组的长度,判断中位数的位置:
- 如果数组长度为奇数,中位数位置为 (数组长度 - 1) / 2。
- 如果数组长度为偶数,中位数位置为数组长度 / 2 和 (数组长度 / 2) - 1 之间的平均值。
5. 根据中位数的位置获取对应的元素值。
以下是一个示例代码,演示如何获取 JSON 数组的中位数:
```javascript
const jsonStr = '[1, 3, 5, 7, 9]';
const arr = JSON.parse(jsonStr); // 解析 JSON 字符串为数组
arr.sort((a, b) => a - b); // 数组排序
const length = arr.length;
let median;
if (length % 2 === 0) {
const midIndex1 = length / 2;
const midIndex2 = (length / 2) - 1;
median = (arr[midIndex1] + arr[midIndex2]) / 2; // 偶数个元素取中间两个元素的平均值
} else {
const midIndex = Math.floor(length / 2);
median = arr[midIndex]; // 奇数个元素取中间元素
}
console.log(median); // 输出中位数
```
注意:在实际应用中,需要根据具体情况处理 JSON 字符串的解析和排序方式。例如,如果 JSON 数组中的元素是对象,你可能需要指定键来进行排序。
js获取一个未经排序数组的中位数
可以采用快速选择算法(QuickSelect),其基本思想与快速排序类似,但不需要对整个数组进行排序,只需要找到中位数所在的位置即可。
具体步骤如下:
1. 随机选择数组中的一个元素pivot,将数组分成两部分,左边的元素都小于等于pivot,右边的元素都大于等于pivot。
2. 计算pivot在排序后的位置,如果该位置等于中位数所在的位置,则直接返回pivot;否则判断该位置与中位数所在的位置的大小关系,如果该位置小于中位数所在的位置,则在右半部分继续查找;否则在左半部分继续查找。
3. 重复上述步骤,直到找到中位数所在的位置。
代码如下:
function findMedian(arr) {
var len = arr.length;
var mid = Math.floor(len / 2);
var left = 0, right = len - 1;
while (left <= right) {
var pivotIndex = partition(arr, left, right);
if (pivotIndex == mid) {
// 中位数为奇数个,直接返回pivot
if (len % 2 == 1) {
return arr[mid];
}
// 中位数为偶数个,计算中间两个数的平均值
else {
return (arr[mid] + arr[mid-1]) / 2;
}
}
else if (pivotIndex < mid) {
left = pivotIndex + 1;
}
else {
right = pivotIndex - 1;
}
}
}
function partition(arr, left, right) {
var pivotIndex = Math.floor(Math.random() * (right - left + 1)) + left;
var pivot = arr[pivotIndex];
swap(arr, pivotIndex, right);
var storeIndex = left;
for (var i = left; i < right; i++) {
if (arr[i] <= pivot) {
swap(arr, i, storeIndex);
storeIndex++;
}
}
swap(arr, storeIndex, right);
return storeIndex;
}
function swap(arr, i, j) {
var temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
示例:
var arr = [3, 1, 4, 2, 5];
console.log(findMedian(arr)); // 3
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)