寻找一个数组的中位数
来源:互联网 发布:火车头数据采集器 编辑:程序博客网 时间:2024/06/07 07:22
一,我的方法
double median(double arr[],int num){double total = 0;for(int i=0;i<num;i++){for(int j=0;j<num-i-1;j++){int temp = 0;if(arr[j]>arr[j+1]){temp = arr[j];arr[j] = arr[j+1];arr[j+1] = temp;}}}total = arr[num/2];return total;}思想:对这个数组进行冒泡排序然后找中间的那一个。
二,更好的方法
#include<stdio.h>void main(){int x[] = { 10,11,10,12,9,8,7 };int number = 7;int median = 0;int j = -1;int count_lower = 0;int count_higher = 0;do{j++;count_lower = 0; count_higher = 0;for (int i = 0; i < number; i++){if (x[j] <= x[i])count_higher++;if (x[j] >= x[i])count_lower++;}}while (j < number && ((count_lower <= (double)(number)/ 2.)|| (count_higher <= (double)(number) / 2.)));median = x[j];printf("中位数是:%d",median);思想:每一个数与数组的所有数字比较,若是不满足while里面的条件便是中位数,可以提前退出。(为什么这么写的原因,提前退出减少了时间复杂度)。
阅读全文
0 0
- 寻找一个数组的中位数
- 寻找两个数组的中位数
- 寻找无序数组的中位数
- 寻找两个排序数组的中位数
- 关于在一个序列中寻找中位数和第K大的数(在两个等长有序数组中寻找中位数)
- 寻找两个有序数组合并之后的中位数
- 2个有序数组,寻找他们合并之后的中位数
- 寻找两个等size有序数组的中位数
- leetcode题目 寻找两个排序数组的中位数
- 求一个无序数组的中位数
- 找出一个无序数组的中位数
- 求一个无序数组的中位数
- 求一个无序数组的中位数。
- 求一个无序数组的中位数
- 求一个无序数组的中位数
- 求一个无序数组的中位数
- 求一个无序数组的中位数
- 寻找3个数的中位数
- 交叉编译zlib
- [11.8]模拟
- 文章标题
- obj + mtl 格式说明
- 解决xib requirs xcode9.0 or later问题
- 寻找一个数组的中位数
- linux 下的mysql软件
- CCF CSP 2016年04月第4题 游戏 (BFS)
- LeetCode Median of Two Sorted Arrays
- 查看spring源码出现找不到com.sun.net.httpserver.HttpServer错误
- TEXT
- AndroidStudio 导入项目 Building gradle project info 卡住
- Python---注释
- Tarjan模板