当前位置:   article > 正文

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

查找一个无序数列的中位c++

给定一个未排序的数组(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
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105

运行示例

运行示例

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/784634
推荐阅读
  

闽ICP备14008679号