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

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

思路

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

伪代码

伪代码

#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;
}

运行示例

运行示例

  • 1
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值