(C/C++)給定一個帶權無序陣列,線性複雜度求出其帶權中位數(select分治演算法)
給定一個未排序的陣列(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; }
執行示例

執行示例