美文网首页程序员
(C/C++)给定一个带权无序数组,线性复杂度求出其带权中位数(

(C/C++)给定一个带权无序数组,线性复杂度求出其带权中位数(

作者: 魔娃 | 来源:发表于2019-03-28 12:34 被阅读0次

    给定一个未排序的数组(x1, x2, … ,xn),其中每个元素关联一个权值:(w1, w2, … ,wn),且 。请设计一个线性时间的算法,在该数组中查找其带权中位数xk,满足:


    image

    思路

    基于寻找无序数组第k小个数的select算法,以rand()选出的pivot将数组分为三个部分,并进行前后两部分权值总和的判断。
    若leftWeight <=0.5 && rightWeight <=0.5,pivot为带权中位数
    否则,若leftWeight > rightWeight,则带权中位数在pivot左侧数组中,将右侧权值赋给pivot,进行递归
    leftWeight<= rightWeight同理

    伪代码

    3.png
    #include <iostream>
    #include <iomanip>
    #include <stdlib.h>
    #include <time.h>
    
    #define N 5
    using namespace std;
    
    struct node
    {
        int value;
        double weight;
    };
    
    const int VALUE_MAX = 100;
    node INDEX[N] = { {1,0.6},{2,0.1},{5,0.1},{3,0.1},{4,0.1} };
    
    void Print(node *A, int len)
    {
        int i;
        for (i = 0; i < len; i++)
            cout << A[i].value << '\t';
        cout << endl;
        for (i = 0; i < len; i++)
            cout << A[i].weight << '\t';
        cout << endl;
    }
    
    //返回选定的pivot中值
    int Partition(node *A, int begin, int end)
    {
        int less = begin - 1, i;
        int pivotIndex = begin + rand() % (end - begin + 1);
        for (i = begin; i <= end; i++)
        {
            if (A[i].value < A[pivotIndex].value)
            {
                less++;
                swap(A[less], A[i]);
            }
        }
        swap(A[less + 1], A[pivotIndex]);
        return less + 1;
    }
    
    double getSumWeight(node*A, int begin, int end) {
        double sum = 0;
        for (int i = begin; i <= end; i++) {
            sum += A[i].weight;
        }
        return sum;
    }
    
    //
    int selectWeightedMedian(node* index, int begin, int end) {
        if (begin == end)
            return index[begin].value;
        if (end - begin == 1) {
            if (index[begin].weight == index[end].weight)
                return (index[begin].value + index[end].value) / 2;
            if (index[begin].weight > index[end].weight)
                return index[begin].value;
            else
                return index[end].value;
        }
        int midIndex = Partition(index, begin, end);
        double leftWeight = getSumWeight(index, begin, midIndex - 1);
        double rightWeight = getSumWeight(index, midIndex + 1, end);
        if (leftWeight <= 0.5&&rightWeight <= 0.5)
            return index[midIndex].value;
        else
            if (leftWeight > rightWeight) {
                index[midIndex].weight += rightWeight;
                return selectWeightedMedian(index, begin, midIndex);
            }
            else {
                index[midIndex].weight += leftWeight;
                return selectWeightedMedian(index, midIndex, end);
            }
    }
    
    int main(void) {
        srand((int)time(0));
        cout << setprecision(3);
        int length, sum = 0;
        cout << "请输入数组长度:";
        cin >> length;
        node *index = new node[length + 1];
        int * weights = new int[length + 1];
        //生成随机数据
        for (int i = 0; i < length; i++)
        {
            index[i].value = rand() % VALUE_MAX;
            do { weights[i] = rand() % VALUE_MAX; } while (weights[i] == 0);
            sum = sum + weights[i];
        }
        //将权值规格化
        for (int i = 0; i < length; i++)
            index[i].weight = (double)weights[i] / sum;
        //打印生成的数据
        Print(index, length);
        cout << "带权中位数为:" << selectWeightedMedian(index, 0, length - 1) << endl;
        system("pause");
        return 0;
    }
    

    运行示例

    运行示例

    相关文章

      网友评论

        本文标题:(C/C++)给定一个带权无序数组,线性复杂度求出其带权中位数(

        本文链接:https://www.haomeiwen.com/subject/bjkqbqtx.html