本文共 758 字,大约阅读时间需要 2 分钟。
一.知识储备
有符号无符号int取值范围:
有符号int 取值范围[- 2^31 , 2^31-1]; 无符号int取值范围[ 0,2^32-1]。关于中位数:数据排序后,位置在最中间的数值。即将数据分成两部分,一部分大于该数值,一部分小于该数值。中位数的位置:当样本数为奇数时,中位数=(N+1)/2 ; 当样本数为偶数时,中位数为N/2与1+N/2的均值(那么10G个数的中位数,就第5G大的数与第5G+1大的数的均值了)。
思想: 假设整数为无符号数 0-2^32-1; 1.读取10g大小的数字,需要一个64bit的数据空间 2*5*2^30>2^32, 这里选用64bit; 2.每次读取2g数据进内存,需要分区间段落 区间数位:2G / 64bit = 256M 个区间。 3.每个区间段落表示的数字范围 32bit的整数最大值为2^32-1,所以区间的范围是2^32 / 256M = 16. 4.遍历10g的数字,找到中位数的区间段 统计每个区间中整数的值。然后从第一个区间的整数值开始累加。当累加到5G时,停止。此时的区间便包含中位数。记下此区间所 表示的范围,设为[a,a+15].并且记下此区间之前所有区间的累加和,设为m。释放掉除包含中位数区间的其他所有区间的内存。 5.再次遍历10g的整数,统计出现在[a,a+15]内整数,有16个数,并且从小到达排序,n0,n1,..n15 6.m+[n0-n15],m加上n0到n15中的数字首次出现大于5g,则a+nx就是所求的中位数。 https://blog.csdn.net/acceptyly/article/details/48240711