算法导论9.3-8-设X[1..n]和Y[1..n]为两个数组,每个都包含n个已排好序的数,给出一个求数组X和数组Y中所有2n个元素的中位数
2014-01-16 16:46
561 查看
#include <iostream> #include <ctime> #include <limits> #include <boost/timer/timer.hpp> using namespace boost::timer; using namespace std; int partition(int *a,int low,int high) { int key=a[high]; int p=low-1; int 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(int *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 find_median(int *a,int *b,int lowA,int highA,int lowB,int highB) { while (lowA<highA && lowB<highB) { int kA=(lowA+highA+1)/2; int kB=(lowB+highB+1)/2; if(a[kA]<b[kB]){ lowA=kA; highB=kB; }else if(a[kA]>b[kB]){ highA=kA; lowB=kB; }else if((a[kA]==b[kB])/* && kA+kB==n*/){ return a[kA]; } } if(lowA==highA) return a[lowA]; if(lowB==highB) return b[lowB]; } int random_parition(int *a,int low,int high) { int index=rand()%(high-low+1)+low; int tmp=a[index]; a[index]=a[high]; a[high]=tmp; return partition(a,low,high); } int random_select(int *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); } } } int random_find_median(int *a,int *b,int lowA,int highA,int lowB,int highB) { while (lowA<highA && lowB<highB) { int kA=(lowA+highA+1)/2; int kB=(lowB+highB+1)/2; random_select(a,lowA,highA,kA+1);//选取A中位数 random_select(b,lowB,highB,kB+1);//选取B中位数 if(a[kA]<b[kB]){ lowA=kA; highB=kB; }else if(a[kA]>b[kB]){ highA=kA; lowB=kB; }else if((a[kA]==b[kB])/* && kA+kB==n*/){ return a[kA]; } } if(lowA==highA){ random_select(a,lowA,highA,lowA+1);//选取A中位数 return a[lowA]; } if(lowB==highB){ random_select(b,lowB,highB,lowB+1);//选取B中位数 return b[lowB]; } } int find_median_two_array(int *a,int *b,int low,int high,int n) { if(low>high){ return numeric_limits<int>::min(); }else{ int k=(low+high)/2; if(k==n && a[k]<b[0]){ return a[k]; } else if(k<n && a[k]>=b[n-k] && a[k]<=b[n-k+1]){ return a[k]; }else if(a[k]>b[n-k+1]){ return find_median_two_array(a,b,low,k-1,n); }else{ return find_median_two_array(a,b,k+1,high,n); } } } int random_find_median_two_array(int *a,int *b,int low,int high,int n) { if(low>high){ return numeric_limits<int>::min(); }else{ int k=(low+high)/2; random_select(a,low,high,k+1); random_select(b,low,high,n-k+1); random_select(b,low,high,n-k+2); if(k==n && a[k]<b[0]){ return a[k]; } else if(k<n && a[k]>=b[n-k] && a[k]<=b[n-k+1]){ return a[k]; }else if(a[k]>b[n-k+1]){ return random_find_median_two_array(a,b,low,k-1,n); }else{ return random_find_median_two_array(a,b,k+1,high,n); } } } int two_array_median(int *a,int *b,int n) { int median=find_median_two_array(a,b,0,n,n); if(median==numeric_limits<int>::min()) median=find_median_two_array(b,a,0,n,n); return median; } int random_two_array_median(int *a,int *b,int n) { int median=random_find_median_two_array(a,b,0,n,n); if(median==numeric_limits<int>::min()) median=random_find_median_two_array(b,a,0,n,n); return median; } int main() { srand((unsigned)time(NULL)); int count; while (count=rand()%10,count<3); count=20000000; int *a=new int[count]; int *b=new int[count]; int *c=new int[count]; int *d=new int[count]; int *e=new int[count]; int *f=new int[count]; int *g=new int[count]; int *h=new int[count]; for(int i=0;i<count;++i){ a[i]=rand()%400000001-200000000; b[i]=rand()%400000001-200000000; c[i]=a[i]; d[i]=b[i]; e[i]=a[i]; f[i]=b[i]; g[i]=a[i]; h[i]=b[i]; } cpu_timer t1; t1.start(); quick_sort_partition(a,0,count-1);//对两个数组快速排序 quick_sort_partition(b,0,count-1);//对两个数组快速排序 /*for(int i=0;i<count;++i){ cout<<a[i]<<"\t"; } cout<<endl; for(int i=0;i<count;++i){ cout<<b[i]<<"\t"; } cout<<endl;*/ int median=find_median(a,b,0,count-1,0,count-1); t1.stop(); cout<<median<<endl; cout<<t1.format(); cpu_timer t2; t2.start(); median=random_find_median(c,d,0,count-1,0,count-1); t2.stop(); cout<<median<<endl; cout<<t2.format(); cpu_timer t3; t3.start(); quick_sort_partition(e,0,count-1);//对两个数组快速排序 quick_sort_partition(f,0,count-1);//对两个数组快速排序 median=two_array_median(e,f,count-1); t3.stop(); cout<<median<<endl; cout<<t3.format(); cpu_timer t4; t4.start(); median=random_two_array_median(g,h,count-1); t4.stop(); cout<<median<<endl; cout<<t4.format(); delete[]a; delete[]b; delete[]c; delete[]d; delete[]e; delete[]f; delete[]g; delete[]h; }
相关文章推荐
- 数组a中每个元素包含两个值第一个是整型第二个是字符串写一个sort函数以第一个值为键按从小到大排序
- List的All方法使用问题:我用List1的All方法来确保List1的所有每个数组元素中是否包含Arr1中的任意元素,这个部分很难实现。 因为,没有一个数组中是否包含另一个数组中的任意元素这个功能
- 编写一个程序,输入两个包含 5 个元素的数组,先将两个数组升序排序,然 后将这两个数组合并成一个升序数组(合并排序)。
- 算法导论 9.3-8 求两个数组的中位数
- 定义一个int型的一维数组,包含10个元素,分别赋一些随机整数,然后求出所有元素的最大值,最小值,平均值,和值,并输出出来。
- 算法导论 9.3-8 求两个数组的中位数
- 数组中的数分为两组,让给出一个算法,使得两个组的和的差的绝对值最小,数组中的数的取值范围是0<x<100,元素个数也是大于0, 小于100 。
- 找出数组X和Y中所有2n个元素的中位数
- 9.28机试 定义两个数组,首先把两个数组合并成一个新数组,然后把新数组中的所有元素逆序排列
- 用于判断数组是否完全包含另外的一个数组的所有元素
- 求k个数组包含每个数组至少一个元素的最小范围(待字闺中,备忘)
- 给定一个数组,求数组中所有元素的可能组合(每个元素只出现一次),使其和等于给定数的解决办法
- 数组中的数分为两组,让给出一个算法,使得两个组的和的差的绝对值最小,数组中的数的取值范围是0<x<100,元素个数也是大于0, 小于100 。
- 给出一个 m*n 的二维数组(元素可为正可为负),求该二维数组的一个子数组,且此子数组中所有元素的和最大,并输出该数组的和。
- 给定一个整数数组,两个数字的返回索引将它们加到一个特定的目标中。 您可以假设每个输入都只有一个解决方案,并且您可能不会两次使用相同的元素。
- Java编程:定义一个int型的一维数组,包含10个元素,分别赋一些随机整数,然后求出所有元素的最大值,最小值,平均值,和值,并输出出来。
- 给出两个表示两个非负整数的非空链表。数字以相反的顺序存储,它们的每个节点都包含一个数字。添加两个数字,并将其作为链接列表返回。
- 获取两个数组的差异元素,返回一个包含差异元素的新数组
- 定义一个int型的一维数组,包含10个元素,分别赋一些随机整数,然后求出所有元素的最大值,最小值,平均值,和值,并输出出来。
- 设计一个算法找到数组中两个元素相加等于指定数的所有组合