算法导论9-2-c-带权中位数
来源:互联网 发布:java 手册百度云 编辑:程序博客网 时间:2024/05/27 01:26
#include <iostream>#include <ctime>#include <limits>#include <boost/timer/timer.hpp>using namespace boost::timer;using namespace std;int partition(double *a,int low,int high){double key=a[high];int p=low-1;double tmp;for(int i=low;i<high;++i){if(a[i]<=key){p++;tmp=a[i];a[i]=a[p];a[p]=tmp;}}tmp=a[p+1];a[p+1]=key;a[high]=tmp;return p+1;}void quick_sort_partition(double *a,int low,int high){if(low<high){int q=partition(a,low,high);quick_sort_partition(a,low,q-1);quick_sort_partition(a,q+1,high);}}int random_parition(double *a,int low,int high){int index=rand()%(high-low+1)+low;double tmp=a[index];a[index]=a[high];a[high]=tmp;return partition(a,low,high);}double random_select(double *a,int low,int high,int index){if(low==high)return a[low];if(low<high){int q=random_parition(a,low,high);int k=q+1;if(k==index){return a[q];}else if(k<index){return random_select(a,q+1,high,index);}else{return random_select(a,low,q-1,index);}}}double find_weight_median(double *a,int low,int high,double w_l,double w_r)//计算带权中位数{int k=(low+high)/2;random_select(a,low,high,k+1);//查找第k+1小的元素double sum_l=w_l,sum_r=w_r; for(int i=low;i<k;++i){sum_l+=a[i];//中位数左边权重值之和}for(int i=k+1;i<=high;++i){sum_r+=a[i];//中位数右边权重值之和}if(sum_l<0.5 && sum_r<=0.5){return a[k];}else if(sum_l>=0.5){w_r=sum_r;return find_weight_median(a,low,k-1,w_l,w_r+a[k]);}else{w_l=sum_l;return find_weight_median(a,k+1,high,sum_l+a[k],w_r);}}void random_weight_number(double *a,int n)//随机生成n个和为1的浮点数{double *b=new double[n];b[n-1]=1;for(int i=0;i<n-1;++i){b[i]=(double) rand() / (RAND_MAX);}quick_sort_partition(b,0,n-1);a[0]=b[0];for(int i=1;i<n;++i){a[i]=b[i]-b[i-1];}delete [] b;}int main(){srand((unsigned)time(NULL));int count;while (count=rand()%10,count<3);count=2000;double *a=new double[count];random_weight_number(a,count);//随机生成count个和为1的浮点数//a[0]=0.014,a[1]=0.047,a[2]=0.05,a[3]=0.09,a[4]=0.095,a[5]=0.126,a[6]=0.128,a[7]=0.133,a[8]=0.134,a[9]=0.183;//a[0]=0.012,a[1]=0.064,a[2]=0.154,a[3]=0.33,a[4]=0.44;//a[0]=0.1,a[1]=0.35,a[2]=0.05,a[3]=0.1,a[4]=0.15,a[5]=0.05,a[6]=0.2;double weight_median= find_weight_median(a,0,count-1,0,0);quick_sort_partition(a,0,count-1);for(int i=0;i<count;++i){cout<<a[i]<<"\t";}cout<<endl;cout<<"weighted median: ";cout<<weight_median<<endl;delete[]a;}
0 0
- 算法导论-9-2-c-带权中位数
- 算法导论-9-2-c-带权中位数
- 算法导论9-2-c-带权中位数
- 算法导论9-2 邮局位置问题的带权中位数解法证明
- 【算法导论】中位数
- 《算法导论》10、中位数和顺序统计学(C++)
- 中位数及带权中位数
- 算法导论代码 第9章 中位数和顺序统计学
- 【算法导论 第9章 中位数和顺序统计学】
- 算法导论第9章 中位数和顺序统计学
- 《算法导论》— Chapter 9 中位数和顺序统计学
- 【算法导论 第9章 中位数和顺序统计学】
- 算法导论读书笔记(9)中位数和顺序统计量
- 带权中位数问题:
- 带权中位数
- 带权中位数
- 带权中位数
- 带权中位数
- 使用法国SOLEN电容有感
- [Vim]UTF-8乱码问题
- [Leetcode] Same Tree (Java)
- Mini2440移植qt-extended4.4.3
- 交叉编译opencv2.31与移植至arm9
- 算法导论9-2-c-带权中位数
- QT Creator和MinGW中配置OpenCV2.3.1
- CCI: Sort a stack
- 幻方程序
- Eclipse + ADT(包括NDK Plugin) + CDT 搭建Android NDK开发环境
- iOS Dev (30) 用 beginAnimations 和 commitAnimations 来实现最简单的动画
- java 常见问题 之 不使用finally块释放资源
- Qt用QAxObject读取excel编译出错问题解决
- Java多线程研究(七)Callable与Future的应用