爲什麼不建立直方圖?即屬於幾個類別的情況(值)的數量。類別應該是連續的,不重疊的變量區間。使用此直方圖可以對中間值進行首次估計(即中值在[a,b]之間),並知道有多少值落入該區間(H)。如果H < = N,請再次讀取數字,忽略超出此間隔的數字,並將間隔內的數字移至RAM。找到中位數。
如果H> N,做一個新的間隔分區並重復該過程。它不應該超過2或3次迭代。
請注意,對於每個分區,只需存儲a,b,一個Delta和具有落入每個子區間的值數量的數組。
編輯。它預計會變得更加複雜一些。在估計間隔中間值後的每次迭代中,我們還應該考慮在這個間隔的右側和左側留下「多少」直方圖。我也改變了停止條件。無論如何,我做了一個C++實現。
#include <iostream>
#include <algorithm>
#include <time.h>
#include <stdlib.h>
//This is N^2... or just the number of values in your array,
//note that we never modify it except at the end (just for sorting
//and testing purposes).
#define N2 1000000
//Number of elements in the histogram. Must be >2
#define HISTN 1000
double findmedian (double *values, double min, double max);
int getindex (int *hist);
void put (int *hist, double min, double max, double val, double delta);
int main()
{
//Set max and min to the max/min values your array variables can hold,
//calculate it, or maybe we know that they are bounded
double max=1000.0;
double min=0.0;
double delta;
double values[N2];
int hist[HISTN];
int ind;
double median;
int iter=0;
//Initialize with random values
srand ((unsigned) (time(0)));
for (int i=0; i<N2; ++i)
values[i]=((double)rand()/(double)RAND_MAX);
double imin=min;
double imax=max;
clock_t begin=clock();
while (1) {
iter++;
for (int i=0; i<HISTN; ++i)
hist[i]=0;
delta=(imax-imin)/HISTN;
for (int j=0; j<N2; ++j)
put (hist, imin, imax, values[j], delta);
ind=getindex (hist);
imax=imin;
imin=imin+delta*ind;
imax=imax+delta*(ind+1);
if (hist[ind]==1 || imax-imin<=DBL_MIN) {
median=findmedian (values, imin, imax);
break;
}
}
clock_t end=clock();
std::cout << "Median with our algorithm: " << median << " - " << iter << "iterations of the algorithm" << std::endl;
double time=(double)(end-begin)/CLOCKS_PER_SEC;
std::cout << "Time: " << time << std::endl;
//Let's compare our result with the median calculated after sorting the
//array
//Should be values[(int)N2/2] if N2 is odd
begin=clock();
std::sort (values, values+N2);
std::cout << "Median after sorting: " << values[(int)N2/2-1] << std::endl;
end=clock();
time=(double)(end-begin)/CLOCKS_PER_SEC;
std::cout << "Time: " << time << std::endl;
return 0;
}
double findmedian (double *values, double min, double max) {
for (int i=0; i<N2; ++i)
if (values[i]>=min && values[i]<=max)
return values[i];
return 0;
}
int getindex (int *hist)
{
static int pd=0;
int left=0;
int right=0;
int i;
for (int k=0; k<HISTN; k++)
right+=hist[k];
for (i=0; i<HISTN; i++) {
right-=hist[i];
if (i>0)
left+=hist[i-1];
if (hist[i]>0) {
if (pd+right-left<=hist[i]) {
pd=pd+right-left;
break;
}
}
}
return i;
}
void put (int *hist, double min, double max, double val, double delta)
{
int pos;
if (val<min || val>max)
return;
pos=(val-min)/delta;
hist[pos]++;
return;
}
我還包括一個天真的計算中位數(排序),以便與算法的結果進行比較。4或5次迭代就足夠了。這意味着我們只需要從網絡或硬盤讀取設置4-5次。
的一些結果:
N2=10000
HISTN=100
Median with our algorithm: 0.497143 - 4 iterations of the algorithm
Time: 0.000787
Median after sorting: 0.497143
Time: 0.001626
(Algorithm is 2 times faster)
N2=1000000
HISTN=1000
Median with our algorithm: 0.500665 - 4 iterations of the algorithm
Time: 0.028874
Median after sorting: 0.500665
Time: 0.097498
(Algorithm is ~3 times faster)
如果要並行算法,每臺機器可以有N個元素,並計算直方圖。一旦計算出來,他們就會將它發送給主機器,這將對所有的直方圖進行求和(容易,它可以非常小...算法甚至可以在2個間隔的直方圖上運行)。然後它會發送新的指令(即新的時間間隔)給從機,以便計算新的直方圖。請注意,每臺機器不需要了解其他機器擁有的N個元素。
它可以使用兩堆完成。請參閱此問題的最常見答案[http://stackoverflow.com/questions/3440905/find-the-median-from-a-stream-of-integers/3441042#3441042] – deinst
@ deinst-該方法需要如果你想把所有東西都放到主存中,一些不太重要的修改。 – templatetypedef
有線性時間算法來尋找中位數。你提到分佈式計算。你是否假設你有K個處理器,每個處理器有N個內存元素,並且你想獲得一個良好的分佈式運行時間,例如你除以K?爲此,理想情況下需要線性時間基本算法,而不是O(N log N)算法。 – user2566092