C++ 恒尺寸阵列的运行中值

C++ 恒尺寸阵列的运行中值,c++,median,C++,Median,我试图找到恒定大小数组的中值。但数组总是上升的。我的意思是新号码被旧号码取代。我把这个过程称为运行中位数,或者我们可以说是动态中位数。。这是我的代码,在代码中,当rand()函数生成78时,代码无法找到正确的中值。(78之前;生成41、67、34、0、69、24) #包括 #包括 #包括 #定义最大尺寸5 使用名称空间std; 布尔isOdd(整数) { 如果(整数%2==0) 返回false; 其他的 返回true; } int main() { 整数中值; 国际*米纳雷; int*maxAr

我试图找到恒定大小数组的中值。但数组总是上升的。我的意思是新号码被旧号码取代。我把这个过程称为运行中位数,或者我们可以说是动态中位数。。这是我的代码,在代码中,当rand()函数生成78时,代码无法找到正确的中值。(78之前;生成41、67、34、0、69、24)

#包括
#包括
#包括
#定义最大尺寸5
使用名称空间std;
布尔isOdd(整数)
{
如果(整数%2==0)
返回false;
其他的
返回true;
}
int main()
{
整数中值;
国际*米纳雷;
int*maxArray;
int myArray[最大大小];

对于(inti=0;i我认为可能有另一种方法

由于您现在可以定义数据集的边界,如所示:
int v=rand()%100;
您还可以跟踪每个数字的出现次数

您需要将出现次数存储在长度为100的数组中。您还需要跟踪“外出”的次数以减少出现次数

如果你已经准备好了,只要从0到100循环,如果你的出现次数大于最大值/2

这将是一个0(n)操作,但开销很大,特别是因为数字0…100的范围比最大值5大得多(反之更好)

无论如何,我认为如果你应用这个原则,你也不会对你的数组的改变有任何问题

如果你愿意,我可以给你举个简单的例子

编辑

此示例并不完美,但您可以尝试一下:

#include <iostream>
#include <stdlib.h>
#include <algorithm>

#define MAX_ELEMENTS 5

#define MAX_VALUE 100

using namespace std;
bool isOdd( int integer )
{

    if ( integer % 2 == 0 )
        return false;
    else
        return true;
}


int main()
{
int median;

int numberOfElements = 0;
int myValueArray[MAX_VALUE];
int myArray[MAX_ELEMENTS];

 //quick n dirty init
 for (int c = 0; c < MAX_VALUE; c++)
    myValueArray[c] = 0;

 for (int c = 0; c < MAX_ELEMENTS; c++)
     myArray[c] = 0;

for(int i=0; i<20; i++)
{
    //generate random number 0...100
    int v = rand() % MAX_VALUE;
    cout << "| " << v << " | "; //incomming value

    myValueArray[v]++;

    int leavingValue = myArray[i%MAX_ELEMENTS]; 
    myArray[i%MAX_ELEMENTS] = v; // just to keep track of leaving value

    if (numberOfElements < MAX_ELEMENTS)
        numberOfElements++;

    else //remove leaving value
    {
        myValueArray[leavingValue]--;
        cout << "| " << leavingValue << " | "; //leaving value
    }

    for (int c = 0, occurances = 0; c < MAX_VALUE; c++)
    {
        occurances += myValueArray[c];

        //(numberOfElements + 1) = dirty indexer correction, but you'll get the point
        if (occurances >= (numberOfElements + 1) / 2)
        {
            if (isOdd(numberOfElements))
                median = c;

            else
                cout << "work to do here...";

            break;
        }
    }

    cout << "array: ";
    //just print the array, to confirm
    for (int c = 0, occurances = 0; c < MAX_VALUE; c++)
    {
        if (myValueArray[c] > 0)
        {
            for (int x = 0; x < myValueArray[c]; x++)
                cout << " {" << c << "}, ";
        }
    }

        cout << " >> median: "<< median<<endl;
    }
   return 0;
}
#包括
#包括
#包括
#定义最大元素5
#定义最大值100
使用名称空间std;
布尔isOdd(整数)
{
如果(整数%2==0)
返回false;
其他的
返回true;
}
int main()
{
整数中值;
int numberOfElements=0;
int myValueArray[最大值];
int myArray[MAX_元素];
//快速n脏初始化
对于(int c=0;cO(n log n)
解决方案:对数组排序,然后选择
n/2
th元素(或中间两个元素的平均值,根据中位数的定义需要)。您可以用于
O(n)
答案。@H2CO3:给定“常量大小”甚至可以使用排序网络不仅在任何O(n log n)中这样做,而且在O(n log n)中这样做,O(n log n)具有恒定的、保证的平均和最坏的运行时间。
#include <iostream>
#include <stdlib.h>
#include <algorithm>

#define MAX_ELEMENTS 5

#define MAX_VALUE 100

using namespace std;
bool isOdd( int integer )
{

    if ( integer % 2 == 0 )
        return false;
    else
        return true;
}


int main()
{
int median;

int numberOfElements = 0;
int myValueArray[MAX_VALUE];
int myArray[MAX_ELEMENTS];

 //quick n dirty init
 for (int c = 0; c < MAX_VALUE; c++)
    myValueArray[c] = 0;

 for (int c = 0; c < MAX_ELEMENTS; c++)
     myArray[c] = 0;

for(int i=0; i<20; i++)
{
    //generate random number 0...100
    int v = rand() % MAX_VALUE;
    cout << "| " << v << " | "; //incomming value

    myValueArray[v]++;

    int leavingValue = myArray[i%MAX_ELEMENTS]; 
    myArray[i%MAX_ELEMENTS] = v; // just to keep track of leaving value

    if (numberOfElements < MAX_ELEMENTS)
        numberOfElements++;

    else //remove leaving value
    {
        myValueArray[leavingValue]--;
        cout << "| " << leavingValue << " | "; //leaving value
    }

    for (int c = 0, occurances = 0; c < MAX_VALUE; c++)
    {
        occurances += myValueArray[c];

        //(numberOfElements + 1) = dirty indexer correction, but you'll get the point
        if (occurances >= (numberOfElements + 1) / 2)
        {
            if (isOdd(numberOfElements))
                median = c;

            else
                cout << "work to do here...";

            break;
        }
    }

    cout << "array: ";
    //just print the array, to confirm
    for (int c = 0, occurances = 0; c < MAX_VALUE; c++)
    {
        if (myValueArray[c] > 0)
        {
            for (int x = 0; x < myValueArray[c]; x++)
                cout << " {" << c << "}, ";
        }
    }

        cout << " >> median: "<< median<<endl;
    }
   return 0;
}