MCPcopy Create free account
hub / github.com/Derious/cuMPC / aav86_sort

Function aav86_sort

test/test_CORE/test_sort.cpp:47–91  ·  view source on GitHub ↗

AAV86 排序的核心函数

Source from the content-addressed store, hash-verified

45
46// AAV86 排序的核心函数
47void aav86_sort(vector<int>& arr, int k) {
48 int n = arr.size();
49 if (k == 1 || n <= 1) {
50 sort(arr.begin(), arr.end()); // 基本情况,直接排序
51 return;
52 }
53
54 // 第一步:计算 p = ceil(n^(1/k))
55 int p = ceil(pow(n, 1.0 / k));
56
57 // 第二步:采样 P 集合
58 vector<int> sample_indices = sample(n, p - 1);
59 vector<int> pivots;
60 for (int index : sample_indices) {
61 pivots.push_back(arr[index]);
62 }
63 sort(pivots.begin(), pivots.end()); // 确保采样的锚点已排序
64
65 // 第三步:将数据划分为 p 个块
66 vector<int> A, B;
67 set<int> pivot_set(sample_indices.begin(), sample_indices.end());
68 for (int i = 0; i < n; ++i) {
69 if (pivot_set.count(i)) {
70 B.push_back(arr[i]); // P 集合的元素
71 } else {
72 A.push_back(arr[i]); // 剩余元素
73 }
74 }
75
76 vector<vector<int>> blocks = partition(A, pivots);
77
78 // 第四步:递归排序每个块
79 for (vector<int>& block : blocks) {
80 aav86_sort(block, k - 1); // 递归调用
81 }
82
83 // 第五步:合并所有块和锚点
84 arr.clear();
85 for (size_t i = 0; i < blocks.size(); ++i) {
86 arr.insert(arr.end(), blocks[i].begin(), blocks[i].end());
87 if (i < B.size()) {
88 arr.push_back(B[i]); // 插入锚点
89 }
90 }
91}
92
93vector<int> generateRandomData(int size, int min_val = 1, int max_val = 0xFFFFFF) {
94 vector<int> data(size);

Callers

nothing calls this directly

Calls 11

sampleFunction · 0.85
partitionFunction · 0.70
ceilFunction · 0.50
powFunction · 0.50
sizeMethod · 0.45
beginMethod · 0.45
endMethod · 0.45
push_backMethod · 0.45
countMethod · 0.45
clearMethod · 0.45
insertMethod · 0.45

Tested by

no test coverage detected